<%@ 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" />

    <style>
        /*.layui-table th .layui-table-cell{overflow: inherit; text-overflow: inherit; white-space: inherit;}*/
        .layui-table-cell img{ margin-left: 6px}

        .circle2{
            width: 80px;
            height: 80px;
            border-radius: 50%;
            line-height: 22px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        html{ background-color: #FFF}
        .content-box{border-radius: 5px; width: 1000px; margin: 0 auto; box-sizing: border-box; font-size: 14px; color: #333333; line-height: 28px; display: flex;}
        .c-title{ font-weight: 600;margin-top: 20px}
        .content-box .c-left{ flex: 1}
        .content-box .c-right{ width: 200px; text-align: center}
        .m-num{font-size: 20px;}
        .opt-btn{background-color: #0a6597; font-size: 14px; color: #ffffff; padding: 5px 10px; display: inline-block; border-radius: 3px; margin-top: 10px; cursor: pointer}
        .edAmount{font-size: 18px;color:#ff0000}
    </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="ZHGL_KPHZH_ZHMX_T" html='<a href="${ctx}/account/toKphAccountDetail"><li class="layui-this">账户明细</li></a>'/>
             <%--   <sfTags:pagePermission authFlag="ZHGL_KPHZH_BDMX_T" html='<a href="${ctx}/account/toKphBdDetail"><li class="">变动明细</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-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>
        </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>
<div class="layui-fluid layui-bg-white over  hide" style="padding: 10px" id="ideaDiv">
    <div class="">
        <div class="ms">首次发货额度：实缴保证金金额+min(通过踏勘审核的进件量*1.2倍，保证金*2倍）</div>
        <div class="c-title">您当前可申请赊销下单最大额度为： <span class="dt"></span></div>
           <div  class="c-title edAmount" style="text-align: center"></div>
    </div>
</div>
<!-- toolbar -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
<%--        <sfTags:pagePermission authFlag="ZHGL_KPHZH_ZHMX_DC_B" html='<button class="layui-btn layui-btn-sm" id="exportBtn"><i class="layui-icon layui-icon-export"></i>导出</button>'/>--%>
        <sfTags:pagePermission authFlag="ZHGL_KPHZH_ZHMX_DC_B" html='<button class="layui-btn layui-btn-sm"  id="exportExcel"  onclick="exportExcel()"  ><i class="layui-icon layui-icon-export"></i>导出</button>'/>
    </div>
</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();

        var editBth = '${fns:checkPagePermission(request, 'ZHGL_KPHZH_ZHMX_XG_B')}';

        tableExt.render({
            elem: '#test'
            , title:'账户明细'
            , height: 'full-150'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/account/ajaxForAccountDetail?formType=1'
            , cols: [[
                {type: 'numbers',fixed:'left',title:'序号',rowspan:2}
                ,{field:'id', title: '操作',align: 'center',fixed:'left',minWidth:150,rowspan:2
                    ,templet:function(d){
                        var htm="";
                        if(isBlank(d.ifOne) || d.ifOne == 0){
                            htm+= '<a class="layui-table-opt-link" lay-event="getSfAmount">首发授信额</a>';
                        }else if(d.ifOne > 0){
                            htm+= '<a class="layui-table-opt-link" lay-event="getSfAmount">后续授信额</a>';
                        }
                        return htm;
                    }
                }
                , {field: 'traderName', title: '代理商名称',minWidth: 150, sort: false, align: 'center',exportable:true ,rowspan:2}
               /* , {field: 'contacts', title: '联系人',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'telephone', title: '联系电话',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'traderAddress', title: '代理商地址',minWidth: 150, sort: false, align: 'center',exportable:true}*/
                , {field: 'bzjAmount', title: '实缴保证金余额(元)<a lay-tips="代理商当前已实缴保证金余额"><img src="${ctxPlugin}/static/sxImg/wh.png"/></a>',minWidth: 210, sort: false, align: 'center',exportable:true, colspan:3}
                , {field: 'bondAmountLimit', title: '保证金额度(元)<a lay-tips="代理商当前已审核的保证金额度"><img src="${ctxPlugin}/static/sxImg/wh.png"/></a>',minWidth: 180, sort: false, align: 'center',exportable:true,rowspan:2}
                , {field: 'yk_tkl_total_capacity', title: '历史阶段踏勘量(W)<a lay-tips="历史阶段踏勘量(W)"><img src="${ctxPlugin}/static/sxImg/wh.png"/></a>',minWidth: 180, sort: false, align: 'center',exportable:true,rowspan:2
                    ,templet:function(d){
                        var htm = "";
                        if(d.yk_bzj_amount != null) {
                            htm+= d.yk_tkl_total_capacity
                        } else {
                            htm += '0';
                        }
                        if (editBth === '0'){
                            htm += '<a onclick="ykBzjClicked(\'' + d.trader_id + '\', \'' + d.yk_tkl_total_capacity + '\', \'5\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                        }
                        return htm;
                    }
                }
                , {field: 'deliverTotalAmount', title: '累计已发货(元)<a lay-tips="代理商物料申请单，平台已确认发货数量*零售价"><img src="${ctxPlugin}/static/sxImg/wh.png"/></a>',minWidth: 210, sort: false, align: 'center',exportable:true, colspan:3}
                , {field: 'yazTotalCapacity', title: '累计已安装(元)<a lay-tips="标记已安装电站容量（MW）*1.5*1000000/10000"><img src="${ctxPlugin}/static/sxImg/wh.png"/></a>',minWidth: 210, sort: false, align: 'center',exportable:true, colspan:3}
                , {field: 'ybwTotalCapacity', title: ' 累计已并网(元)<a lay-tips="标记已已并网电站容量（MW）*1.5*1000000/10000"><img src="${ctxPlugin}/static/sxImg/wh.png"/></a>',minWidth: 210, sort: false, align: 'center',exportable:true,colspan:3}
               /* , {field: 'basic_quota_amount', title: '标准授信额(元)',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'max_quota_amount', title: '可授信额上限(元)',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'basic_quota_amount_balance', title: '标准授信额余额(元)',minWidth: 150, sort: false, align: 'center',exportable:true}
                , {field: 'max_quota_amount_balance', title: '可授信额上限余额(元)',minWidth: 150, sort: false, align: 'center',exportable:true}*/
            ], [
                 {field: 'bzjAmount', title: '实缴保证金',minWidth: 120, sort: false, align: 'center',exportable:true}
                , {field: 'yk_bzj_amount', title: '历史数据',minWidth: 120, sort: false, align: 'center',exportable:true
                        ,templet:function(d){
                        var htm = "";
                        if(d.yk_bzj_amount != null) {
                            htm += d.yk_bzj_amount;
                        } else {
                            htm += '0';
                        }
                        if (editBth === '0'){
                            htm += '<a onclick="ykBzjClicked(\'' + d.trader_id + '\', \'' + (d.yk_bzj_amount != null ? d.yk_bzj_amount : '0') + '\', \'1\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                        }
                        return htm;
                        }
                }
                , {field: 'bzjAmount + yk_bzj_amount', title: '合计',minWidth: 80, sort: false, align: 'center',exportable:true
                    ,templet:function(d){
                    var sum = d.bzjAmount + d.yk_bzj_amount;
                        var htm = "";
                        htm += sum
                        return htm;
                    }}
                 ,{field: 'deliverTotalAmount', title: '已发货',minWidth: 80, sort: false, align: 'center',exportable:true}
                , {field: 'yk_deliver_total_capacity', title: '历史数据',minWidth: 120, sort: false, align: 'center',exportable:true
                    ,templet:function(d){
                        var htm = "";
                        if(d.yk_bzj_amount != null) {
                            htm+= d.yk_deliver_total_capacity
                        } else {
                            htm += '0';
                        }
                        if (editBth === '0'){
                            htm += '<a onclick="ykBzjClicked(\'' + d.trader_id + '\', \'' + d.yk_deliver_total_capacity + '\', \'2\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                        }
                        return htm;
                    }}
                , {field: '', title: '合计',minWidth: 30, sort: false, align: 'center',exportable:true
                    ,templet:function(d){
                        var sum = d.deliverTotalAmount + d.yk_deliver_total_capacity;
                        var htm = "";
                        htm += sum
                        return htm;
                    }}
                 ,{field: 'yazTotalCapacity', title: '已安装',minWidth: 80, sort: false, align: 'center',exportable:true}
                , {field: 'yk_yaz_total_capacity', title: '历史数据',minWidth: 120, sort: false, align: 'center',exportable:true
                    ,templet:function(d){
                        var htm = "";
                        if(d.yk_bzj_amount != null) {
                            htm+= d.yk_yaz_total_capacity
                        } else {
                            htm += '0';
                        }
                        if (editBth === '0'){
                          htm += '<a onclick="ykBzjClicked2(\'' + d.trader_id + '\', \'' + d.yk_yaz_total_capacity + '\', \'3\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                        }
                        return htm;
                    }}
                , {field: '', title: '合计',minWidth: 30, sort: false, align: 'center',exportable:true
                    ,templet:function(d){
                        var sum = d.yazTotalCapacity + d.yk_yaz_total_capacity;
                        var htm = "";
                        htm += sum
                        return htm;
                    }}
                , {field:  'ybwTotalCapacity', title: '已并网',minWidth: 80, sort: false, align: 'center',exportable:true}
                , {field: 'yk_ybw_total_capacity', title: '历史数据',minWidth: 120, sort: false, align: 'center',exportable:true
                    ,templet:function(d){
                        var htm = "";
                        if(d.yk_bzj_amount != null) {
                            htm += d.yk_ybw_total_capacity
                        } else {
                            htm += '0';
                        }
                        if (editBth === '0'){
                           htm += '<a onclick="ykBzjClicked2(\'' + d.trader_id + '\', \'' + d.yk_ybw_total_capacity + '\', \'4\')"><img style="float: right;" src="${ctxPlugin}/static/sxImg/yk_xg.png"/></a>';
                        }
                        return htm;
                    }}
                , {field: '', title: '合计',minWidth: 30, sort: false, align: 'center',exportable:true
                    ,templet:function(d){
                        var sum = d.ybwTotalCapacity + d.yk_ybw_total_capacity;
                        var htm = "";
                        htm += sum
                        return htm;
                    }}
             ]
            ]
            ,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('tool(test)', function(obj){
            var data = obj.data;
            var traderId = data.trader_id;
            var ifOne=data.ifOne;
            if(isBlank(ifOne)){
                ifOne="0";
            }
            if(obj.event === 'getSfAmount'){
                $(".edAmount").html();
                $.ajax({
                    type:"POST",
                    url:"${ctx}/account/countLimit",
                    data:{
                        traderId:traderId,
                        type:ifOne
                    },
                    success:function(result){
                        if(result.code =='200'){
                            console.log(result.data);
                            $(".edAmount").html(result.data.ed+'元');
                            let totalCapacity = result.data.totalCapacity;//通过踏勘审核的进件量
                            let bondAmountLimit = result.data.bondAmountLimit;//保证金额度
                            let bzjAmount = result.data.bzjAmount;//实缴保证金金额
                            let ykBzjAmount = result.data.ykBzjAmount;//友夸实缴保证金金额
                            let totalBzjAmount = result.data.totalBzjAmount;//总实缴保证金金额
                            let wazTotalCapacity = result.data.wazTotalCapacity;//累计未安装量
                            let wbwTotalCapacity = result.data.wbwTotalCapacity;//累计未并网量

                            var edSet=result.data.edSet;
                            var htm="";
                            var html="";
                            if("0"== ifOne){
                               htm='首次发货额度：实缴保证金金额+min((通过踏勘审核的进件量*'+edSet.columns.capacity_ratio+')*'+edSet.columns.capacity_multiple+'倍，保证金额度*'+edSet.columns.amount+'倍）';
                                html = totalBzjAmount +' + min ( ( '+totalCapacity+' x '+edSet.columns.capacity_ratio+' ) x '+edSet.columns.capacity_multiple+' , '+bondAmountLimit+' x '+edSet.columns.amount+' ) ';
                            }else{
                                htm='发货额度：min(实缴保证金金额+保证金额度*'+edSet.columns.amount+'倍-累计未安装量，保证金额度*'+edSet.columns.capacity_multiple+'倍-累计未并网量）';
                                html=' min ( '+totalBzjAmount+' + '+bondAmountLimit+' x '+edSet.columns.amount+' - '+wazTotalCapacity+' , '+bondAmountLimit+' x '+edSet.columns.capacity_multiple+' - '+wbwTotalCapacity+' ) ';
                            }
                            $(".ms").html(htm);
                            $(".dt").html(html);
                            var index=layer.open({
                                type: 1,
                                area: ['1000px', '400px'],
                                content: $("#ideaDiv"),
                                title: '首发授信额',
                                btn: ['取消'],
                                btnAlign: 'c',
                                yes: function () {
                                    layer.close(index);
                                }
                            })
                        }else{
                            layer.msg("查询失败！")
                        }
                    }
                })
            }
            if(obj.event === 'getHxAmount'){
                var index=layer.open({
                    type: 1,
                    area: ['1000px', '400px'],
                    content: $("#ideaDiv"),
                    title: '后续授信额',
                    btn: ['取消'],
                    btnAlign: 'c',
                    yes: function () {
                        layer.close(index);
                    }
                })
            }
        })

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

    })

    function exportExcel(){
        let traderId = $("select[name='traderId'] option:selected").val();
        window.open('${ctx}/account/exportExcel?traderId='+traderId);
    }
    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 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 ykBzjClicked(trader_id,jq,type) {
       var index =  layer.prompt({title: '修改金额',value:jq}, function(value, index, elem){
            if(!/^\d+(\.\d{1,2})?$/.test(value) || parseFloat(value) < 0) {
                layer.msg("请输入大于等于零的数字，最多保留两位小数！");
                return elem.focus();
            }
            $.ajax({
                type:"POST",
                url:"${ctx}/account/editAmount",
                data:{
                    traderId:trader_id,
                    type:type,
                    amount:value
                },
                success:function(result){
                    if(result.code =='200'){
                        layer.msg("保存成功",{icon:1,time:1000},function(){
                            layer.closeAll()
                            $('#searchBtn').click()
                        })
                    }else{
                        layer.msg("修改失败！")
                    }

                }
            })
        });
    }

    function ykBzjClicked2(trader_id,jq,type) {
        let amount = Number((jq/1500).toFixed(3));
        layer.open({
            type:2,
            title: '修改金额',
            content: '${ctx}/account/toEditAmountPage?amount='+amount+'&type='+type+'&traderId='+trader_id,
            area: ['282px', '152px'],
        });
    }
</script>
</body>
</html>

