
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
    <title>能辉</title>
    <meta name="decorator" content="base" />
    <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
    <style>
        .layui-card{box-shadow: none;}
        .layui-btn + .layui-btn {
            margin-left: 35px;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-form layui-card">
        <div class="layui-tab  layui-tab-brief">
            <ul class="layui-tab-title">
                <sfTags:pagePermission authFlag="HTMBGL_HTMOBL_HTMBGL_T" html='<li class="layui-this sf-tab"><a href="${ctx}/contractTemplate/templateSetListPage">合同模板管理</a></li>'/>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset"  onclick="reset()" >
                    <i class="layui-icon fs-12">&#xe9aa;</i>
                    重置
                </button>
                <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " lay-submit lay-filter="LAY-user-back-search" data-type="reload">
                    <i class="layui-icon fs-12">&#xe615;</i>
                    查询
                </button>
            </ul>
        </div>
        <div class="layui-card-body" id="searchForm">
            <div class="layui-form-item">

               <%-- <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">代理商：</label>
                    <div class="layui-input-block">
                        <select name="traderId" id="traderId" lay-search>
                            <option value="">请选择</option>
                            <c:forEach var="trader" items="${traders}">
                                <option value="${trader.columns.id}" data-value="${trader.columns.contacts}" data-anim="${trader.columns.mobile}"
                                        data-type="${trader.columns.traderAddress}" <c:if test="${apply.columns.trader_id eq trader.columns.id}"> selected</c:if>>${trader.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-xs5">
                            <input type="text" name="createTimeSta" id="createTimeSta" class="layui-input"  placeholder="开始日期">
                        </div>
                        <div class="layui-col-xs2 text-c lh-30">
                            —
                        </div>
                        <div class="layui-col-xs5">
                            <input type="text" name="createTimeEnd" id="createTimeEnd" class="layui-input"  placeholder="结束日期">
                        </div>
                    </div>
                </div>

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

    <div class='layui-card'>
        <div class="layui-card-body">
            <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                  <%--  <sfTags:pagePermission authFlag="HTMBGL_HTMOBL_HTMBGL_ADD_B" html='<button class="layui-btn layui-btn-sm" id="add" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增签约合同</button>'/>--%>
                    <sfTags:pagePermission authFlag="HTMBGL_HTMOBL_XZNHHT_B" html='<button class="layui-btn layui-btn-sm" id="farmerAdd" lay-event="farmerAdd"><i class="layui-icon layui-icon-add-1"></i>新增农户合同</button>'/>
                    <sfTags:pagePermission authFlag="HTMBGL_HTMOBL_HTMBGL_ADD_B" html=' <button class="layui-btn layui-btn-sm" id="add" lay-event="addFarmerAuth"><i class="layui-icon layui-icon-add-1"></i>新增农户授权合同</button>'/>
                </div>
            </script>
        </div>
    </div>

</div>
<div class="hide pd-10" style="padding: 10px" id="positionSetDiv">
    <div class="layui-col-lg6 layui-col-sm6">
        <label class="layui-form-label">页码：</label>
        <div class="layui-input-block">
            <input type="text" name="positionPage" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
        </div>
    </div>
    <div class="layui-col-lg12 layui-col-sm12"></div>

    <div class="layui-col-lg6 layui-col-sm6">
        <label class="layui-form-label">X坐标：</label>
        <div class="layui-input-block">
            <input type="text" name="positionAcross" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
        </div>
    </div>
    <div class="layui-col-lg6 layui-col-sm6">
        <label class="layui-form-label">Y坐标：</label>
        <div class="layui-input-block">
            <input type="text" name="positionVertical" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
        </div>
    </div>

</div>

<div class="hide pd-10" style="padding: 10px" id="positionSetDiv2">
    <div class="layui-card" style="height: 100%;padding: 20px;">
        <div class="layui-card-body">
            <p>合同签字坐标</p>
            <div class="layui-col-lg6 layui-col-sm6">
                <label class="layui-form-label">页码：</label>
                <div class="layui-input-block">
                    <input type="text" name="positionPage" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
                </div>
            </div>
            <div class="layui-col-lg12 layui-col-sm12"></div>

            <div class="layui-col-lg6 layui-col-sm6">
                <label class="layui-form-label">X坐标：</label>
                <div class="layui-input-block">
                    <input type="text" name="positionAcross" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
                </div>
            </div>
            <div class="layui-col-lg6 layui-col-sm6">
                <label class="layui-form-label">Y坐标：</label>
                <div class="layui-input-block">
                    <input type="text" name="positionVertical" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
                </div>
            </div>
        </div>
    </div>
    <div class="layui-card" style="height: 100%;padding: 20px;">
        <div class="layui-card-body">
            <p>承诺人签字坐标</p>
            <div class="layui-col-lg6 layui-col-sm6">
                <label class="layui-form-label">页码：</label>
                <div class="layui-input-block">
                    <input type="text" name="positionPage2" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
                </div>
            </div>
            <div class="layui-col-lg12 layui-col-sm12"></div>

            <div class="layui-col-lg6 layui-col-sm6">
                <label class="layui-form-label">X坐标：</label>
                <div class="layui-input-block">
                    <input type="text" name="positionAcross2" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
                </div>
            </div>
            <div class="layui-col-lg6 layui-col-sm6">
                <label class="layui-form-label">Y坐标：</label>
                <div class="layui-input-block">
                    <input type="text" name="positionVertical2" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
                </div>
            </div>
        </div>

    </div>

</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/html" id="table-toolbar">
</script>

<script type="text/javascript">
    var table;
    var $;

    var deleteBtn = '${fns:checkPagePermission(request, "HTMBGL_HTMOBL_HTMBGL_DELETE_B")}';
    var editBtn = '${fns:checkPagePermission(request, "HTMBGL_HTMOBL_HTMBGL_EDIT_B")}';
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laydate','form'], function(){
        $ = layui.jquery;
        tableExt = layui.tableExt;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var  form = layui.form;
        table = tableExt.getLayTable();
        //开始日期
        var insStart = laydate.render({
            elem: '#createTimeSta'
            ,type: 'date'
            ,done: function(value, date){
                //更新结束日期的最小日期
                insEnd.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                insEnd.config.elem[0].focus();
            }
        });
        //结束日期
        var insEnd = laydate.render({
            elem: '#createTimeEnd'
            ,type: 'date'
            ,done: function(value, date){
                //更新开始日期的最大日期
                insStart.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        tableExt.render({
            elem: '#test'
            ,toolbar : '#toolbarDemo'
            , height: 'full-160'
            ,defaultToolbar : false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            ,url: '${ctx}/contractTemplate/listData'
            ,cols: [[
                {type: 'numbers',title:'序号',fixed:'left',exportable:true}
                ,{field:'id', title: '操作',align: 'center',fixed:'left'
                    ,templet:function(d){
                        var htm="";
                        if(d.status == 0){
                            if(editBtn =='0'){
                                htm+= '<a class="layui-table-opt-link" lay-event="edit">编辑</a>';
                            }
                            if(deleteBtn =='0'){
                                htm+= '&nbsp;&nbsp;&nbsp;<a class="layui-table-opt-link" lay-event="delete">禁用</a>';
                            }
                            if(d.type=='3'){
                                htm+= '&nbsp;&nbsp;&nbsp;<a class="layui-table-opt-link" lay-event="positionSet">坐标设置</a>';
                            }
                            if(d.type=='2' && d.partner_id == '04'){
                                htm+= '&nbsp;&nbsp;&nbsp;<a class="layui-table-opt-link" lay-event="positionSet2">坐标设置</a>';
                            }
                            return htm;
                        }else{
                            return "--";
                        }
                    }

                }
                ,{field:'status', title: '状态',align: 'center',fixed:'left',exportable:true
                    ,templet:function(d){
                        if(d.status == 0){
                            return "正常";
                        } else if(d.status == 1){
                            return "禁用";
                        }
                        return "";
                        },
                    expFormat: function (d) {  //格式导出的化字段
                        if(d.status == 0){
                            return "正常";
                        }else if(d.status == 1){
                            return "禁用";
                        }
                        return "";
                    }
                }
                ,{field:'contract_name', title: '合同名称',align: 'center',fixed:'left',exportable:true}
                ,{field:'type', title: '合同类型',align: 'center',fixed:'left',exportable:true
                    ,templet:function(d){
                        if(d.type == 1){
                            return "签约合同";
                        } else if(d.type == 2){
                            return "农户合同";
                        }else if(d.type == 3){
                            return "农户授权合同";
                        }
                        return "";
                    },
                }
               /* ,{field:'companyName', title: '项目公司',align: 'center',fixed:'left',exportable:true}*/
                ,{field:'filing_type', title: '备案类型',align: 'center',fixed:'left',exportable:true
                    ,templet:function(d){
                        if(d.filing_type == 1){
                            return "农户备案";
                        } else if(d.filing_type == 2){
                            return "项目公司备案";
                        }
                        return "";
                    },
                }
                ,{field:'patern', title: '所属资方',minWidth: 240,align: 'center',fixed:'left',exportable:true}
                ,{field:'doc_template_id', title: '合同模板',align: 'center'
                    ,templet: function (d) {
                        return "<a href=\"#\" class=\"layui-table-link \" style='font-size:14px;' onclick=\"lookDetail('" + d.doc_template_id + "')\">查看</a>";
                 }}
                ,{field:'createTime', title: '创建时间',align: 'center',exportable:true}

            ]]
            ,done: function(res, curr, count){

            },exportOpt:{                           //导出选项配置
                ele:'#exportBtn',                   //导出的Dom元素
                headerTitle: '合同模板',             //excel表格的标题
                pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 1000,                    //当pageData为false生效
                fileName: '合同模板',
                where: function (){                 //当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            switch (obj.event){
                case 'add':
                    layer.open({
                        title:'新增合同模板',
                        type: 2,
                        area: ["60%", "65%"],
                        content: '${ctx}/contractTemplate/addTemplatePage?type=1'
                    })
                    break;
                case 'farmerAdd':
                    layer.open({
                        title:'新增农户合同模板',
                        type: 2,
                        area: ["60%", "65%"],
                        content: '${ctx}/contractTemplate/addTemplatePage?type=2'
                    })
                    break;
                case 'addFarmerAuth':
                    layer.open({
                        title:'新增农户授权合同模板',
                        type: 2,
                        area: ["60%", "65%"],
                        content: '${ctx}/contractTemplate/addTemplatePage?type=3'
                    })
                    break;
            }
        })

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var docTemplateId = data.doc_template_id;
            if(obj.event === 'edit'){
                $.ajax({
                    type: 'post',
                    url: '${ctx}/contractTemplate/editContractTemplate',
                    data: {
                        docTemplateId: docTemplateId
                    },
                    success: function (data) {
                        if('200' ==data.code){
                            var da=data.data;
                            layer.msg('请编辑合同模板中的内容！', {icon: 1, time: 2000}, function () {
                                window.open(da.docTemplateEditUrl);
                            });
                        }else{
                            layer.msg(data.errMsg, {icon: 2});
                        }
                    },
                });
            }else if(obj.event === 'delete'){
                var id=data.id;
                layer.confirm('确认禁用该模板？',{
                    btn: ['确定', '取消'],
                    success: function (layero, index) {
                        this.enterEsc = function (event) {
                            if (event.keyCode == 13 ) {
                                //确认  执行的代码
                                deleteContractTemplate(id);
                                //关闭弹窗
                                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 () {
                        //确认执行的代码
                        deleteContractTemplate(id);
                    },
                    no: function () {

                    }
                });
            }else if(obj.event === 'positionSet'){
                var id=data.id;
                $('input[name="positionPage"]').val(data.position_page);
                $('input[name="positionAcross"]').val(data.position_across);
                $('input[name="positionVertical"]').val(data.position_vertical);
                layer.open({
                    type: 1,
                    area: ['480px', '280px'],
                    content: $("#positionSetDiv"),
                    title: '设置',
                    btn: ['确认', '取消'],
                    btnAlign: 'c',
                    yes: function () {
                        var positionPage = $('input[name="positionPage"]').val();
                        var positionAcross = $('input[name="positionAcross"]').val();
                        var positionVertical = $('input[name="positionVertical"]').val();
                        if (isBlank(positionPage)) {
                            layer.msg("请输入页码");
                        }else if(isBlank(positionAcross)){
                            layer.msg("请输入X坐标");
                        } else if(isBlank(positionVertical)){
                            layer.msg("请输入Y坐标");
                        } else {
                            $.ajax({
                                type: 'post',
                                url: '${ctx}/contractTemplate/positionSet',
                                data: {
                                    id: id,
                                    positionPage: positionPage,
                                    positionAcross:positionAcross,
                                    positionVertical:positionVertical
                                },
                                success: function (result) {
                                    if (result.code === '200') {
                                        layer.msg('操作成功！', {time: 1000}, function(){
                                            layer.closeAll();
                                            $('.layui-laypage-btn').click();
                                        })
                                    } else {
                                        layer.msg('保存失败!'+result.errMsg)
                                    }
                                }
                            });
                        }
                    }
                })
            }else if(obj.event === 'positionSet2'){
                var id=data.id;
                $('#positionSetDiv2 input[name="positionPage"]').val(data.position_page);
                $('#positionSetDiv2 input[name="positionAcross"]').val(data.position_across);
                $('#positionSetDiv2 input[name="positionVertical"]').val(data.position_vertical);
                $('#positionSetDiv2 input[name="positionPage2"]').val(data.position_page2);
                $('#positionSetDiv2 input[name="positionAcross2"]').val(data.position_across2);
                $('#positionSetDiv2 input[name="positionVertical2"]').val(data.position_vertical2);
                layer.open({
                    type: 1,
                    area: ['680px', '400px'],
                    content: $("#positionSetDiv2"),
                    title: '设置',
                    btn: ['确认', '取消'],
                    btnAlign: 'c',
                    yes: function () {
                        var positionPage = $('#positionSetDiv2 input[name="positionPage"]').val();
                        var positionAcross = $('#positionSetDiv2 input[name="positionAcross"]').val();
                        var positionVertical = $('#positionSetDiv2 input[name="positionVertical"]').val();
                        var positionPage2 = $('#positionSetDiv2 input[name="positionPage2"]').val();
                        var positionAcross2 = $('#positionSetDiv2 input[name="positionAcross2"]').val();
                        var positionVertical2 = $('#positionSetDiv2 input[name="positionVertical2"]').val();
                        if (isBlank(positionPage)) {
                            layer.msg("请输入合同签字坐标页码");
                        }else if(isBlank(positionAcross)){
                            layer.msg("请输入合同签字X坐标");
                        } else if(isBlank(positionVertical)){
                            layer.msg("请输入合同签字Y坐标");
                        } else if(isBlank(positionPage2)){
                            layer.msg("请输入承诺人签字坐标页码");
                        } else if(isBlank(positionAcross2)){
                            layer.msg("请输入承诺人签字X坐标");
                        } else if(isBlank(positionVertical2)){
                            layer.msg("请输入承诺人签字Y坐标");
                        }else {
                            $.ajax({
                                type: 'post',
                                url: '${ctx}/contractTemplate/positionSet2',
                                data: {
                                    id: id,
                                    positionPage: positionPage,
                                    positionAcross:positionAcross,
                                    positionVertical:positionVertical,
                                    positionPage2: positionPage2,
                                    positionAcross2:positionAcross2,
                                    positionVertical2:positionVertical2
                                },
                                success: function (result) {
                                    if (result.code === '200') {
                                        layer.msg('操作成功！', {time: 1000}, function(){
                                            layer.closeAll();
                                            $('.layui-laypage-btn').click();
                                        })
                                    } else {
                                        layer.msg('保存失败!'+result.errMsg)
                                    }
                                }
                            });
                        }
                    }
                })
            }
        });

        //监听搜索
        form.on('submit(LAY-user-back-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('test', {
                where: field,
                page:{
                    curr:1
                }
            });
        });



    });

    function deleteContractTemplate(id){
        $.post('${ctx}/contractTemplate/deleteContractTemplate', {id:id}, function (result) {
            if (result.code === '200') {
                layer.msg('操作成功', {time: 2000}, function () {
                    reload();
                });
            }else {
                layer.msg('操作失败！' + result.errMsg);
            }
        });
    }


    function lookDetail(id){
        $.ajax({
            type: 'post',
            url: '${ctx}/contractTemplate/lookContractTemplateDetail',
            data: {
                docTemplateId: id
            },
            success: function (data) {
                if('200' ==data.code){
                    var docTemplateEditUrl=data.data;
                    window.open(docTemplateEditUrl);
                }else{
                    layer.msg(data.errMsg, {icon: 2});
                }
            },
        });
    }

    function reload(){
        table.reload('test', {
            page:{
                curr:1
            }
        });
    }

    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
    }

    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>

