<%@ 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="TBJL_TBJL_T" html='<a href="${ctx}/customerEntry/insurance/list"><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="orderStatus" lay-search>
                                <option value="">请选择</option>
                                <option value="0">推送成功</option>
                                <option value="1">推送失败</option>
                                <option value="2">批改推送成功</option>
                                <option value="3">批改推送失败</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="distributorId" 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>
                    <div class="layui-col-lg4 layui-col-sm6 layui-col-md6" >
                        <div class="query-adr">
                            <label class="layui-form-label">项目区域：</label>
                            <div class="inline-block" style="width: 25%">
                                <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 ml-5" style="width: 25%;">
                                <select name="city" id="city" lay-filter="city"
                                        lay-search>
                                    <option value="">市</option>
                                </select>
                            </div>
                            <div class="inline-block ml-5" style="width: 25%">
                                <select name="area" id="area" lay-search>
                                    <option value="">县/区</option>

                                </select>
                            </div>
                        </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="orderTimeMin" class="layui-input" id="orderTimeMin" placeholder="开始日期">
                            </div>
                            <div class="layui-col-xs2 text-c lh-30">
                                —
                            </div>
                            <div class="layui-col-xs5">
                                <input type="text" name="orderTimeMax" class="layui-input" id="orderTimeMax" placeholder="结束日期">
                            </div>
                        </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="TBJL_TBJL_DCLB_B" 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>
<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: '#orderTimeMin'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新结束日期的最小日期
                startTimeMax.config.min = lay.extend({}, date, {
                    month: date.month - 1
                });
            }
        });
        //结束日期
        var startTimeMax = laydate.render({
            elem: '#orderTimeMax'
            /* ,type: 'datetime'*/
            ,done: function(value, date){
                //更新开始日期的最大日期
                startTimeMin.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 "批改推送失败";
            }
        }

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

        tableExt.render({
            elem: '#test'
            , title:'投保记录'
            , height: 'full-180'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/insurance/push/insuranceData'
            , 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, "TBJL_TBJL_XQ_B")}') {
                            add += '<a class="layui-table-opt-link" lay-event="detail">详情</a>';
                        }
                        if('${fns:checkPagePermission(request, "TBJL_TBJL_CXTS_B")}' && d.status !=='1') {
                            add += ' <a class="layui-table-opt-link" lay-event="push">重新推送</a>'
                        }
                        return add;
                    },
                }
                , {field: 'order_status', title: '状态',minWidth: 120, align: 'center',exportable:true,
                    templet: translateStatus ,expFormat: translateStatus }
                , {field: 'power_station_code', 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: 'tenant_name', title: '业主姓名', minWidth: 150, align: 'center', exportable: true}
                , {field: 'address', title: '项目地址', minWidth: 100, align: 'center', exportable: true,
                    templet: function (d) {
                        return safeVal(d.province) + safeVal(d.city) + safeVal(d.area) + safeVal(d.street) + safeVal(d.address);
                    }, expFormat: function(d) {
                        return safeVal(d.province) + safeVal(d.city) + safeVal(d.area) + safeVal(d.street) + safeVal(d.address);
                    }}
                , {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/insurance/detail/" + id
                    , title: "详情"
                    , area: ["60%", "75%"]
                });
            } else if (obj.event === 'downLoadCon') {
                var href = obj.data.down_url;
                if (!isBlank(href)) {
                    window.open('${commonStaticImgPath}' + href);
                } else {
                    layer.msg("保单数据为空");
                }
            } else if (obj.event === 'push') {
                if (repushing) {
                    return;
                }
                repushing = true;
                $.ajax({
                    url: '${ctx}/insurance/push/repush',
                    method: 'post',
                    data: { id: obj.data.id },
                    success: function (res) {
                        if (res.code === "200") {
                            layer.msg("重新推送成功！", {time: 2000});
                            table.reload('test');
                        } else {
                            layer.msg(res.errMsg, {time: 2000});
                        }
                    },
                    complete: function() {
                        repushing = false;
                    },
                    error: function () {
                        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 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("");
    }
</script>
</body>
</html>
