<?php
  require __DIR__.'/head.html';
  require __DIR__.'/header.html';
  $File_Ming = 'fileinfo';
  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">
            <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.编辑文件内容框留空默认删除当前编辑的文件<br />
                        3.文件上传支持zip压缩自动解压成功后删除压缩包<br />
                        4.接口默认入口文件为index.php
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <div class="form-group">
                            <div class="apihttp">
                                <div class="row">
                                    <div class="col-12 col-lg-2">
                                        <div class="col-12 col-lg-12">
                                            <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_alias']; ?>">
                                                        <?php echo $v['l_title']; ?>
                                                    </option>
                                                    <?php endforeach;
                                                        endif;
                                                        ?>
                                                </select>
                                                <div class="form-group">
                                                    <input id="apiok" type="button" value="选定接口"
                                                        class=" form-control btn btn-info btn-sm">
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label for="table">上传接口文件</label>
                                                <form action="/admin/putApiFile" method="POST"
                                                    enctype="multipart/form-data">
                                                    <input type="hidden" name="op" value="" id="op">
                                                    <input type="file" name="file" id="files">
                                                    <input id="upfile1" type="submit" value="上传文件"
                                                        class=" form-control btn btn-info btn-sm">
                                                </form>
                                            </div>
                                        </div>
                                        <div class="col-lg-12">
                                            <div class="form-group">
                                                <label for="table">编辑文件列表</label>
                                                <select class="form-control" name="table" id="filelist">

                                                </select>
                                                <input id="upfilelist" type="button" value="更新列表"
                                                    class=" form-control btn btn-info btn-sm">
                                            </div>
                                        </div>

                                    </div>

                                    <div class="col-12 col-lg-10">
                                        <div class="form-group">
                                            <label for="where">编辑文件</label>
                                            <textarea name="content" id="content" rows="15"
                                                class="w-100 mono"></textarea>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </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>
        </div>
    </div>
    <?php  require __DIR__.'/fooer.html'; ?>
    <script>
        var optapif = '';
        //获取文件列表
        $('#apiok').click(function () {
            $('#filelist').html('');
            $("#content").val('');
            optapif = $("#optApi").val();
            $("#op").val(optapif);
            fd = new FormData();
            fd.append("optapi", optapif);
            $.ajax({
                url: '/admin/getApiFile',
                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);
                        $('#filelist').html(file_html(data.data));
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (XMLResponse) {
                    layer.msg(XMLResponse.error);
                }
            });
        });

        // 更新文件列表
        $('#upfilelist').click(function () {
            if (optapif == '') {
                layer.msg('请先选定接口')
                return;
            }
            fd = new FormData();
            fd.append("optapi", optapif);
            $.ajax({
                url: '/admin/getApiFile',
                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('更新成功');
                        $('#filelist').html(file_html(data.data));
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (XMLResponse) {
                    layer.msg(XMLResponse.error);
                }
            });
        });

        // 获取文件内容
        $("#filelist").change(function () {
            if ($("#filelist").val() == "") {
                $("#content").val('');
                return;
            }
            var opt = $("#filelist").val();
            fd = new FormData();
            fd.append("optapi", optapif);
            fd.append("file", opt);
            $.ajax({
                url: '/admin/getApiFileC',
                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);
                        $("#content").val(data.content);
                    } else {
                        layer.msg(data.msg);
                        $("#content").val('');
                    }
                },
                error: function (XMLResponse) {
                    layer.msg(XMLResponse.error);
                }
            });
        });

        // 文件上传
        $("#upfile").click(function () {
            if ($("#files").val() == '') {
                return;
            }
            var opt = $("#filelist").val();
            fd = new FormData();
            fd.append("optapi", optapif);
            fd.append("file", $("#files")[0]);
            $.ajax({
                url: '/admin/putApiFile',
                type: "FILES",
                async: false,
                timeout: "30000",
                dataType: "json",
                contentType: false,
                data: fd,
                contentType: false,
                processData: false,
                success: function (data) {
                    if (data.code == "200") {
                        layer.msg(data.msg);
                        $('#upfilelist').click();
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (XMLResponse) {
                    layer.msg(XMLResponse.error);
                }
            });
        });


        // 文件列表生成器
        function file_html(data) {
            var code_main = '<option value="">' + "请选择文件" + '</option>';
            var code_bottom = '';
            var i = 0;
            data.forEach(element => {
                code_main = code_main + '<option value="' + element + '">' + element + '</option>';
                i++;
            });
            return code_main;
        }

        //保存修改文件
        $('#button').click(function () {
            if (optapif != "") {
                if ($('#filelist').val() == '') {
                    layer.msg("请选择修改文件");
                    return;
                }
                fd = new FormData();
                fd.append('optapi', optapif);
                fd.append('content', $('#content').val());
                fd.append('filename', $('#filelist').val());
                $.ajax({
                    url: '/admin/editFile',
                    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);
                        } else {
                            layer.msg(data.msg);
                        }
                    },
                    error: function (XMLResponse) {
                        layer.msg(XMLResponse.error);
                    }
                });
            } else {
                layer.msg("请先选定接口");
            }
        });
    </script>
    </body>

    </html>