<%@ 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" type="text/css" href="${ctxPlugin}/static/css/reset.css" />
    <link rel="stylesheet" href="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.css" media="all">
    <style>
        .layui-form-select dl {
            max-height: 200px;
            width: 80%;
        }
        html {
            background-color: #fff;
        }
        .layui-table td, .layui-table th {
            text-align: center;
        }
        .hx_l > span {
            position: absolute;
            right: 10px;
            top: 5px
        }
        .form-span {
            display: block;
            padding: 5px 0;
            margin-left: 110px;
        }
        #attachFile > li {
            height: 26px
        }
    </style>
</head>
<body>
<div class="layui-form">
    <div class="layui-card">
        <div class="layui-card-body layui-col-xs12" id="LAY_app">
            <div class="layui-row layui-col-space12 layui-form-item">
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label w-130">电站编码：</label>
                    <div class="layui-input-block ">
                        <input type="hidden" id="entryId" value="">
                        <input type="text" id="powerStationCode" name="powerStationCode" disabled class="layui-input" value="">
                        <a href="#" class="pos-a h-32 lh-32 layui-span-unit pl-10 layui-table-link" onclick="checkEntry('${partnerId}')" style="font-size: 20px">+</a>
                    </div>
                </div>
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label">电站状态：</label>
                    <div class="layui-input-block">
                        <input type="text" id="entryStatus" disabled class="layui-input " value="" disabled>
                    </div>
                </div>
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label">代理商名称：</label>
                    <div class="layui-input-block">
                        <input type="text" id="traderName" name="traderName" class="layui-input layui-bg-gray" value="" disabled>
                    </div>
                </div>
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label w-130">所属资方：</label>
                    <div class="layui-input-block ">
                        <input type="text" id="partnerName" class="layui-input" value="" disabled>
                    </div>
                </div>
            </div>
            <div class="layui-row layui-col-space12 layui-form-item">
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label w-130">业主姓名：</label>
                    <div class="layui-input-block ">
                        <input type="text" id="tenantName" name="tenantName"  class="layui-input" value="" disabled>
                    </div>
                </div>
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label w-130">身份证号：</label>
                    <div class="layui-input-block ">
                        <input type="text" id="idCode" name="idCode" class="layui-input" value="" disabled>
                    </div>
                </div>
                <div class="layui-col-lg6 layui-col-sm6">
                    <label class="layui-form-label w-130">安装地址：</label>
                    <div class="layui-input-block ">
                        <input type="text" id="address" name="address" class="layui-input" value="" disabled>
                    </div>
                </div>
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label w-130">变更类型：</label>
                    <div class="layui-input-block ">
                        <input type="text" name="idCode" class="layui-input" value="物料信息" disabled>
                    </div>
                </div>
<%--                <div class="layui-col-lg3 layui-col-sm3">--%>
<%--                    <label class="layui-form-label w-130">变更单号：</label>--%>
<%--                    <div class="layui-input-block ">--%>
<%--                        <input type="text" name="number" class="layui-input" value="" disabled>--%>
<%--                    </div>--%>
<%--                </div>--%>
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label w-130">变更申请人：</label>
                    <div class="layui-input-block ">
                        <input type="text" name="creator" class="layui-input" value="${userName}" disabled>
                    </div>
                </div>
                <div class="layui-col-lg3 layui-col-sm3">
                    <label class="layui-form-label w-130">申请时间：</label>
                    <div class="layui-input-block ">
                        <input type="text" name="creatTime" class="layui-input" value="${now}" disabled>
                    </div>
                </div>
            </div>
            <div class="clear-both">
                <p class="layui-elem-quote" style="margin: 0px 0px 0px 10px">产品信息</p>
                <div class="layui-form-item layui-row">
                    <div class="layui-col-sm3 layui-col-md3">
                        <label class="layui-form-label">备案类型：</label>
                        <span class="form-span" id="filingType"></span>
                    </div>
                    <div class="layui-col-sm3 layui-col-md3">
                        <label class="layui-form-label">项目公司：</label>
                        <span class="form-span" id="projectCompanyName"></span>
                    </div>
                    <div class="layui-col-sm3 layui-col-md3">
                        <label class="layui-form-label">业务属性：</label>
                        <span class="form-span" id="productLine"></span>
                    </div>
                    <div class="layui-col-sm12 layui-col-md12" id="projectTab">
                        <table class="layui-table">
                            <colgroup>
                                <col width="25%">
                                <col width="25%">
                                <col width="25%">
                                <col width="25%">
                            </colgroup>
                            <thead>
                            <tr style="background: white">
                                <th style="text-align: center">序号</th>
                                <th style="text-align: center">产品编号</th>
                                <th style="text-align: center">产品名称</th>
                                <th style="text-align: center">屋顶类型</th>
                            </tr>
                            </thead>
                            <tbody id="projectList">

                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="layui-inline layui-col-xs12">
                    <label class="layui-form-label w-130">变更原因：</label>
                    <div class="layui-input-block ">
                        <textarea name="reason" id="reason" placeholder="" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 mb-20 pr-30 mt-10">
                <p class="layui-elem-quote" style="margin: 0px 0px 0px 10px">物料信息</p>
                <div class="layui-tab layui-tab-brief" lay-filter="materialTabBrief" id="materialInfoTab">
                    <ul class="layui-tab-title" id="materialInfoUl"></ul>
                </div>
            </div>
        </div>

        <div class="layui-form-item operate-footer" style="text-align: center;">
            <input type="button" lay-submit lay-filter="confirm" id="confirm" class="layui-btn " value="提交">
            <button type="button" class="layui-btn layui-btn-primary " onclick="closeDiv();">取消</button>
        </div>

    </div>
</div>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.js"></script>
<script src="${ctxPlugin}/lib/xm-select.js"></script>
<script type="text/javascript">

    //图片查看效果
    var viewer = new Viewer(document.getElementById('LAY_app'));
    //初始数据
    var $form;
    var form;
    var upload;
    var $;
    var imgs = '';
    var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    layui.use(['form', 'table', 'upload', 'jquery', 'element'], function () {
        $ = layui.jquery;
        element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
        form = layui.form;
        upload = layui.upload;
        $form = $('form');

        element.on('tab(materialTabBrief)', function (data) {
            $('.materialTabContent').eq(data.index).removeClass('layui-hide').siblings('.materialTabContent').addClass('layui-hide');
        });

        var saveFlag = true;
        form.on('submit(confirm)', function(data){
            if(!saveFlag){
                return;
            }
            if (isBlank($("#entryId").val())){
                layer.msg("请选择电站",{icon:2})
                return;
            }
            if (isBlank($("#reason").val())){
                layer.msg("请填写变更原因",{icon:2})
                return;
            }

            var bom = {};
            bom.entryId=$("#entryId").val();
            bom.powerStationCode=$("#powerStationCode").val();
            bom.changeReason=$("#reason").val();
            bom.materialList = [];

            var projectTrs =$("#materialInfoUl li");
            var checkRe = true;
            projectTrs.each(function (index) {
                var materialInfo = {};
                let projectId = $(this).attr("data-value");
                let materialTab = $("#materialInfoTab").find("#materialInfoDiv"+index);
                var zjDetails = [];
                materialTab.find("#materialTable"+index).find("tr").each(function () {
                    var material = {};
                    material.materialId = $(this).attr("data-id");
                    material.materialCode=$(this).find('td').eq(1).text();
                    material.materialName=$(this).find('td').eq(2).text();
                    material.materialSpec=$(this).find('td').eq(3).text();
                    material.materialBrand=$(this).find('td').eq(4).text();
                    material.barcode = $(this).find('td').eq(5).text();
                    material.newBarcode = $(this).find('td').eq(6).text();
                    if(isBlank(material.newBarcode)){
                        material.newBarcode = $(this).find('td').eq(6).find("input").val();
                    }
                    zjDetails.push(material);
                });
                materialInfo.zjDetails = zjDetails;
                var nbqDetails=[];
                materialTab.find("#nbqTable"+index).find("tr").each(function () {
                    var material2 = {};
                    material2.materialId = $(this).attr("data-id");
                    material2.materialCode=$(this).find('td').eq(1).text();
                    material2.materialName=$(this).find('td').eq(2).text();
                    material2.materialSpec=$(this).find('td').eq(3).text();
                    material2.materialBrand=$(this).find('td').eq(4).text();
                    material2.nbqSn = $(this).find('td').eq(5).text();
                    material2.newNbqSn = $(this).find('td').eq(6).text();
                    if(isBlank(material2.newNbqSn)){
                        material2.newNbqSn = $(this).find('td').eq(6).find("input").val().trim();
                    }
                    material2.sn = $(this).find('td').eq(7).text();
                    material2.newSn = $(this).find('td').eq(8).text();
                    if(isBlank(material2.newSn)){
                        material2.newSn = $(this).find('td').eq(8).find("input").val();
                    }
                    material2.yzCode = $(this).find('td').eq(9).text();
                    material2.newYzCode = $(this).find('td').eq(10).text();
                    if(isBlank(material2.newYzCode)){
                        material2.newYzCode = $(this).find('td').eq(10).find("input").val();
                    }

                    if (material2.newYzCode == '--'){
                        material2.newYzCode = null;
                    }
                    nbqDetails.push(material2);
                });
                materialInfo.nbqDetails = nbqDetails;
                materialInfo.projectId=projectId;
                bom.materialList.push(materialInfo);
            });
            saveFlag = false;
            var index= layer.load(2, {shade: [0.5,'#fff'], content:''});
            $.ajax({
                url:'${ctx}/entryChanges/materialInformationChange/addMaterialInformationChangeHR',
                method:'post',
                contentType: 'application/json',
                data:JSON.stringify(bom),
                dataType:'JSON',
                success:function(res){
                    if(res.code=="200"){
                        parent.layer.msg("操作成功",{time:1000});
                        closeDiv();
                        parent.searchOrder();
                    } else{
                        layer.msg(res.errMsg,{icon:2,time:1000});
                        saveFlag = true;
                    }
                },
                error: function (res) {
                    layer.msg(res.errMsg, {icon: 2});
                    saveFlag = true;
                },
                complete:function (){
                    layer.close(index);
                }
            }) ;
        });
    })

    function getStatus(d) {
        var status =d.status;

        if (status == '0') {
            //预审状态 0.暂存 1.待预审 2.预审待审批 3.预审审批中 4.预审通过 5.预审拒绝
            if(d.credit=='1'){
                return '待预审';
            }else if(d.credit=='2'){
                return '预审审批中';
            }else if(d.credit=='3'){
                return '预审审批中';
            }else if(d.credit=='4'){
                if(d.designAuditFlag == '3'){
                    return '自审驳回';
                }else if(d.designAuditFlag == 'a'){
                    return '待设计';
                }else {
                    return '待踏勘';
                }
            }else if(d.credit=='5'){
                return '预审拒绝';
            }
            return '暂存';
        }
        if (status == '1') {
            //设计审核状态：1.待踏勘 2.待自审 3.自审驳回 4.自审通过 5.设计审核驳回 6.设计审核通过/已完成 7.进件审核驳回
            if(d.designAuditFlag=='1'){
                return '待踏勘';
            }else if(d.designAuditFlag=='2'){
                return '待自审';
            }  else if(d.designAuditFlag=='3'){
                return '自审驳回';
            } else if(d.designAuditFlag=='4'){
                return '自审通过';
            } else if(d.designAuditFlag=='5'){
                return '设计审核驳回';
            } else if(d.designAuditFlag=='6'){
                return '设计审核通过';
            }else if(d.designAuditFlag=='7'){
                return '进件审核驳回';
            }else if (d.designAuditFlag == '8') {
                return "进件审核中";
            } else if (d.designAuditFlag == '9') {
                return "进件审核通过";
            } else if (d.designAuditFlag == 'a') {
                return "待设计";
            }
            return '待审核';
        }
        if (status == '2') {
            return '审核驳回';
        }
        if (status == '3') {
            return '待下单';
        }
        if (status == '4') {
            if (d.installFlag == '1'){
                return '待派工';
            }else if (d.installFlag == '2'){
                return '待安装';
            }else if (d.installFlag == '3'){
                return '自审驳回';
            }else if (d.installFlag == '4'){
                return '安装完成';
            }
        }
        if(status == '5'){
            return '待安装验收';
        }
        if(status == 'f'){
            return '安装验收退回';
        }
        if(status == '6'){
            return '待并网';
        }
        if(status == '7'){
            return '待并网验收';
        }
        if(status == '8'){
            return '待交付';
        }
        if(status == '9'){
            return '待归档';
        }
        if(status == '10'){
            return '验收驳回';
        }
        if(status == '11'){
            return '归档驳回';
        }
        if(status == 'a'){
            return '已完成';
        }if(status == 'c'){
            return '待代理商确认';
        }
        // if(status == 'd'){
        //     return '待上传发票';
        // }
        if(status == 'e'){
            return '待签署合同';
        }
        if(status == 'w'){
            return '信息登记';
        }
        if(status == 'g'){
            return '设计审核';
        }
        if(status == 'AN'){
            return '待安装确认';
        }
        if(status == 'AT'){
            return '安装确认驳回';
        }
        if(status == 'BW'){
            return '待并网确认';
        }
        if(status == 'BN'){
            return '并网确认驳回';
        }
        if(status == 'h'){
            return '设计自审';
        }
        if(status == 'i'){
            return '安装自审';
        }
    }

    function addInfo(result){
        let customerEntry = result.customerEntry;
        let projectList = result.projectList;

        $("#entryId").val(customerEntry.id);
        $("#powerStationCode").val(customerEntry.powerStationCode);
        $("#entryStatus").val(getStatus(customerEntry));
        $("#traderName").val(customerEntry.traderName);
        $("#projectCompanyName").text(customerEntry.projectCompanyName);
        $("#partnerName").val(customerEntry.partnerName);
        $("#tenantName").val(customerEntry.tenantName);
        if ("1"==customerEntry.filingType){
            $("#filingType").text("农户备案");
        }else if ("2"==customerEntry.filingType){
            $("#filingType").text("项目公司备案");
        }
        let productLine = "";
        if (customerEntry.productLine == "1"){
            productLine="纯租25年业务";
        }else if (customerEntry.productLine == "2"){
            productLine="经营性租赁";
        }else if (customerEntry.productLine == "3"){
            productLine="融资租赁";
        }else if (customerEntry.productLine == "4"){
            productLine="光伏贷";
        }else if (customerEntry.productLine == "5"){
            productLine="全款";
        }else if (customerEntry.productLine == "6"){
            productLine="其他";
        }else {
            productLine="其他";
        }
        $("#productLine").text(productLine);
        $("#idCode").val(customerEntry.idCode);
        $("#address").val(customerEntry.province+""+customerEntry.city+""+customerEntry.area+""+customerEntry.address);
        let html = "";//产品信息
        let html1 = "";//屋顶信息
        let html2 = "";//物料信息
        // 产品信息
        let index = 1;
        for (var i=0; i<projectList.length; i++){
            let projectName = projectList[i].columns.projectName;
            let projectCode = projectList[i].columns.projectCode;
            let projectId = projectList[i].columns.projectId;
            let roofType = projectList[i].columns.roofType;
            let zjList = projectList[i].columns.zjList;
            let nbqList = projectList[i].columns.nbqList;
            // projectList
            html += '<tr style="text-align: center" data-id="'+projectId+'" class="projectTr" >';
            html += '<td>'+index+'</td>';
            html += '<td>'+projectCode+'</td>';
            html += '<td data-id="'+projectId+'">'+projectName+'</td>';
            html += '<td>'+roofType+'</td>';
            html += '</tr>';
            // materialInfoUl
            if (i == 0){
                html1 += '<li class="layui-this" id="'+projectCode+'" data-value="'+projectId+'">';
            }else {
                html1 += '<li id="'+projectCode+'" data-value="'+projectId+'">';
            }
            if (!isBlank(roofType)){
                html1 += roofType;
            }else {
                html1 += projectName;
            }
            html1 += '</li>';

            if (i==0){
                html2 +=`<div class="layui-tab layui-tab-brief materialTabContent " id="materialInfoDiv`+i+`"  lay-filter="docDemoTabBrief">`;
            }else {
                html2 +=`<div class="layui-tab layui-tab-brief materialTabContent layui-hide " id="materialInfoDiv`+i+`"  lay-filter="docDemoTabBrief">`;
            }
            html2 += `<ul class="layui-tab-title">
                        <li class="layui-this">组件</li>
                        <li>逆变器</li>
                      </ul>`;
            html2 += `<div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <table class="layui-table">
                                <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>物料代码</th>
                                    <th>物料名称</th>
                                    <th>物料规格</th>
                                    <th>品牌</th>
                                    <th>变更前物料条码</th>
                                    <th>变更后物料条码</th>
                                </tr>
                                </thead>
                                <tbody id="materialTable`+i+`">`;
                                for (let a = 0; a < zjList.length; a++){
                                    let code = zjList[a].columns.code;
                                    html2+='<tr data-id="'+zjList[a].columns.material_id+'">';
                                    html2+='<td>'+(a+1)+'</td>';
                                    html2+='<td>'+code+'</td>';
                                    html2+='<td>'+zjList[a].columns.name+'</td>';
                                    html2+='<td>'+zjList[a].columns.spec+'</td>';
                                    html2+='<td>'+zjList[a].columns.brand+'</td>';
                                    html2+='<td>'+zjList[a].columns.barcode+'</td>';
                                    html2 += '<td>' + '<input type="text" readonly class="layui-input" id='+index+code+(a+1)+'  onclick="getChangeBarCode('+'\''+code+'\',\''+index+code+(a+1)+'\''+')" name="newBarcode"/>'+ '</td>';
                                    html2+='</tr>';
                                }
                   html2 += `</tbody>
                            </table>
                        </div>
                        <div class="layui-tab-item">
                            <table class="layui-table">
                                <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>物料代码</th>
                                    <th>物料名称</th>
                                    <th>物料规格</th>
                                    <th>品牌</th>
                                    <th>逆变器sn码</th>
                                    <th>变更后逆变器sn码</th>
                                    <th>通讯棒sn码</th>
                                    <th>变更后通讯棒sn码</th>
                                    <th>验证码</th>
                                    <th>变更后验证码</th>
                                </tr>
                                </thead>
                                <tbody id="nbqTable`+i+`">`;
                                for (let b = 0; b < nbqList.length; b++){
                                    let code = nbqList[b].columns.code;
                                    html2+="<tr data-id="+nbqList[b].columns.material_id+">";
                                    html2+='<td>'+(b+1)+'</td>';
                                    html2+='<td>'+code+'</td>';
                                    html2+='<td>'+nbqList[b].columns.name+'</td>';
                                    html2+='<td>'+nbqList[b].columns.spec+'</td>';
                                    html2+='<td>'+nbqList[b].columns.brand+'</td>';
                                    html2+='<td>'+nbqList[b].columns.nbq_sn+'</td>';
                                    html2+= '<td>' + '<input type="text" readonly class="layui-input" id='+index+code+(b+1)+'  onclick="getChangeBarCode('+'\''+code+'\',\''+index+code+(b+1)+'\''+')"  name="newNbqSn"/>'+ '</td>';
                                    html2+='<td>'+nbqList[b].columns.sn+'</td>';
                                    html2+='<td>' + '<input type="text" class="layui-input" name="newSn"/>'+ '</td>';
                                    if (nbqList[b].columns.brand == '固德威' || nbqList[b].columns.brand == '爱士惟'){
                                        html2 += '<td>' + nbqList[b].columns.yz_code + '</td>';
                                        html2 += '<td>' + '<input type="text"class="layui-input" name="newYzCode"/>'+ '</td>';
                                    }else {
                                        html2 += '<td>--</td>';
                                        html2 += '<td>--</td>';
                                    }
                                    html2+='</tr>';
                                }
                    html2 += `</tbody>
                                    </table>
                                </div>
                            </div>`;
                    html2+='</div>';
            index++;
        }
        $("#projectList").empty();
        $("#projectList").append(html);
        $("#materialInfoTab").empty().append(`<ul class="layui-tab-title" id="materialInfoUl"></ul>`)
        $("#materialInfoUl").empty().append(html1);
        $("#materialInfoTab").append(html2);
    }

    function checkEntry(partnerId){
        layer.open({
            type: 2,
            title: '选择电站',
            content: '${ctx}/entryChanges/materialInformationChange/toCheckEntry?partnerId='+partnerId,
            area: ['80%','80%']
        });
    }
    function setValue(uniqueId,code){
        document.getElementById(""+uniqueId+"").value=code;
    }
    function getChangeBarCode(code,id){
        layer.open({
            type: 2,
            title: '选择条码',
            content: '${ctx}/entryChanges/materialInformationChange/toCheckBarcode?code='+code+'&id='+id,
            area: ['40%','40%'],
            success: function(layero, index) {

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

    function closeDiv() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    }

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

</script>
</body>
</html>


