<%@ 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" type="text/css" href="${ctxPlugin}/static/css/reset.css" />
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/style/formSelects-v4.css" media="all">
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/lib/font/iconfont.css" />
</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="BDGL_XDJL_T" html='<a href="${ctx}/customerEntry/insurance/renewalList"><li>续保记录</li></a>'/>
                <sfTags:pagePermission authFlag="BDGL_NOXDJL_T" html='<a href="${ctx}/customerEntry/insurance/renewalListNoSystem"><li class="layui-this">非系统电站续保记录</li></a>'/>
                <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" >
            <form class="layui-form layui-card" action="" 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="insuranceCompanyName" lay-search>
                                <option value="">请选择</option>
                                <option value="0">深圳市慧择保险记录有限公司</option>
                                <option value="1">其他</option>
                            </select>
                        </div>
                    </div>
                    <c:if test="${user.userType ne '6' && user.userType ne '7'}">
                        <div class="layui-col-lg2 layui-col-sm3">
                            <label class="layui-form-label">代理商名称：</label>
                            <div class="layui-input-block" >
                                <select name="traderName" lay-search>
                                    <option value="">请选择</option>
                                    <c:forEach items="${traderList}" var="trader">
                                        <option value="${trader.columns.name}">${trader.columns.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                    </c:if>
                    <div class="layui-col-lg4 layui-col-sm6 layui-col-md6" >
                        <label class="layui-form-label">项目区域：</label>
                        <div class="layui-input-block">
                            <input type="text" name="address" class="layui-input">
                        </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="powerStationCode" class="layui-input" >
                        </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="tenantName" class="layui-input" >
                        </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="startTimeMin" class="layui-input" id="startTimeMin" placeholder="开始日期">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="startTimeMax" class="layui-input" id="startTimeMax" placeholder="结束日期">
                            </div>
                        </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="endTimeMin" class="layui-input" id="endTimeMin" placeholder="开始日期">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="endTimeMax" class="layui-input" id="endTimeMax" placeholder="结束日期">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">资方名称：</label>
                        <div class="layui-input-block">
                            <select name="partnerName" id="partnerName" lay-search>
                                <option value="">请选择</option>
                                <c:forEach items="${paterns}" var="data">
                                    <option value="${data.columns.name}">${data.columns.name}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">电站拆除：</label>
                        <div class="layui-input-block">
                            <select name="removeSign" id="removeSign" lay-search>
                                <option value="">请选择</option>
                                <option value="0">未拆除</option>
                                <option value="1">已拆除</option>
                            </select>
                        </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="BDGL_XDJL_BJCC_B" html='<button class="layui-btn layui-btn-sm" lay-event="remove" onclick="signOperate(1)"><i class="layui-icon "></i>标记拆除</button>'/>
                    <sfTags:pagePermission authFlag="BDGL_XDJL_QXBJ_B" html='<button class="layui-btn layui-btn-sm" lay-event="cancelRemove" onclick="signOperate(0)"><i class="layui-icon"></i>取消标记</button>'/>
                    <sfTags:pagePermission authFlag="BDGL_XDJL_DR_B" html='<button class="layui-btn layui-btn-sm" lay-event="import" id="importBtn" onclick="importData()"><i class="layui-icon layui-icon-download-circle"></i>导入</button>'/>
                </div>
            </script>
        </div>
    </div>
</div>
<a class="hide" id="batchDownLoad" href=""/>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/lib/formSelects-v4.js"></script>
<script>
    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 startTimeMin = laydate.render({
            elem: '#startTimeMin'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新结束日期的最小日期
                startTimeMax.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //结束日期
        var startTimeMax = laydate.render({
            elem: '#startTimeMax'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新开始日期的最大日期
                startTimeMin.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        var endTimeMin = laydate.render({
            elem: '#endTimeMin'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新结束日期的最小日期
                endTimeMax.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //结束日期
        var endTimeMax = laydate.render({
            elem: '#endTimeMax'
            ,done: function(value, date){
                //更新开始日期的最大日期
                endTimeMin.config.max = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });

        function translateStatus(d) {
            if (d.order_status === '0') {
                return "推送成功";
            } else if (d.order_status === '1') {
                return "推送失败";
            } else if (d.order_status === '2') {
                return "批改推送成功";
            } else if (d.order_status === '3') {
                return "批改推送失败";
            } else{
                return "--";
            }
        }

        tableExt.render({
            elem: '#test'
            , title:'保单记录'
            , height: 'full-180'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/customerEntry/noSystem/insurance/insuranceListNoSystemData'
            , cols: [[
                {type: 'numbers',fixed:'left',title:'序号'}
                , {type: 'checkbox', fixed: 'left'}
                , {field : 'id', minWidth : 200 , title: '操作', align : 'center',rowspan:'2' , fixed: 'left',
                    templet: function (d) {
                        var add = '';
                        if('${fns:checkPagePermission(request, "BDGL_XDJL_XQ_B")}'){
                            add += '<a class="layui-table-opt-link" lay-event="detail">详情</a>';
                        }
                        if('${fns:checkPagePermission(request, "BDGL_XDJL_TS_B")}' && d.status =='2') {
                            add += '<a class="layui-table-opt-link" lay-event="pushAgain"> 推送</a>';
                        }
                        if('${fns:checkPagePermission(request, "BDGL_XDJL_CXTS_B")}' && d.status !=='1'&& d.status !='2') {
                            add += ' <a class="layui-table-opt-link" lay-event="pushAgain"> 重新推送</a>'
                        }
                        if('${fns:checkPagePermission(request, "BDGL_XDJL_CXTS_B")}' && d.partner_name.indexOf("越秀") !== -1) {
                            add += '<a class="layui-table-opt-link" lay-event="pushPartner"> 重推资方</a>';
                        }
                        return add;
                    },
                }
                , {field: 'order_status', title: '保险推送状态',minWidth: 120, align: 'center',exportable:true,
                    templet: translateStatus ,expFormat: translateStatus }
                , {field: 'renewal_status',title: '资方审批状态',minWidth: 150,sort: false,align: 'center',exportable: true}
                , {field: 'remove_sign', title: '拆除状态',minWidth: 100, align: 'center',exportable:true,
                    templet: function (d) {
                        if (d.remove_sign == '0') {
                            return "未拆除";
                        } else {
                            return "已拆除";
                        }
                    },expFormat:function (d){
                        if (d.remove_sign == '0') {
                            return "未拆除";
                        } else {
                            return "已拆除";
                        }
                    }
                }
                , {field: 'remarks',title: '投保反馈',minWidth: 150,sort: false,align: 'center',exportable: true}
                , {field: 'item_no', title: '电站编码',minWidth: 180, align: 'center',exportable:true}
                , {field: 'partner_name', title: '资方',minWidth: 180, align: 'center',exportable:true}
                , {field: 'source', title: '投保方式',minWidth: 100, align: 'center',exportable:true,
                    templet: function (d) {
                        if (d.source == '0') {
                            return "线上投保";
                        } else {
                            return "线下投保";
                        }
                    },expFormat:function (d){
                        if (d.source == '0') {
                            return "线上投保";
                        } else {
                            return "线下投保";
                        }
                    }
                }
                , {field: 'input_piece_id', title: '进件序号',minWidth: 140, align: 'center',exportable:true}
                , {field: 'order_time',title: '推送时间',minWidth: 160,sort: false,align: 'center',exportable: true}
                , {field: 'remarks',title: '投保反馈',minWidth: 150,sort: false,align: 'center',exportable: true}
                , {field: 'request_no',title: '请求流水号',minWidth: 150,sort: false,align: 'center',exportable: true}
                , {field: 'number', title: '保单编号', minWidth: 200, align: 'center', exportable: true}
                , {field: 'insurance_money', title: '投保金额',minWidth: 180, align: 'center', exportable: true}
                , {field: 'price', title: '保费', minWidth: 100, align: 'center', exportable: true}
                , {field: 'partner_name', title: '资方', minWidth: 100, align: 'center', exportable: true}
                , {field: 'tenant_name', title: '业主姓名', minWidth: 150, align: 'center', exportable: true}
                , {field: 'address', title: '项目地址', minWidth: 100, align: 'center', exportable: true}
                , {field: 'trader_name', title: '代理商',minWidth: 200, align: 'center',exportable:true}
                , {field: 'project_name', title: '产品名称', minWidth: 150, align: 'center', exportable: true}
                , {field: 'insurance_company_name',title: '投保公司',minWidth: 200,sort: false,align: 'center',exportable: true}
                , {field: 'policyholder',title: '投保人',minWidth: 200,sort: false,align: 'center',exportable: true}
                , {field: 'insurant',title: '被保人',minWidth: 200,sort: false,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"));
                }
            }
        });

        let repushing = false;
        table.on('tool(test)',function(obj) {
            var id = obj.data.id;
            if (obj.event === 'detail') {
                layer.open({
                    type: 2,
                    content: "${ctx}/customerEntry/noSystem/insurance/renewalNoSysDetail/" + id
                    , title: "详情"
                    , area: ["80%", "80%"]
                });
            }else if (obj.event === 'pushAgain') {
                loading = layer.msg('正在加载', {icon: 16, shade: 0.3, time: 0});
                if (repushing) {
                    return;
                }
                repushing = true;
                $.ajax({
                    url: '${ctx}/customerEntry/noSystem/insurance/repushNoSys',
                    method: 'post',
                    data: { id: obj.data.id },
                    success: function (res) {
                        if (res.code === "200") {
                            layer.close(loading);
                            layer.msg("重新推送成功！", {time: 2000});
                            searchOrder();
                        } else {
                            layer.msg(res.errMsg, {time: 2000});
                        }
                    },
                    complete: function() {
                        repushing = false;
                    },
                    error: function () {
                        layer.close(loading);
                        layer.msg("重新推送失败！", {time: 2000})
                    }
                });
            }else if (obj.event === 'pushPartner') {
                loading = layer.msg('正在加载', {icon: 16, shade: 0.3, time: 0});
                if (repushing) {
                    return;
                }
                repushing = true;
                $.ajax({
                    url: '${ctx}/customerEntry/noSystem/insurance/pushNoSysInsuranceToYueXiu',
                    method: 'post',
                    data: { id: obj.data.id },
                    success: function (res) {
                        if (res.code === "200") {
                            layer.close(loading);
                            layer.msg("推送成功！", {time: 2000});
                            searchOrder();
                        } else {
                            layer.msg(res.errMsg, {time: 2000});
                        }
                    },
                    complete: function() {
                        repushing = false;
                    },
                    error: function () {
                        layer.close(loading);
                        layer.msg("重新推送失败！", {time: 2000})
                    }
                });
            }
        })

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

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

        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 batchDownload(){
        let cacheElement = table.cache["test"];
        var idArr = [];
        cacheElement.forEach(function (json) {
            idArr.push(json.id);
        });
        window.open('${ctx}/customerEntry/insurance/batchDownload/' + idArr)
    }

    //购买保单页面
    function addInsurance(){
        layer.open({
            type: 2,
            content: "${ctx}/customerEntry/insurance/toAddPage"
            , title: "新增保单"
            , area: ["60%", "75%"]
        });
    }

    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) {
        $("#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 + '" title="' + area.columns.DistrictID + '">' + area.columns.DistrictName + '</option>';
                }
                $("#area").append(html);

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

    }


    function searchOrder() {
        //执行重载
        var wherobj =_serializeJson($("#searchForm"));
        tableExt.reload('test', {
            where: wherobj
        });
    }


    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }

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

    function safeVal(val) {
        if (!!val) {
            return val;
        }
        return "";
    }

    function signOperate(removeSign) {
        let ids = [];
        let checked = table.checkStatus('test').data;
        if(!checked || !checked.length) {
            layer.msg('请勾选数据！', {icon:2});
            return;
        }
        for (let i =0; i<checked.length;i++){
            ids.push(checked[i].id);
        }
        $.ajax({
            type: "POST",
            url: "${ctx}/customerEntry/noSystem/insurance/removeSign",
            data: {
                ids: ids.join(","),
                removeSign:removeSign
            },
            success: function (data) {
                if (data.code === "200") {
                    layer.msg("操作成功！", {icon: 1});
                    searchOrder();
                }
            }
        });
    }

    function importData() {
        layer.open({
            title: '批量导入',
            type: 2,
            area: ['550px', '300px'],
            content: '${ctx}/customerEntry/noSystem/insurance/toImportInsuranceNoSystem'
        });
    }
</script>
</body>
</html>

