<%@ 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">
</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="XMGL_NHHT_T_${partner}" html='<li class="layui-this"><a href="${ctx}/customerContract/customerContractList/${partner}">农户合同</a></li>'/>
                <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="powerStationCode" class="layui-input" id="powerStationCode" >
                        </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="customerName" class="layui-input" id="customerName" >
                        </div>
                    </div>
                    <c:if test="${user.userType ne '6' && user.userType ne '7'}">
                        <div class="layui-col-lg2 layui-col-sm3" style="width: 33.3%">
                            <label class="layui-form-label">代理商名称：</label>
                            <div class="layui-input-block">
                                <select id="traderId" name="traderId" lay-search>
                                    <option value="">请选择</option>
                                    <c:forEach items="${traderList}" var="trader">
                                        <option value="${trader.columns.id}">${trader.columns.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>

                    </c:if>
                    <c:if test="${'2' ne ifTop}">
                        <div class="layui-col-lg2 layui-col-sm3" style="width: 33.3%">
                            <label class="layui-form-label">二级代理商：</label>
                            <div class="layui-input-block">
                                <select id = "twoTrader" name="twoTrader" lay-search>
                                    <option value="">请选择</option>
                                    <c:forEach items="${twoTrader}" var="trader">
                                        <option value="${trader.columns.id}">${trader.columns.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                    </c:if>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">产品名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="projectName" id="projectName" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">产品编码：</label>
                        <div class="layui-input-block" >
                            <select name="projectCode" id="projectCode" lay-search>
                                <option value="">请选择</option>
                                <c:forEach items="${projectList}" var="project">
                                    <option value="${project.columns.id}">${project.columns.code}</option>
                                </c:forEach>
                            </select>
                        </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="number" class="layui-input" id="number" >
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm3">
                        <label class="layui-form-label">合同状态：</label>
                        <div class="layui-input-block">
                            <select id="status" 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>
                            </select>
                        </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="idCode" class="layui-input" id="idCode" >
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">联系方式：</label>
                        <div class="layui-input-block">
                            <input type="text" name="mobile" id="mobile" class="layui-input" >
                        </div>
                    </div>
                    <div class="layui-col-lg4 layui-col-sm4 layui-col-md4" style="margin-top: 5px" >
                        <label class="layui-form-label">安装地址：</label>
                        <div class="inline-block" style="width: 18%">
                            <select name="province" lay-search lay-filter="province"  id="province">
                                <option value="">省</option>
                                <c:forEach items="${province}" var="trader">
                                    <option value="${trader.columns.ProvinceName}"  title="${trader.columns.ProvinceID}">${trader.columns.ProvinceName}</option>
                                </c:forEach>
                            </select>

                        </div>
                        <div class="inline-block" style="width: 18%;">
                            <select name="city" id="city" lay-filter="city"
                                    lay-search>
                                <option value="">市</option>

                            </select>
                        </div>
                        <div class="inline-block" style="width: 18%">
                            <select name="area" id="area" lay-search>
                                <option value="">县/区</option>

                            </select>
                        </div>
                        <div class="inline-block" style="width: 20%">
                            <input type="text" id="address" name="address" class="layui-input">

                        </div>
                    </div>
                </div>
            </form>
        </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="XMGL_NHHT_DC_B_${partner}" html='<button class="layui-btn layui-btn-sm" lay-event="export" id="exportBtn"><i class="layui-icon layui-icon-export"></i>导出列表</button>'/>
                </div>
            </script>
        </div>
    </div>
</div>
<%--<div id="toolbarDemo" class="layui-form-item layui-hide">--%>
    <%--<sfTags:pagePermission authFlag="HTGL_HTGL_NHHT_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'], function(){
        $ = layui.$;
        tableExt = layui.tableExt;
        layer = layui.layer;
        laypage = layui.laypage;
        laydate = layui.laydate;
        form = layui.form;
        table = tableExt.getLayTable();

        var downloadBtn = '${fns:checkPagePermission(request, downloadBtnPermission)}';

        //方案信息列表
        tableExt.render({
            elem: '#test'
            , height: 'full-180'
            , id: 'test'
            , defaultToolbar: false
            , url: '${ctx}/customerContract/ContractListDate?partner='+'${partner}'
            , 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', fixed: 'left'}
                , {field: 'id', minWidth: '4%', checkbox: true, align: 'center', fixed: 'left'}
                , {field: 'sign', title: '操作', minWidth: 120, align: 'center', fixed: 'left'
                    , templet: function (d) {
                        var html = "";
                        if (downloadBtn == "0" && d.tenant_type == '1') {
                            if (d.status == '2' || d.status == '3' || d.status == '5') {
                                html += '<a href="#" class="layui-table-link" onclick="contractDetail(\'' + d.id + '\')">下载</a>&nbsp;';
                            }
                            else {
                                html += '<a href="#" class="layui-table-link" onclick="viewContractDetail(\'' + d.id + '\',\'00\')">签约链接</a>&nbsp;';
                                if (d.if_click_sign == 1 && d.age >= 65) {
                                    html += '<a href="#" class="layui-table-link" onclick="viewContractDetail(\'' + d.id + '\',\'01\')">共签链接</a>&nbsp;';
                                }
                            }

                        } else {
                            html += '--';
                        }
                        return html;
                    }
                }
                , {field: 'contractNumber', title: '合同编码', minWidth: 200, align: 'center',exportable:true , fixed: 'left'}
                , {field: 'status', title: '合同状态', minWidth: 150, 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 "已审核";
                        }
                        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 "已审核";
                        }
                        return "--";
                    }
                    }
                , {field: 'powerStationCode', title: '电站编码', minWidth: 150, align: 'center',exportable:true}
               /* , {field: 'entryStatus', title: '电站状态', minWidth: 150, 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 "已审核";
                        }
                        return "--";
                    }
                }*/
                <c:if test="${user.userType ne '6' && user.userType ne '7'}">
                , {
                    field: 'traderName',
                    title: '代理商名称',
                    minWidth: '280',
                    sort: false,
                    align: 'center',
                    exportable: true
                }
                </c:if>
                <c:if test="${'2' ne ifTop}">
                , {field: 'createTraderName',title: '二级代理商',minWidth: '250',sort: false,align: 'center',exportable: true
                    ,templet: function (d) {
                        if(d.distributor_id==d.create_trader_id){
                            return "";
                        }else{
                            return d.createTraderName
                        }
                    },
                    expFormat: function (d) {
                        if(d.distributor_id==d.create_trader_id){
                            return "";
                        }else{
                            return d.createTraderName
                        }
                    }
                }
                </c:if>
                , {field: 'projectCode', title: '产品编号', minWidth: 150, align: 'center', exportable: true}
                , {field: 'projectName', title: '产品名称', minWidth: 200, align: 'center', exportable: true}
                , {field: 'planName', title: '方案名称', minWidth: 200, align: 'center', exportable: true}
               /* , {field: 'companyName', title: '所属项目公司', minWidth: 150, align: 'center', exportable: true}*/
                , {field: 'tenantName', title: '用户姓名', minWidth: 200, align: 'center', exportable: true}
                , {field: 'idCode', title: '身份证号', minWidth: 200, align: 'center', exportable: true}
                , {field: 'tenantMobile', title: '联系方式', minWidth: 200, align: 'center', exportable: true}
                , {field: 'detailAddress', title: '安装地址', minWidth: 300, align: 'center', exportable: true}
                , {field: 'tenantSignTime', title: '农户签约时间', minWidth: 150, align: 'center', exportable: true}
               /* , {field: 'roofType', title: '屋顶类型', minWidth: 200, align: 'center', exportable: true}*/
            ],
            ]
            , exportOpt: {                           //导出选项配置
                ele: '#exportBtn',                   //导出的Dom元素
                headerTitle: '农户合同',             //excel表格的标题
                pageData: false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
                maxAmount: 5000,                    //当pageData为false生效
                fileName: '农户合同',
                where: function (){                 //当pageData为false时生效，过滤的条件
                    return _serializeJson($("#searchForm"));
                }
            }
        });

        table.on('toolbar(test)',function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            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 ) {
                                //确认  执行的代码
                                deleteContract(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 () {
                        //确认执行的代码
                        deleteContract(ids);
                    },
                    no: function () {

                    }
                });
            }
        });


        form.on('select(province)', function (data) {
            var province = $(data.elem).find("option:selected").attr("title");
            provinceOrder(province);
        });
        form.on('select(city)', function (data) {
            var city = $(data.elem).find("option:selected").attr("title");
            cityOrder(city);

        });

        function provinceOrder(province) {
            $("#city").empty();
            $("#area").empty();
            if (isBlank(province)) {
                $("#city").append("<option value=''>市</option>");
                return "";
            }
            $.ajax({
                type: "POST",
                url: "${ctx}/main/redirect/getCitysByProId",
                data: {
                    province: province
                },
                success: function (result) {

                    if (result == "205") {
                        layer.msg("暂无数据!")
                        return
                    }
                    var html = " <option value=''>市</option>";
                    for (var i = 0; i < result.length; i++) {
                        var city = result[i];
                        html = html + '<option value="' + city.columns.CityName + '" title="' + city.columns.CityID + '">' + city.columns.CityName + '</option>';
                    }
                    $("#city").append(html);
                    form.render('select');
                }
            });

        }

        function cityOrder(city, arean) {
            $("#area").empty()
            if (isBlank(city)) {
                $("#area").append("<option value=''>区/县</option>");
                return "";
            }
            $.ajax({
                type: "POST",
                url: "${ctx}/main/redirect/getAreaByCityId",
                data: {
                    city: city
                },
                success: function (result) {
                    if (result == "205") {
                        layer.msg("暂无数据!")
                        return
                    }
                    var html = "<option value=''>区/县</option>";
                    for (var i = 0; i < result.length; i++) {
                        var area = result[i];
                        html = html + '<option value="' + area.columns.DistrictName + '" value="' + area.columns.DistrictId + '">' + area.columns.DistrictName + '</option>';
                    }
                    $("#area").append(html);
                    if (!isBlank(arean)) {
                        $("#area").val(arean)
                    }

                    form.render('select');
                }
            });

        }


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

        });

        document.addEventListener('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();
            }
        };
    });

    function reload(){
        table.reload('test', {
            where: {
                number: $('#number').val().trim(),
                customerName:$("#customerName").val().trim(),
                idCode:$("#idCode").val().trim(),
                status:$("#status").val().trim(),
                powerStationCode:$("#powerStationCode").val().trim(),
                traderId:$("#traderId").val(),
                twoTrader:$("#twoTrader").val(),
                province:$("#province").val(),
                city:$("#city").val(),
                area:$("#area").val(),
                address:$("#address").val(),
                projectName:$("#projectName").val(),
                projectCode:$("#projectCode").val(),
                mobile:$("#mobile").val(),



            }
        });
    }

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

    function platformSign(id) {
        $.ajax({
            type: 'post',
            url: '${ctx}/customerEntry/platformContracting',
            data: {
                id: id
            },
            success: function (data) {
                if ('200' == data.code) {
                    let url = data.data.url;
                    window.open(url);
                } else {
                    layer.msg(data.errMsg, {icon: 2});
                }
            },
        });
    }

    function contractDetail(id) {
        var index= layer.load(2, {shade: [0.5,'#fff'], content:''});
        $.ajax({
            type: 'post',
            url: '${ctx}/main/yueXiuContract/getContractUrlToLook',
            data: {
                entryId: id
            },
            success: function (data) {
                if ('200' == data.code) {
                    let url = data.data.url;
                    console.log(url);
                    window.open('${commonStaticImgPath}'+url);
                } else {
                    layer.msg(data.errMsg, {icon: 2});
                }
            },
            complete: function () {
            layer.close(index);
        }
        });
    }

    function viewContractDetail(id,type) {
        var index= layer.load(2, {shade: [0.5,'#fff'], content:''});
        $.ajax({
            type: 'post',
            url: '${ctx}/main/yueXiuContract/getContractUrlForPC',
            data: {
                entryId: id,
                type: type
            },
            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 deleteContract(ids){
        $.ajax({
            type: "POST",
            url: "${ctx}/customerContract/deleteContract",
            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:{
            }
        })
    }
</script>
</body>
</html>

