<%@ 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="ZHGL_ZBJZH_ZHMX_T" html='<a href="${ctx}/account/toZbjAccountDetail"><li class="">账户明细</li></a>'/>
                <sfTags:pagePermission authFlag="ZHGL_ZBJZH_BDMX_T" html='<a href="${ctx}/account/toZbjChangeDetail"><li class="">变动明细</li></a>'/>
                <sfTags:pagePermission authFlag="ZHGL_ZBJZH_JSMX_T" html='<a href="${ctx}/account/toZbjSettleDetail"><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" 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="projectId" id="projectId" 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-lg2 layui-col-sm3">
                    <label class="layui-form-label">产品名称：</label>
                    <div class="layui-input-block">
                        <input type="text" name="projectName" class="layui-input" autocomplete="off">
                    </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" id="powerStationCode" class="layui-input" autocomplete="off">
                    </div>
                </div>

                <c:if test="${user.userType ne '6' && user.userType ne '7'}">
                    <div class="layui-col-lg3 layui-col-sm3" style="width: 27.5%">
                        <label class="layui-form-label">代理商名称：</label>
                        <div class="layui-input-block" >
                            <select 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>
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">代理商电话：</label>
                        <div class="layui-input-block">
                            <input type="text" name="traderMobile" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </c:if>

                <c:if test="${'2' ne user.traderType}">
                    <div class="layui-col-lg2 layui-col-sm3">
                        <label class="layui-form-label">二级代理商：</label>
                        <div class="layui-input-block">
                            <select 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="tenantName" id="tenantName" class="layui-input" autocomplete="off">
                    </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" autocomplete="off">
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">屋顶类型：</label>
                    <div class="layui-input-block" >
                        <select name="roofType" id="roofType">
                            <option value="">请选择</option>
                            <option value="平屋顶">平屋顶</option>
                            <option value="斜屋顶">斜屋顶</option>
                        </select>
                    </div>
                </div>
                <div class="layui-col-lg2 layui-col-sm3">
                    <label class="layui-form-label">结算类型：</label>
                    <div class="layui-input-block" >
                        <select name="type">
                            <option value="">请选择</option>
                            <option value="3">质保金</option>
                            <option value="5">惩罚政策</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>
        </div>
    </div>
</div>
<!-- toolbar -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <sfTags:pagePermission authFlag="ZHGL_ZBJZH_JSMX_DC_B" html='<button class="layui-btn layui-btn-sm" id="exportBtn"><i class="layui-icon layui-icon-export"></i>导出</button>'/>
        <sfTags:flowableAddPermission formId="${formId}" html='<button class="layui-btn layui-btn-sm" id="addCashOut" lay-event="addCashOut"><i class="layui-icon layui-icon-add-1"></i>请款</button>'/>
    </div>
</script>
<!--操作栏-->
<script type="text/html" id="optTpl">
    <a class="layui-table-opt-link" lay-event="viewDetail">查看</a>
</script>
<!-- 电站编码 -->
<script type="text/html" id="stationCodeTpl">
    <a href="#" class="layui-table-link" onclick="psDetail('{{d.entry_id}}')">{{d.power_station_code}}</a>
</script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/lib/formSelects-v4.js"></script>
<script>
    var entrycount=0;
    layui.config({
        base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'tableExt', 'jquery', 'layer','laypage','laydate','form', 'upload', 'laytpl'], function(){
        //layui.use(['table','layer','laypage','laydate','form'], function(){
        $ = layui.$;
        tableExt = layui.tableExt;
        layer = layui.layer;
        laypage = layui.laypage;
        laydate = layui.laydate;
        form = layui.form;
        laytpl = layui.laytpl;
        table = tableExt.getLayTable();

        tableExt.render({
            elem: '#test'
            , title:'结算明细'
            , height: 'full-180'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/account/ajaxForSettleDetail?objType=1'
            , cols: [[
                {type: 'numbers',fixed:'left',title:'序号'}
                ,{field : 'id', minWidth : '4%',checkbox : true,align : 'center',rowspan:'2' , fixed: 'left'}
                , {field: 'power_station_code', title: '电站编码',minWidth: 150,fixed:'left', sort: false, align: 'center',exportable:true,templet: '#stationCodeTpl'}
                <c:if test="${user.userType ne '6' && user.userType ne '7'}">
                , {field: 'trader_name', title: '代理商名称',minWidth: 150, sort: false, align: 'center',exportable:true}
                </c:if>
                <c:if test="${'2' ne user.traderType}">
                , {field: 'createTraderName',title: '二级代理商',minWidth: '150',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: 'type', title: '结算类型', minWidth: 150, sort: false, align: 'center', exportable: true
                    , templet: function (d) {
                        if (d.type === '2') {
                            return '电站整改';
                        }else if (d.type === '3') {
                            return '质保金';
                        }else if (d.type === '5') {
                            return '惩罚政策';
                        }
                    }
                    ,expFormat: function(d){
                        if (d.type === '2') {
                            return '电站整改';
                        }else if (d.type === '3') {
                            return '质保金';
                        }else if (d.type === '5') {
                            return '惩罚政策';
                        }
                    }
                }
                , {field: 'amount', title: '结算金额',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'unit_price', title: '结算单价',minWidth: 130, sort: false, align: 'center',exportable:true,
                    templet: function (d) {
                        if (d.type === '4') {
                            return '--';
                        }else if(d.unit_price===null||d.unit_price===''){
                            return'--';
                        }
                        else{
                            return d.unit_price;
                        }
                    }
                    ,expFormat: function(d){
                        if (d.type === '4') {
                            return '--';
                        }else if(d.unit_price===null||d.unit_price===''){
                            return'--';
                        }else{
                            return d.unit_price;
                        }
                    }

                }
                , {field: 'capacity', title: '安装容量(W)',minWidth: 130, sort: false, align: 'center',exportable:true,
                    templet: function (d) {

                        if (d.type === '4') {
                            return '--';
                        }else if(d.capacity===null||d.capacity===''){
                            return'--';
                        }else{
                            return d.capacity;
                        }

                    }
                    ,expFormat: function(d){
                        if (d.type === '4') {
                            return '--';
                        }else if(d.capacity===null||d.capacity===''){
                            return'';
                        }
                        else{
                            return d.capacity;
                        }
                    }
                }

                , {field: 'flag', title: '请款状态', minWidth: 150, sort: false, align: 'center', exportable: true
                    , templet: function (d) {
                        if (d.flag === '2') {
                            return '已请款';
                        }else{
                            return '未请款';
                        }
                    }
                    ,expFormat: function(d){
                        if (d.flag === '2') {
                            return '已请款';
                        }else{
                            return '未请款';
                        }

                    }
                }
                , {field: 'roof_type', title: '屋顶类型',minWidth: 120, sort: false, align: 'center',exportable:true}
                , {
                    field: 'scheme_type', title: '设计方案', minWidth: 100, sort: false, align: 'center', exportable: true,
                    templet: function (d) {
                        if (d.scheme_type === '1') {
                            return '标准方案';
                        }
                        if (d.scheme_type === '2') {
                            return '组合方案';
                        }
                        return '';
                    },
                    expFormat: function (d) {
                        if (d.scheme_type === '1') {
                            return '标准方案';
                        }
                        if (d.scheme_type === '2') {
                            return '组合方案';
                        }
                        return '';
                    }
                }
                , {field: 'scheme_type_name', title: '方案名称',minWidth: 120, sort: false, align: 'center',exportable:true}
                , {field: 'tenant_name', title: '业主姓名',minWidth: 100, sort: false, align: 'center',exportable:true}
                , {field: 'mobile', title: '联系方式',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'project_code', title: '产品编号',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'project_name', title: '产品名称',minWidth: 180, sort: false, align: 'center',exportable:true}
                , {field: 'create_time', title: '生成时间',minWidth: 180, sort: false, align: 'center',exportable:true}
                , {field: 'end_time', title: '到期时间',minWidth: 180, 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"));
                }
            },done: function(res, curr, count){
                //得到数据总量

                entrycount = count;
            }
        });

        table.on('toolbar(test)',function(obj){
            switch(obj.event){
                case 'addCashOut':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请至少选择一条数据",{icon:5,anim:6});
                        return;
                    }
                    var settleId="";
                    var outAmount=0;
                    var traderId="";
                    for(var i=0;i<data.length;i++){
                        if(data[i].flag ==1){
                            layer.msg("第"+(i+1)+"条，质保金未释放，不能请款！",{icon:5,anim:6});
                            return;
                        }
                        if(data[i].flag ==2){
                            layer.msg("第"+(i+1)+"条，已请款的结算明细不能重复操作请款，请重新选择！",{icon:5,anim:6});
                            return;
                        }
                        if(isBlank(traderId)){
                            traderId=data[i].distributor_id;
                        }else if(traderId != data[i].distributor_id){
                            layer.msg("第"+(i+1)+"条，请选择相同的代理商请款！",{icon:5,anim:6});
                            return;
                        }
                        outAmount+=data[i].amount;
                        settleId+=data[i].id+",";
                    }
                    console.log(settleId+'*****'+outAmount);
                    var tem=parseFloat(outAmount).toFixed(2)
                    layer.open({
                        title:'新增',
                        type: 2,
                        area: ["100%", "100%"],
                        content: '${ctx}/apply/outCash/addCashOutApplyPage?objType=1&settleId='+settleId+'&outAmount='+tem+'&traderId='+traderId
                    })

                    break;
            }

        })

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

        table.on('tool(test)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if (layEvent === 'viewDetail') {
                layer.open({
                    title:'查看',
                    type: 2,
                    area: ["60%", "70%"],
                    content: '${ctx}/apply/settlementAmount/settlementAmountApplyDetail?id='+data.id
                })
            }
        })
    })

    function downloadAttach(attach){
        attach=decodeURI(attach);
        attach = attach.replace(/\+/g,"%2B"); //
        window.location.href = '${commonStaticImgPath}' + attach;
    }

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

    //查看详情
    function psDetail(id){
        layer.open({
            type: 2
            , title: '电站详情'
            , content: '${ctx}/customerEntry/entryDetail?entryId=' + id
            , area: ['100%', '100%']
            , yes: function(index, layero){

            }
        });
    }

    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>

