<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>标准方案管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base"/>
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card layui-form">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="JXSGL_JXSGL_JXSGL_T" html='<li class="layui-this"><a href="${ctx}/planManage/index">标准方案管理</a></li>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset" id="reset">
                    <i class="layui-icon fs-12">&#xe9aa;</i>
                    重置
                </button>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " id="searchBtn" data-type="reload">
                    <i class="layui-icon fs-12">&#xe615;</i>查询
                </button>
            </ul>
        </div>

        <div class="layui-card-body layuiadmin-card-header-auto" >
            <form class="layui-form layui-card" action="" id="searchForm">
                <div class="layui-form-item">
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">方案名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" class="layui-input" id="name" >
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">方案编号：</label>
                        <div class="layui-input-block">
                            <input type="text" name="code" class="layui-input" id="code" >
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">方案类型：</label>
                        <div class="layui-input-block">
                            <select name="planTypeId" id="planTypeId">
                                <option value="">请选择</option>
                                <c:forEach items="${insta}" var="ins">
                                    <option value="${ins.columns.id}">${ins.columns.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-lg4 layui-col-sm6">
                        <label class="layui-form-label">创建时间：</label>
                        <div class="layui-input-block">
                            <div class="layui-col-xs3">
                                <input type="text" name="createTimeSta" class="layui-input" id="startTime" placeholder="开始日期">
                            </div>
                            <div class="layui-col-xs1 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs3">
                                <input type="text" name="createTimeEnd" class="layui-input" id="endTime" placeholder="结束日期">
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <div class="layui-card-body">
            <table class="layui-hide " id="test" lay-filter="test"></table>
        </div>

    </div>
</div>
<div id="toolbarDemo" class="layui-form-item layui-hide">
    <sfTags:pagePermission authFlag="FAGL_FAGL_FAGL_ADD_B" html='<button class="layui-btn layui-btn-sm" id="addBtn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加 </button>'/>
    <sfTags:pagePermission authFlag="FAGL_FAGL_FAGL_EDIT_B" html='<button class="layui-btn layui-btn-sm" id="editBtn" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑 </button>'/>
    <sfTags:pagePermission authFlag="FAGL_FAGL_FAGL_DELETE_B" html='<button class="layui-btn layui-btn-sm" id="deleteBtn" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除 </button>'/>
</div>
</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/data.js"></script>
<script type="text/javascript">
    var saveMark = false;
    //初始数据
    var $form;
    var form;
    var upload;
    var $;
    var table;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laypage','laydate','form','excel'], function(){
        $ = layui.$
            ,form = layui.form
            ,tableExt = layui.tableExt;
        table = layui.table;
        laydate = layui.laydate;

        var excel = layui.excel;
        var detail = '${fns:checkPagePermission(request, "FAGL_FAGL_FAGL_DETAIL_B")}';
        var download = '${fns:checkPagePermission(request, "FAGL_FAGL_FAGL_DOWNLOAD_B")}';

        //开始日期
        var insStart = laydate.render({
            elem: '#startTime'
            ,done: function(value, date){
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });

                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });

        //结束日期
        var insEnd = laydate.render({
            elem: '#endTime'
            ,done: function(value, date){
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //方案信息列表
        tableExt.render({
            elem: '#test'
            , height: 'full-130'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/planManage/list?type=1'
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , page: true
            , toolbar:"#toolbarDemo"
            , method:"post"
            , limits: [20, 50, 100]
            , limit: 20
            , cols: [[
                {field: 'number', minWidth: 50, title: '序号', align: 'center', type: 'numbers',rowspan:'2'}
                ,{field : 'id', minWidth : '4%',checkbox : true,align : 'center',rowspan:'2'}
                , {field: 'code', title: '方案编号', minWidth: 200, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'name', title: '方案名称', minWidth: 150, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'plan_type', title: '方案类型', minWidth: 200, align: 'center',exportable:true ,rowspan:'2'}
                , {field: 'attach', minWidth: 150, title: '参考图纸', align: 'center',exportable:true ,rowspan:'2',
                    templet: function (d) {
                        if (download == '0') {
                            return "<a href=\"#\" class=\"layui-table-link\" onclick=\"downloadAttach('" + d.attach + "')\">下载</a>";
                        }
                        return "";
                    }
                }
                , {field: 'create_time', minWidth: 150, title: '创建时间', align: 'center',exportable:true ,rowspan:'2'}
            ],
            ]
            ,exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '标准方案管理',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '标准方案管理',
                where: function (){
                    console.log(_serializeJson($("#searchForm")));//当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        table.on('toolbar(test)',function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'add') {
                layer.open({
                    type: 2,
                    title: '添加方案',
                    content: '${ctx}/planManage/toAdd?type=1',
                    area: ['50%','70%'],
                    success: function(layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                });
            } else if (obj.event === 'edit') {
                if(checkStatus.data.length < 1){
                    layer.msg("请先选择数据",{icon:5,anim:6});
                    return;
                }else if(checkStatus.data.length > 1){
                    layer.msg("只能选择一条数据",{icon:5,anim:6});
                    return;
                }
                layer.open({
                    type: 2,
                    title: '编辑方案',
                    content: '${ctx}/planManage/toAdd?type=1&&id=' + checkStatus.data[0].id,
                    area: ['50%','70%'],
                    success: function(layero, index) {
                        childWin = window[layero.find('iframe')[0]['name']];
                    }
                });
            } else if (obj.event === 'delete') {
                var data = checkStatus.data;
                if (data.length < 1) {
                    layer.msg("请先选择数据",{icon:5,anim:6});
                    return;
                }
                var ids="";
                for(var i=0;i<data.length;i++){
                    ids+=data[i].id+",";
                }
                layer.confirm('您确定要删除方案吗？',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                deletePlan(ids);
                                //关闭弹窗
                                layer.close(index);
                                console.log("回车确认时事件")
                                //取消连续回车键，导致不断弹出询问框
                                $(':focus').blur();
                            }
                        };
                        //监听键盘事件
                        $(document).on('keydown', this.enterEsc);
                        //esc键触发取消
                        this.escQuit = (ev) => {
                            let code_val = ev.which;
                            if(code_val == 0x1B){
                                $('.layui-layer-btn1').click();
                                return false;
                            }
                        };
                        $(document).on('keydown', this.escQuit);
                    },
                    end: function () {
                        //取消键盘监听事件
                        $(document).off('keydown', this.enterEsc);
                        $(document).off('keydown', this.escQuit);
                    },
                    yes: function () {
                        //确认执行的代码
                        deletePlan(ids);
                    },
                    no: function () {

                    }
                });
            }
        });

        //监听事件
        table.on('tool(test)', function (obj) {

        });
        $('input').on('keydown', function(event){
            if(event.keyCode == 13){
                //执行相关操作
                $("#searchBtn").click();
            }
        });

        function isBlank(val) {
            if (val == null || val == '' || val == undefined) {
                return true;
            }
            return false;
        }
        $('#searchBtn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        var active = {
            reload: function(){
                reload();
            }
        };

        $("#reset").on('click', function () {
            $("#searchForm").find("select").val("");
            $("#searchForm").find("input").val("");
        })

    });

    function reload(){
        table.reload('test', {
            page:{
                curr:1
            },
            where: {
                name: $('#name').val(),
                code:$('#code').val(),
                planTypeId:$("#planTypeId").val(),
                createTimeSta: $("#startTime").val(),
                createTimeEnd: $("#endTime").val()
            }
        });
    }

    window.importMaterial = function(materials){
        childWin.importMaterial(materials);
    }

    function deletePlan(ids){
        $.ajax({
            type: "POST",
            url: "${ctx}/planManage/delete",
            data: {
                ids: ids
            },
            success: function (result) {
                if(result.code=='200'){
                    layer.msg('删除成功！', {icon: 1, time: 1000}, function () {
                        window.location.reload();
                    });
                }else {
                    layer.msg('删除失败!', {icon: 2});
                }
            },
            error:{
            }
        })
    }


    function  denglsj(id,userId) {
        console.log("siteInfo" + id +" userId : "+ userId)
        parent.denglusdj(id,userId);
    }

    /*方案详情*/
    function siteDetail(id) {
        layer.open({
            type: 2,
            title: '查看',
            content: '${ctx}/planManage/toAdd?type=1&mode=view&id=' + id,
            area: ['80%','80%']
        });
    }
    
    function downloadAttach(attach){
        var name = attach.substring(0, attach.lastIndexOf(".") - 1);
        var suffix = attach.substring(attach.lastIndexOf("."));
        attach=decodeURI(attach);
        attach = attach.replace(/\+/g,"%2B"); //
        window.open('${commonStaticImgPath}'+attach)
       // location.href = '${ctx}/technical/imgDownload?path=' + '${commonStaticImgPath}' + attach + "&fileGS=" + suffix + "&name=" + name;
    }

    /*方案详情*/
    function employeList(id) {
        window.location.href = '${ctx}/employeInformation/employeList?id='+id;
    }

</script>
</body>
</html>
