<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <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">
    <style>
        .layui-table-link{padding: 0px 6px}
    </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="JXSGL_JXSGL_JXSGL_T" html='<li><a href="${ctx}/trader/index">代理商管理</a></li>'/>
                <sfTags:pagePermission authFlag="KHGL_DLSGL_HTGL_T" html='<li class="layui-this"><a href="${ctx}/trader/contract/listPage">合同管理</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" id="searchBtn" lay-submit
                        lay-filter="LAY-user-back-search"><i class="layui-icon fs-12">&#xe615;</i>查询
                </button>
            </ul>
        </div>

        <div class="layui-card-body layuiadmin-card-header-auto" id="searchForm">
            <div class="layui-form-item">
                <div class="layui-col-md3 layui-col-sm3">
                    <label class="layui-form-label">代理商名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="traderName" class="layui-input" id="traderName">
                    </div>
                </div>

                <div class="layui-col-md3 layui-col-sm3">
                    <label class="layui-form-label">合同状态：</label>
                    <div class="layui-input-block">
                        <select name="status">
                            <option value="">请选择</option>
                            <option value="1">暂存</option>
                            <option value="2">代理商待签约</option>
                            <option value="3">能辉待签约</option>
                            <option value="4">生效中</option>
                            <option value="5">已过期</option>
                            <option value="6">已作废</option>
                        </select>
                    </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="KHGL_DLSGL_HTGL_CJHT_B" html='<button class="layui-btn layui-btn-sm" id="addContract" lay-event="addContract"><i class="layui-icon layui-icon-add-1"></i>创建合同</button>'/>
                    <sfTags:pagePermission authFlag="KHGL_DLSGL_HTGL_EXPORT_B" html='<button class="layui-btn layui-btn-sm" id="exportBtn" lay-event="exports"><i class="layui-icon layui-icon-export"></i>导出</button>'/>
                </div>
            </script>
        </div>
    </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;
    var laydate;
    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;
        laydate = layui.laydate;
        table = layui.table;
        //代理商信息列表
        var editBtn = '${fns:checkPagePermission(request, "KHGL_DLSGL_HTGL_BJ_B")}';  //编辑
        var urlGetBtn = '${fns:checkPagePermission(request, "KHGL_DLSGL_HTGL_URL_B")}'; //获取签约链接
        var cancelBtn='${fns:checkPagePermission(request, "KHGL_DLSGL_HTGL_CANCEL_B")}'; //作废
        var downLoadBtn='${fns:checkPagePermission(request, "KHGL_DLSGL_HTGL_DOWN_B")}'; //下载
        var deleteBtn='${fns:checkPagePermission(request, "KHGL_DLSGL_HTGL_DELETE_B")}'; //删除

        tableExt.render({
            elem: '#test'
            , height: 'full-145'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/trader/contract/listDate'
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , page: true
            , toolbar:"#toolbarDemo"
            , method:"post"
            , limits: [20, 50, 100]
            , limit: 20
            , cols: [[
                {field: 'number', title: '序号', align: 'center', type: 'numbers'}
                ,{field : 'id', title: '操作',align : 'center',templet:function(d){
                        var html = "";
                        if(editBtn=='0' && d.status=='1'){
                            html += "<a href=\"#\" class=\"layui-table-link\" onclick=\"editContract('"+d.id+"')\">编辑</a>";
                        }
                        if(d.status=='2' || d.status=='3'){
                            if(urlGetBtn=='0'){
                                html += "<a href=\"#\" class=\"layui-table-link\" onclick=\"getSignUrl('"+d.id+"')\">签约链接</a>";
                            }
                            if(cancelBtn=='0'){
                                html += "<a href=\"#\" class=\"layui-table-link\" onclick=\"cancelContract('"+d.id+"')\">作废</a>";
                            }
                        }
                        if(cancelBtn=='0' && d.status=='4'){
                            html += "<a href=\"#\" class=\"layui-table-link\" onclick=\"cancelContract('"+d.id+"')\">作废</a>";
                        }
                        if(downLoadBtn=='0' && (d.status=='4' || d.status=='5')){
                            html += '<a href="#" class="layui-table-link" onclick="downLoadContract(\'' + d.id + '\',\'' + d.contract_signed_path + '\')">下载</a>';
                        }
                        if(deleteBtn=='0' && d.status =='6'){
                            html += "<a href=\"#\" class=\"layui-table-link\" onclick=\"deleteContract('"+d.id+"')\">删除</a>";
                        }
                        return html;
                    }}

                , {field: 'traderName', title: '代理商名称',  align: 'center',exportable:true}
                , {field: 'status', title: '合同状态', align: 'center',exportable:true,
                    templet: function (d) {
                        if (d.status === '1') {
                            return '暂存';
                        }else if(d.status === '2'){
                            return '代理商待签约';
                        }else if(d.status === '3'){
                            return '能辉待签约';
                        }else if(d.status === '4'){
                            return '生效中';
                        }else if(d.status === '5'){
                            return '已过期';
                        }else if(d.status === '6'){
                            return '已作废';
                        } else {
                            return '--';
                        }
                    }, expFormat: function (d) {
                        if (d.status === '1') {
                            return '暂存';
                        }else if(d.status === '2'){
                            return '代理商待签约';
                        }else if(d.status === '3'){
                            return '能辉待签约';
                        }else if(d.status === '4'){
                            return '生效中';
                        }else if(d.status === '5'){
                            return '已过期';
                        }else if(d.status === '6'){
                            return '已作废';
                        } else {
                            return '--';
                        }
                    }
                }
                ,{field: 'contract_number', title: '合同编号', align: 'center',exportable:true }
                , {field: 'start_time', title: '合同起始时间',align: 'center',exportable:true }
                , {field: 'end_time', title: '合同终止时间', align: 'center',exportable:true }
                , {field: 'sign_finish_time',title: '签约完成时间', align: 'center',exportable:true}
                , {field: 'create_name', title: '发起人', align: 'center',exportable:true}
            ],
            ]
            ,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"));
                }
            }
        });
        //开始日期
        var createTimeMin = laydate.render({
            elem: '#createTimeMin'
            , type: 'date'
            , done: function (value, date) {
                //更新结束日期的最小日期
                createTimeMax.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
                //自动弹出结束日期的选择器
                createTimeMax.config.elem[0].focus();
            }
        });
        //结束日期
        var createTimeMax = laydate.render({
            elem: '#createTimeMax'
            , type: 'date'
            , done: function (value, date) {
                //更新开始日期的最大日期
                createTimeMin.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });


        table.on('toolbar(test)',function(obj) {
            if (obj.event === 'addContract') {
                layer.open({
                    title:'新增',
                    type: 2,
                    area: ["60%", "80%"],
                    content: '${ctx}/trader/contract/addContractPage'
                })
            }
        });

        //监听事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'add') {//添加地址


            }
        });

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

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

    });

    function reload(){
        table.reload('test', {
            where: {
                name: $('#name').val().trim(),
                province:$("#province").val().trim(),
                telephone:$("#telephone").val().trim()
            }
        });
    }



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

    /*编辑*/
    function editContract(id) {
        layer.open({
            type: 2,
            title:"编辑",
            type: 2,
            area: ["60%", "80%"],
            content: '${ctx}/trader/contract/addContractPage?id='+id
        });
    }

    //签约链接
    function getSignUrl(id){
        var index = layer.load(2, {shade: [0.5, '#fff'], content: ''});
        $.ajax({
            type: 'post',
            url: '${ctx}/trader/contract/getContractSignUrl',
            data: {
                id: id,
            },
            success: function (data) {
                if ('200' == data.code) {
                    let url = data.data.contractUrl;
                    layer.alert(url);
                } else {
                    layer.msg(data.errMsg, {icon: 2});
                }
            },
            complete: function () {
                layer.close(index);
            }
        });
    }


    function reset(){
        layui.form.render();
        $("#searchForm").find("select").val("");
        $("#searchForm").find("input").val("");
    }
    //作废
    function cancelContract(id) {
        let content = "您确定作废该合同吗？";
        var indexla = layer.confirm(content, {
            icon: 3,
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: "POST",
                url: "${ctx}/trader/contract/cancelContract",
                data: {
                    id: id,
                },
                success: function (result) {
                    if (result.code == "200") {
                        layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg('操作失败！', {icon: 2})
                    }
                }
            });
        }, function () {
            layer.close(indexla)
        });
    }

    //下载
    function downLoadContract(id,signedPath){
          if(!isBlank(signedPath)){
              window.open('${commonStaticImgPath}' + signedPath);
          }
        $.ajax({
            type: 'post',
            url: '${ctx}/trader/contract/downLoadContract',
            data: {
                id: id,
            },
            success: function (data) {
                if ('200' == data.code) {
                    let url = data.data.downLoadUrl;
                    console.log('${commonStaticImgPath}' + url);
                    window.open('${commonStaticImgPath}' + url);
                } else {
                    layer.msg(data.errMsg, {icon: 2});
                }
            }
        });
    }

    //删除
    function deleteContract(id){
        let content = "您确定删除该合同吗？";
        var indexla = layer.confirm(content, {
            icon: 3,
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: "POST",
                url: "${ctx}/trader/contract/deleteContract",
                data: {
                    id: id,
                },
                success: function (result) {
                    if (result.code == "200") {
                        layer.msg('操作成功！', {icon: 1, time: 1000}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg('操作失败！', {icon: 2})
                    }
                }
            });
        }, function () {
            layer.close(indexla)
        });
    }

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