<?php
  require __DIR__.'/head.html';
  require __DIR__.'/header.html';
  $File_Ming = 'datainfo';
  require __DIR__.'/nav.html';
  ?>
<!-- 主体 -->
<main class="app-content">
    <div class="app-title">
        <ul class="app-breadcrumb breadcrumb">
            <li class="breadcrumb-item"><i class="fa fa-home fa-lg"></i></li>
            <li class="breadcrumb-item">数据绑定</li>
        </ul>
    </div>
    <div class="row">
        <div class="col-md-12">
            <form id="admin-datainfo">
                <div class="tile">
                    <div class="bs-component">
                        <div class="alert alert-dismissible alert-success">
                            <button class="close" type="button" data-dismiss="alert">×</button>
                            1.无查询条件 查询条件字段 可为空;<br />
                            2.自定义参数绑定查询字段，例：查询条件【u_id=? and name=?】自定义参数【id,name】 请求 /?id=1&name=admin<br />
                            3.默认自带参数 page当前页数 list取出数量(最大不会超过 取出数量 限制)，请求 page=0&list=10<br />
                            4.排序条件字段 配合 排序方式 使用，排序条件留空排序方式不生效
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-12">
                            <div class="form-group">
                                <div class="apihttp">
                                    <div class="row">
                                        <div class="col-lg-5">
                                            <div class="form-group">
                                                <label for="optApi">选择绑定接口</label>
                                                <select class="form-control" name="optApi" id="optApi">
                                                    <?php if(isset($data['apilist']) && !empty($data['apilist'])):
                                               foreach ($data['apilist'] as $k => $v) : ?>
                                                    <option value="<?php echo $v['l_id']; ?>">
                                                        <?php echo $v['l_title']; ?>
                                                    </option>
                                                    <?php endforeach;
                                                endif; ?>
                                                </select>
                                            </div>
                                        </div>

                                        <div class="col-lg-7">
                                            <div class="form-group">
                                                <label for="val">自定义请求参数</label>
                                                <input class="form-control" id="val" type="text" aria-describedby="val"
                                                    name="val" value="" placeholder="page,list 多个用 , 隔开">
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="apihttp">
                                    <div class="row">
                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="table">选择绑定数据表</label>
                                                <select class="form-control" name="table" id="table">
                                                    <?php 
                                                    $isno=['api_admin','api_user','api_whitelist','api_binding','api_order','api_owned','api_parameter','api_price'];
                                                    if(isset($data['table']) && !empty($data['table'])):
                                                    foreach ($data['table'] as $k => $v) : 
                                                    if(!in_array($v,$isno)):
                                                    ?>
                                                    <option value="<?php echo $v; ?>"><?php echo $v; ?>
                                                    </option>
                                                    <?php 
                                                    endif;
                                                    endforeach;
                                                    endif; ?>
                                                </select>
                                                <div class="form-group">
                                                    <input id="getField" type="button" value="获取字段"
                                                        class="form-control btn btn-info btn-sm">
                                                </div>
                                            </div>
                                        </div>

                                        <div class="col-lg-4">
                                            <div class="form-group">
                                                <label for="where">查询条件字段</label>
                                                <input class="form-control" id="where" type="text"
                                                    aria-describedby="where" name="where" value=""
                                                    placeholder="a_u_id>1 AND 1=1">
                                            </div>
                                        </div>
                                        <div class="col-lg-1">
                                            <div class="form-group">
                                                <label for="item">取出数量</label>
                                                <input class="form-control" id="item" type="text"
                                                    aria-describedby="item" name="item" value="" placeholder="10">
                                            </div>
                                        </div>

                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="sort_field">排序条件字段</label>
                                                <input class="form-control" id="sort_field" type="text"
                                                    aria-describedby="sort_field" name="sort_field" value=""
                                                    placeholder="id">
                                            </div>
                                        </div>

                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="sort">排序方式</label>
                                                <select class="form-control" name="sort" id="sort">
                                                    <option value="1">最新</option>
                                                    <option value="2">最早</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- <div class="form-group">
                                <label class="control-label">工作原理：
                                    <span style="color:#e60c0c">

                                    </span>
                                </label>
                            </div> -->

                            <style>
                                .xc {
                                    max-width: auto;
                                    float: left;
                                    margin-right: 20px;
                                }
                            </style>
                            <div class="form-group" id="field"> </div>
                        </div>

                    </div>
                    <div class="tile-footer">
                        <input value="datainfo" name="target" style="display: none">
                        <button type="button" id="button" class="btn btn-primary">添加</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <?php  require __DIR__.'/fooer.html'; ?>
    <script>
        //获取字段
        $('#getField').click(function () {
            fd = new FormData();
            fd.append("field", $("#table").val());
            $.ajax({
                url: '/admin/getField',
                type: "POST",
                timeout: "3000",
                dataType: "json",
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                data: fd,
                contentType: false,
                processData: false,
                success: function (data) {
                    if (data.code == "200") {
                        layer.msg(data.msg);
                        $('#field').html(field_html(data.data));
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (XMLResponse) {
                    layer.msg(XMLResponse.error);
                }
            });
        });

        //字段生成器
        function field_html(data) {
            var code_top = '<label for="ten">选择关联数据字段</label><br>';
            var code_main = '';
            var code_bottom = '';
            var i = 0;
            data.forEach(element => {
                code_main = code_main + '<div class="animated-checkbox xc"> <label> <input name="field[' + i + ']" type="checkbox" value="' + element + '"><span class="label-text">' + element + '</span> </label> </div>';
                i++;
            });
            return code_top + code_main;
        }

        //添加数据
        $('#admin-datainfo #button').click(function () {
            if ($("input[type='checkbox']").is(':checked')) {
                fd = new FormData(document.getElementById('admin-datainfo'));
                $.ajax({
                    url: '/admin/datainfo',
                    type: "POST",
                    timeout: "5000",
                    dataType: "json",
                    contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                    data: fd,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        if (data.code == "200") {
                            layer.msg(data.msg, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(data.msg);
                        }
                    },
                    error: function (XMLResponse) {
                        layer.msg(XMLResponse.error);
                    }
                });
            } else {
                layer.msg("请先选择关联字段");
            }
        });
    </script>
    </body>

    </html>