<%@ 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
        }
        #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">审核状态：</label>
                    <div class="layui-input-block">
                        <input type="text" name="status" class="layui-input layui-bg-gray" 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="projectName" name="projectName" class="layui-input layui-bg-gray" 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="projectCompanyName" disabled class="layui-input " value="">
                    </div>
                </div>
                <div class="layui-inline layui-col-xs3">
                    <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 class="layui-inline layui-col-xs12">
                    <label class="layui-form-label w-130"><i class="mark" >*</i> 变更原因：</label>
                    <div class="layui-input-block ">
                        <textarea name="changeReason" id="changeReason" placeholder="" class="layui-textarea"></textarea>
<%--                        <input type="text" id="changeReason" name="changeReason" class="layui-input" value="" autocomplete="off">--%>
                    </div>
                </div>
            </div>
            <div class="layui-col-xs12 mb-20 pr-30 mt-10">
                <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                    <ul class="layui-tab-title">
                        <li class="layui-this">组件</li>
                        <li>逆变器</li>
                    </ul>
                    <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">
                                <c:forEach items="${details}" varStatus="vs" var="material">
                                    <tr>
                                        <td>${vs.count}</td>
                                        <td>${material.columns.material_code}</td>
                                        <td>${material.columns.material_name}</td>
                                        <td>${material.columns.material_spec}</td>
                                        <td>${material.columns.material_brand}</td>
                                        <td>${material.columns.barcode}</td>
                                        <td><input type="text" class="layui-input" name="newBarcode"/></td>
                                    </tr>
                                </c:forEach>
                                </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">
                                <c:forEach items="${nbqList}" varStatus="vs" var="material">
                                    <tr>
                                        <td>${vs.count}</td>
                                        <td>${material.columns.material_code}</td>
                                        <td>${material.columns.material_name}</td>
                                        <td>${material.columns.material_spec}</td>
                                        <td>${material.columns.material_brand}</td>
                                        <td>${material.columns.nbq_sn}</td>
                                        <td>

                                        </td>
                                        <td>${material.columns.sn}</td>
                                        <td>

                                        </td>
                                        <td>${material.columns.yz_code}</td>
                                        <td>

                                        </td>
                                    </tr>
                                </c:forEach>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </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');


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

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

            var arr = [];
            $('#materialTable>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();
                }
                arr.push(material);
            });
            bom.zjDetails = arr;

            var arr2 = [];
            $('#nbqTable>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;
                }
                arr2.push(material2);
            });
            bom.nbqDetails = arr2;

            saveFlag = false;
            $.ajax({
                url:'${ctx}/entryChanges/materialInformationChange/addMaterialInformationChange',
                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,{time:1000});
                        saveFlag = true;
                    }
                },
                error: function (res) {
                    layer.msg(res.errMsg, {icon: 2});
                    saveFlag = true;
                },
            }) ;
        });



    })

    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 zjList = result.zjList;
        let nbqList = result.nbqList;
        $("#entryId").val(customerEntry.id);
        $("#powerStationCode").val(customerEntry.powerStationCode);
        $("#entryStatus").val(getStatus(customerEntry));
        $("#traderName").val(customerEntry.traderName);
        $("#projectName").val(customerEntry.projectName);
        $("#projectCompanyName").val(customerEntry.projectCompanyName);
        $("#partnerName").val(customerEntry.partnerName);
        $("#tenantName").val(customerEntry.tenantName);
        $("#idCode").val(customerEntry.idCode);
        $("#address").val(customerEntry.province+""+customerEntry.city+""+customerEntry.area+""+customerEntry.address);


        var html = "";
        for(var i=0; i<zjList.length; i++){
            html += "<tr data-id="+zjList[i].columns.material_id+">";
            html += '<td>' + (i+1) + '</td>';
            html += '<td>' + zjList[i].columns.code + '</td>';
            html += '<td>' + zjList[i].columns.name + '</td>';
            html += '<td>' + zjList[i].columns.spec + '</td>';
            html += '<td>' + zjList[i].columns.material_brand + '</td>';
            html += '<td>' + zjList[i].columns.barcode + '</td>';
            let code = zjList[i].columns.code;
            html += '<td>' + '<input type="text" readonly class="layui-input" id='+code+(i+1)+'  onclick="getChangeBarCode('+'\''+code+'\',\''+code+(i+1)+'\''+')" name="newBarcode"/>'+ '</td>';
            html += "</tr>";
        }
        $('#materialTable').empty().append(html);

        var nbqHtml = "";
        for(var i=0; i<nbqList.length; i++){
            nbqHtml += "<tr data-id="+nbqList[i].columns.material_id+">";
            nbqHtml += '<td>' + (i+1) + '</td>';
            nbqHtml += '<td>' + nbqList[i].columns.code + '</td>';
            nbqHtml += '<td>' + nbqList[i].columns.name + '</td>';
            nbqHtml += '<td>' + nbqList[i].columns.spec + '</td>';
            nbqHtml += '<td>' + nbqList[i].columns.material_brand + '</td>';
            nbqHtml += '<td>' + nbqList[i].columns.nbq_sn + '</td>';
            let code = nbqList[i].columns.code;
            nbqHtml += '<td>' + '<input type="text" readonly class="layui-input" id='+code+(i+1)+'  onclick="getChangeBarCode('+'\''+code+'\',\''+code+(i+1)+'\''+')"  name="newNbqSn"/>'+ '</td>';
            nbqHtml += '<td>' + nbqList[i].columns.sn + '</td>';
            nbqHtml += '<td>' + '<input type="text" class="layui-input" name="newSn"/>'+ '</td>';
            if (nbqList[i].columns.material_brand == '固德威' || nbqList[i].columns.material_brand == '爱士惟'){
                nbqHtml += '<td>' + nbqList[i].columns.yz_code + '</td>';
                nbqHtml += '<td>' + '<input type="text"class="layui-input" name="newYzCode"/>'+ '</td>';
            }else {
                nbqHtml += '<td>--</td>';
                nbqHtml += '<td>--</td>';
            }
            nbqHtml += "</tr>";
        }
        $('#nbqTable').empty().append(nbqHtml);

    }

    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>


