<%@ 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/dist/viewer.css" media="all">
  <style>
    html {
      background-color: #fff;
      padding-bottom: 50px;
    }
    .form-span {
      display: block;
      padding: 8px 0;
      margin-left: 110px;
    }

    .page-iframe {
      border: none;
      width: 100%;
      min-height: calc(100vh - 250px);
    }
    .mb-10{
      margin-bottom: 10px !important;
    }
    .pd-10{
      padding: 10px;
    }
    .idea-text{
      width: 320px; height: 100px;outline: none; padding: 3px
    }
    .msg-title{
      text-align: center;
      margin: 10px 0;
      font-size: 16px;
    }
    .title1 {
      border-left: 4px solid #1b6073;
      padding-left: 20px;
      font-size: 14px;
      margin: 10px auto;
      font-weight: 600;
      height: 34px;
      line-height: 34px;
      background-color: #f4f6f9;
      color: #666666;
    }
    .layui-input-block {
      margin-left: 110px;
    }

    .layui-form-label {
      width: 110px;
    }
    .clearfix{*zoom:1;}
    .clearfix:before,.clearfix:after{display:table; line-height:0; content:"";}
    .clearfix:after{clear:both;}

    .layui-tab-title{
      margin: 0 0 0 20px;
    }
    .layui-tab-content{
      margin: 0 0 0 20px;
    }
    .bj-img {
      margin-right: 5px;
    }
    .blank-img {
      background: url('${ctxPlugin}/static/imgs/blankImg.png') center center no-repeat;
      width: 80px;
      height: 80px;
      display: inline-block;
    }
    .link-blue{ color: #1b6073; cursor: pointer;}
    .two-col {
      display: flex;
    }
    .two-col .scheme-item,.component-item {
      padding-right: 20px;
      flex-grow: 1;
      flex-shrink: 1;
      flex-basis: 0;
    }
    .two-col .design-item {
      flex-grow: 1;
      flex-shrink: 1;
      padding-right: 20px;
      flex-basis: 0;
    }
    div.sep {
      border-bottom: 1px dashed #ccc;
      padding-top: 10px;
      clear: both;
      margin-bottom: 10px;
    }
    .two-col .design-doc {
      flex-grow: 1;
      flex-shrink: 1;
      padding-right: 20px;
      flex-basis: 0;
    }
    .design-item-title {
      margin-bottom: 10px;
    }
  </style>
</head>
<body>
<div class="layui-fluid pd-10">
  <div class="order-win layui-form">
    <div class="layui-card-body pt-20 pb-40">
      <div class="layui-col-xs12 form-box">
        <div class="layui-inline layui-col-xs3">
          <label class="layui-form-label w-130">电站编码：</label>
          <div class="layui-input-block ">
            <input type="text" autocomplete="off"  name="stationCode" lay-verify="stationCode" class="layui-input" value="" disabled>
            <input type="text" style="width: 78%;float: left;"  id="stationId" lay-verify="stationCode" class="layui-input hide" value="">
            <a href="#" class="pos-a h-32 lh-32 layui-span-unit pl-10 layui-table-link" onclick="selectEntry('${partnerId}')" style="font-size: 20px">+</a>
          </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"  name="status" class="layui-input" value="" disabled>
          </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" name="traderName" class="layui-input" value="" disabled>
          </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" name="partnerName" class="layui-input" value="" disabled>
          </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" name="tenantName"  class="layui-input" value="" disabled>
          </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" name="idCode" class="layui-input" value="" disabled>
          </div>
        </div>
        <div class="layui-inline layui-col-xs9">
          <label class="layui-form-label w-130">安装地址：</label>
          <div class="layui-input-block ">
            <input type="text" name="address" class="layui-input" value="" disabled>
          </div>
        </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="clear-both">
      <p class="layui-elem-quote" style="margin: 0px 0px 0px 10px">设计方案信息</p>
      <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief" id="designInfoTab">
        <ul class="layui-tab-title" id="designInfoUl"></ul>
      </div>
    </div>
  </div>
</div>
<div class="layui-form-item text-c operate-footer">
  <button type="button" class="layui-btn" id="save">提交</button>
  <button class="layui-btn layui-btn-primary" onclick="closeSelf()">取消</button>
</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/dist/viewer.js"></script>
<script>
  //图片查看效果
  let form = null;
  let entry = null;
  let formSet = null;
  let info2 = null;
  let preComponents = [];
  var viewer = new Viewer(document.getElementById('designInfoTab'));
  layui.config({
    base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'jquery', 'layer','upload','form'], function () {
    $ = layui.$;
    element = layui.element;
    layer = layui.layer;
    upload = layui.upload;
    form = layui.form;

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


    let saving = false;
    $("#save").click(function() {
      if (saving) {
        return;
      }
      var designInfoList = [];
      var projectTrs =$("#projectTab").find("tbody").find('tr');
      let checkFlag = false;
      let entryId = $("#stationId").val();
      if (isBlank(entryId)) {
        layer.msg("未选择电站信息");
        checkFlag = true;
        return false;
      }
      projectTrs.each(function (index) {
        let projectId = $(this).find("td").eq(2).data("id");
        let roofType = $(this).find("td").eq(3).text();

        let preSchemeTypeId = $("#schemeTypeName"+(index+1)).data('id');
        let preSchemeTypeName = $("#schemeTypeName"+(index+1)).text();
        let preSchemeId = $("#schemeName"+(index+1)).data('id');
        let preSchemeName = $("#schemeName"+(index+1)).text();

        let tds = $("#contentBody2"+(index+1)).find("td");
        let schemeTypeSelect = tds.eq(0).find('select');
        let schemeNameSelect = tds.eq(1).find('select');
        let schemeTypeId = schemeTypeSelect.val();
        let schemeTypeName = schemeTypeSelect.find('option:selected').text();
        let schemeId = schemeNameSelect.val();
        let schemeName = schemeNameSelect.find('option:selected').text();

        //变更前
        //设计图纸
        let preDzDocsEl = $('#preDocs'+index).find('input.preDocs'+index);
        let preDzDocs = [];
        preDzDocsEl.each(function() {
          preDzDocs.push($(this).val());
        });
        let preDzDoc = preDzDocs.join(",");
        //组件排布图
        let preZjpbImg = $('#zjpb_img_pre'+index).find('input.zjpb_img_pre'+index);
        let preZjpbImgDocs = [];
        preZjpbImg.each(function() {
          preZjpbImgDocs.push($(this).val());
        });
        let preZjpbImgDoc = preZjpbImgDocs.join(",");
        //组件接线图
        let preZjzxImg = $('#zjzx_img_pre'+index).find('input.zjzx_img_pre'+index);
        let preZjzxImgDocs = [];
        preZjzxImg.each(function() {
          preZjzxImgDocs.push($(this).val());
        });
        let preZjzxImgDoc = preZjzxImgDocs.join(",");
        //组件支架图
        let preZjcsImg = $('#zjcs_img_pre'+index).find('input.zjcs_img_pre'+index);
        let preZjcsImgDocs = [];
        preZjcsImg.each(function() {
          preZjcsImgDocs.push($(this).val());
        });
        let preZjcsImgDoc = preZjcsImgDocs.join(",");
        //一次接线图/电气图
        let preZjhsImg = $('#zjhs_img_pre'+index).find('input.zjhs_img_pre'+index);
        let preZjhsImgDocs = [];
        preZjhsImg.each(function() {
          preZjhsImgDocs.push($(this).val());
        });
        let preZjhsImgDoc = preZjhsImgDocs.join(",");
        //变更后
        let dzDocsEl = $('#postDocs'+index).find('input.postDocs'+index);
        let dzDocs = [];
        dzDocsEl.each(function() {
          dzDocs.push($(this).val());
        });
        let dzDoc = dzDocs.join(",");
        let zjpbImg = $('#zjpb_img_post'+index).find('input.zjpb_img_post'+index);
        let zjpbImgDocs = [];
        zjpbImg.each(function() {
          zjpbImgDocs.push($(this).val());
        });
        let zjpbImgDoc = zjpbImgDocs.join(",");

        let zjzxImg = $('#zjzx_img_post'+index).find('input.zjzx_img_post'+index);
        let zjzxImgDocs = [];
        zjzxImg.each(function() {
          zjzxImgDocs.push($(this).val());
        });
        let zjzxImgDoc = zjzxImgDocs.join(",");
        let zjcsImg = $('#zjcs_img_post'+index).find('input.zjcs_img_post'+index);
        let zjcsImgDocs = [];
        zjcsImg.each(function() {
          zjcsImgDocs.push($(this).val());
        });
        let zjcsImgDoc = zjcsImgDocs.join(",");
        let zjhsImg = $('#zjhs_img_post'+index).find('input.zjhs_img_post'+index);
        let zjhsImgDocs = [];
        zjhsImg.each(function() {
          zjhsImgDocs.push($(this).val());
        });
        let zjhsImgDoc = zjhsImgDocs.join(",");
        let components = [].concat(preComponents);
        let postComponentsTable = $("#postComponentTableContent"+(index+1));
        let postComponents = [];
        postComponentsTable.find('tr').each(function() {
          let tr = $(this);
          let tds = tr.find('td');
          let spec = tds.eq(2).find('option:selected').data("power");
          if(isBlank(spec)){
            spec = tds.eq(2).find('option:selected').text();
            if(spec.includes("|")){
              spec = spec.substring(spec.indexOf("|")+1);
            }
          }
          postComponents.push({
            type: tr.data('type'),
            spec: spec,
            itemId: tds.eq(2).find('select').val(),
            num: $.trim(tds.eq(3).find('input').val()),
            flag: '1'
          });
        });
        let reason = $("#reason").val();

        if (isBlank(schemeTypeId)) {
          layer.msg(roofType+" 请选择方案类型");
          checkFlag = true;
          return false;
        }
        if (isBlank(schemeId)) {
          layer.msg(roofType+" 请选择方案名称");
          checkFlag = true;
          return false;
        }

        for(var i=0;i<postComponents.length;i++) {
          const c = postComponents[i];
          if (c.type === '1' || c.type === '3') {
            if (!c.itemId) {
              layer.msg(roofType+'请选择第'+(i+1)+'行物料规格');
              checkFlag = true;
              return false;
            }
            if (!c.num) {
              layer.msg(roofType+'请输入第'+(i+1)+'行物料数量');
              checkFlag = true;
              return false;
            }
          } else {
            if (c.num && !c.itemId) {
              layer.msg(roofType+'请选择第'+(i+1)+'行物料规格');
              checkFlag = true;
              return false;
            }
            if (c.itemId && !c.num) {
              layer.msg(roofType+'请输入第'+(i+1)+'行物料数量');
              checkFlag = true;
              return false;
            }
          }
          if (c.num && !/^\d+$/.test(c.num)) {
            layer.msg(roofType+'第'+(i+1)+'行物料数量必须为整数');
            checkFlag = true;
            return false;
          }
        }
        for (i = 0; i < formSet.length; i++) {
          let columnKey = formSet[i].columnKey;
          let useFlag = formSet[i].useFlag;
          let mustFillFlag = formSet[i].mustFillFlag;
          if (columnKey.includes(partnerId)) {
            columnKey = columnKey.substring(0, columnKey.indexOf(partnerId) - 1);
          }
          if (useFlag == '1' && columnKey == 'design_drawings' && mustFillFlag == '1'&&!dzDoc) {
            layer.msg( roofType+"请上传1-6个"+ $("designFileTab"+index+" .design_drawings_name").first().text()+"PDF 附件");
            checkFlag = true;
            return false;
          }
          if (useFlag == '1' && columnKey == 'zjpb_img' && mustFillFlag == '1'&&!zjpbImgDoc) {
            layer.msg(roofType+'请上传1-6个'+$("designFileTab"+index+" .zjpb_img_name").first().text()+' 附件');
            checkFlag = true;
            return false;
          }
          if (useFlag == '1' && columnKey == 'zjzx_img' && mustFillFlag == '1'&&!zjzxImgDoc) {
            layer.msg(roofType+'请上传1-6个'+$("designFileTab"+index+" .zjzx_img_name").first().text()+' 附件');
            checkFlag = true;
            return false;
          }
          if (useFlag == '1' && columnKey == 'zjcs_img' && mustFillFlag == '1'&&!zjcsImgDoc) {
            layer.msg(roofType+'请上传1-6个'+$("designFileTab"+index+" .zjcs_img_name").first().text()+' 附件');
            checkFlag = true;
            return false;
          }
          if (useFlag == '1' && columnKey == 'zjhs_img' && mustFillFlag == '1'&&!zjhsImgDoc) {
            layer.msg(roofType+'请上传1-6个'+$("designFileTab"+index+" .zjhs_img_name").first().text()+' 附件');
            checkFlag = true;
            return false;
          }
        }
        postComponents = postComponents.filter(e => !!e.itemId);
        let filter = components.filter(e => e.projectId=== projectId);
        components = filter.concat(postComponents);
        var  designInfo = {
          reason: reason,
          entryId: entryId,
          projectId:projectId,
          components: components,
          schemeType: entry.schemeType,
          preSchemeType:entry.schemeType,
          schemeTypeId: schemeTypeId,
          preSchemeTypeId: preSchemeTypeId,
          schemeTypeName: schemeTypeName,
          preSchemeTypeName:preSchemeTypeName,
          schemeName: schemeName,
          preSchemeName:preSchemeName,
          schemeId: schemeId,
          preSchemeId: preSchemeId,
          dzDoc: dzDoc,
          preDzDoc:preDzDoc,
          zjzxImgDoc: zjzxImgDoc,
          preZjzxImgDoc: preZjzxImgDoc,
          zjhsImgDoc: zjhsImgDoc,
          preZjhsImgDoc: preZjhsImgDoc,
          zjcsImgDoc: zjcsImgDoc,
          preZjcsImgDoc: preZjcsImgDoc,
          zjpbImgDoc: zjpbImgDoc,
          preZjpbImgDoc: preZjpbImgDoc
        }
        designInfoList.push( designInfo);
      });
      if (checkFlag){
        return false;
      }
      saving = true;
      // schemeType不会变，只有一种标准方案，没有组合方案
      var index= layer.load(2, {shade: [0.5,'#fff'], content:''});
      $.ajax({
        url: "${ctx}/entryChanges/designInfo/addDesignChangeHR",
        type: "POST",
        contentType : "application/json;charsetset=UTF-8",
        data: JSON.stringify(designInfoList),
        success: function (data) {
          if (data.code === "200") {
            top.layer.msg("操作成功");
            parent.searchOrder();
            closeSelf();
          } else {
            saving = false;
            layer.msg(data.errMsg);
          }
        },
        error: function () {
          saving = false;
        },
        complete:function (){
          layer.close(index);
        }
      });
    });

    form.on('select(planType)', function (data) {
      var value = $(data.elem).find("option:selected").val();
      var that_ = $(this);
      fillPlanNameSelect(value,that_);
    });

    form.on('select(spec)', function (data) {
      doCalcPostDesignCapacity();
    });
  });

  function fillPlanNameSelect(value, that_) {
    if (isBlank(value)) {
      return
    }
    //获取当前类型下的方案名称
    $.ajax({
      url: "${ctx}/planManage/findPlanByAssembly",
      type: "POST",
      data: {
        planType: value
      },
      success: function (result) {
        that_.parents(".planbiao").find(".planName").empty();
        var length = result.length;
        var plan_name_html = "<option value=''>请选择</option>";
        for (var i = 0; i < length; i++) {
          plan_name_html += "<option value='" + result[i].columns.id + "' title='" + result[i].columns.assembly_count + "' date='" + result[i].columns.code + "' data-attach='" + result[i].columns.attach + "' data-attach2='" + result[i].columns.attach2 + "'>" + result[i].columns.name + "</option>";
        }
        that_.parents(".planbiao").find(".planName").append(plan_name_html);
        form.render('select');
      }
    });
  }

  function selectEntry(partnerId) {
    layer.open({
      type: 2,
      title: '选择电站',
      content: '${ctx}/entryChanges/designInfo/toSelectEntry?partnerId='+partnerId,
      area: ['80%','80%']
    });
  }

  function setPreDesignDocs(entryDesigns) {
    for (let a = 0; a <entryDesigns.length; a++) {
      if(!isBlank(entryDesigns[a].designDrawings)){
        $("#preDocs"+a).empty();
        let designDrawings = entryDesigns[a].designDrawings;
        let drawings = designDrawings.split(",");
        for(var i=0;i<drawings.length;i++) {
          let s = $(preDesignDocTempl('preDocs'+a));
          let file = s.find('a').eq(0);
          file.attr('href', '${commonStaticImgPath}'+drawings[i]);
          file.attr('download', drawings[i]);
          file.text(filename(drawings[i]));
          $("#preDocs"+a).append(s);
          s.find('input').val(drawings[i]);
        }
      }
      if(!isBlank(entryDesigns[a].zjpbImg)){
        $("#zjpb_img_pre"+a).empty();
        let zjpbImgs = entryDesigns[a].zjpbImg;
        let zjpbImg = zjpbImgs.split(",");
        for(var i=0;i<zjpbImg.length;i++) {
          let s = $(preDesignDocTempl('zjpb_img_pre'+a));
          let file = s.find('a').eq(0);
          file.attr('href', '${commonStaticImgPath}'+zjpbImg[i]);
          file.attr('download', zjpbImg[i]);
          file.text(filename(zjpbImg[i]));
          $("#zjpb_img_pre"+a).append(s);
          s.find('input').val(zjpbImg[i]);
        }
      }
      if(!isBlank(entryDesigns[a].zjzxImg)){
        $("#zjzx_img_pre"+a).empty();
        let zjzxImgs = entryDesigns[a].zjzxImg;
        let zjzxImg = zjzxImgs.split(",");
        for(var i=0;i<zjzxImg.length;i++) {
          let s = $(preDesignDocTempl('zjzx_img_pre'+a));
          let file = s.find('a').eq(0);
          file.attr('href', '${commonStaticImgPath}'+zjzxImg[i]);
          file.attr('download', zjzxImg[i]);
          file.text(filename(zjzxImg[i]));
          $("#zjzx_img_pre").append(s);
          s.find('input').val(zjzxImg[i]);
        }
      }
      if(!isBlank(entryDesigns[a].zjcsImg)){
        $("#zjcs_img_pre"+a).empty();
        let zjcsImgs = entryDesigns[a].zjcsImg;
        let zjcsImg = zjcsImgs.split(",");
        for(var i=0;i<zjcsImg.length;i++) {
          let s = $(preDesignDocTempl('zjcs_img_pre'+a));
          let file = s.find('a').eq(0);
          file.attr('href', '${commonStaticImgPath}'+zjcsImg[i]);
          file.attr('download', zjcsImg[i]);
          file.text(filename(zjcsImg[i]));
          $("#zjcs_img_pre").append(s);
          s.find('input').val(zjcsImg[i]);
        }
      }
      if(!isBlank( entryDesigns[a].zjhsImg)){
        $("#zjhs_img_pre"+a).empty();
        let zjhsImgs = entryDesigns[a].zjhsImg;
        let zjhsImg = zjhsImgs.split(",");
        for(var i=0;i<zjhsImg.length;i++) {
          let s = $(preDesignDocTempl('zjhs_img_pre'+a));
          let file = s.find('a').eq(0);
          file.attr('href', '${commonStaticImgPath}'+zjhsImg[i]);
          file.attr('download', zjhsImg[i]);
          file.text(filename(zjhsImg[i]));
          $("#zjhs_img_pre").append(s);
          s.find('input').val(zjhsImg[i]);
        }
      }

    }
  }

  function filename(filePath) {
    if (!filePath) {
      return '';
    }
    let index = filePath.lastIndexOf("/");
    if (index !== -1) {
      return filePath.substring(index+1);
    }
    return filePath;
  }

  function preDesignDocTempl(index) {
    return "<div style=\"margin-top: 5px\">\n" +
            "    <a href=\"\" class=\"download-link\"></a>\n" +
            "    <input type=\"hidden\" class=\""+index+"\">\n" +
            "</div>";
  }

  function postDesignDocTempl(index) {
    return "<div style=\"margin-top: 5px\">\n" +
            "    <span class=\"layui-icon-close layui-icon\" onclick=\"delDoc(this)\"></span>\n" +
            "    <a href=\"\" class=\"download-link\"></a>\n" +
            "    <input type=\"hidden\" class=\""+index+"\">\n" +
            "</div>";
  }

  function setPostDesignDocs(entryDesigns) {
    for (var a = 0; a < entryDesigns.length; a++) {
      if(!isBlank(entryDesigns[a].designDrawings)){
        $("#postDocs"+a).empty();
        let designDrawings = entryDesigns[a].designDrawings;
        let drawings = designDrawings.split(",");
        for(var i=0;i<drawings.length;i++) {
          appendSingleDoc(drawings[i],'postDocs'+a);
        }
      }
      if(!isBlank(entryDesigns[a].zjpbImg)){
        $("#zjpb_img_post"+a).empty();
        let zjpbImgs = entryDesigns[a].zjpbImg;
        let zjpbImg = zjpbImgs.split(",");
        for(var i=0;i<zjpbImg.length;i++) {
          appendSingleDoc(zjpbImg[i],'zjpb_img_post'+a);
        }
      }
      if(!isBlank(entryDesigns[a].zjzxImg)){
        $("#zjzx_img_post"+a).empty();
        let zjzxImgs = entryDesigns[a].zjzxImg;
        let zjzxImg = zjzxImgs.split(",");
        for(var i=0;i<zjzxImg.length;i++) {
          appendSingleDoc(zjzxImg[i],'zjzx_img_post'+a);
        }
      }
      if(!isBlank(entryDesigns[a].zjcsImg)){
        $("#zjcs_img_post"+a).empty();
        let zjcsImgs = entryDesigns[a].zjcsImg;
        let zjcsImg = zjcsImgs.split(",");
        for(var i=0;i<zjcsImg.length;i++) {
          appendSingleDoc(zjcsImg[i],'zjcs_img_post'+a);
        }
      }
      if(!isBlank( entryDesigns[a].zjhsImg)){
        $("#zjhs_img_post"+a).empty();
        let zjhsImgs = entryDesigns[a].zjhsImg;
        let zjhsImg = zjhsImgs.split(",");
        for(var i=0;i<zjhsImg.length;i++) {
          appendSingleDoc(zjhsImg[i],'zjhs_img_post'+a);
        }
      }
    }

  }

  function delDoc(el) {
    $(el).closest('div').remove();
  }

  function appendSingleDoc(path, index) {
    let s = $(postDesignDocTempl(index));
    let file = s.find('a').eq(0);
    file.attr('href', '${commonStaticImgPath}'+path);
    file.attr('download', path);
    file.text(filename(path));
    $("#"+index).append(s);
    s.find('input').val(path);
  }

  function calcPostDesignCapacity() {
    doCalcPostDesignCapacity();
  }

  function doCalcPostDesignCapacity(r) {
    let tbody = $(r).closest('tr').parent();

    let tds = tbody.find('tr').eq(0).find('td');
    // let tds = $("#postComponentTableContent").find('tr').eq(0).find('td');
    const num = tds.eq(3).find('input').val();
    var value = tds.eq(2).find("option:selected").data('power');
    const capacity = parseInt(value) * parseInt(num);
    tbody.parent().next('div').find('span').text(isNaN(capacity) ? "-" : (capacity/1000).toFixed(3));
    // $('#postDesignCapacity').text(isNaN(capacity) ? "-" : (capacity/1000).toFixed(3));
  }

  function addRow(r) {
    let target = $(r).closest('tr');
    const nextRow = target.clone();
    nextRow.data('type', target.data('type'));
    function _resetRow(nextRow) {
      let tds = nextRow.find('td');
      tds.eq(2).find('select').val('');
      tds.eq(3).find('input').val('');
      tds.eq(4).find('i.layui-icon-delete').removeClass('layui-hide');
    }
    _resetRow(nextRow);
    nextRow.insertAfter(target);
    // initRowNums($('#postComponentTableContent'));
    initRowNums(target.parent());
    invalidate('select');
  }

  function deleteRow(r) {
    let target = $(r).closest('tr');
    let parent = target.parent();
    target.remove();
    // initRowNums($('#postComponentTableContent'));
    initRowNums(parent);
  }

  function invalidate(target) {
    setTimeout(function() {
      form.render(target);
    }, 0);
  }

  function initRowNums(table) {
    let trs = table.find('tr');
    trs.each(function(index, el) {
      $("td:first", $(this)).text(index+1);
    });
  }

  function compChange(r) {
    doCalcPostDesignCapacity(r);
  }

  function searchCode(code){
    $("input[name='stationCode']").val(code);
    var stationCode = code;
    if (isBlank(stationCode)) {
      layer.msg('请输入电站编码')
      return;
    }
    $.ajax({
      type: 'post',
      url: '${ctx}/entryChanges/designInfo/selectNumber/' + stationCode,
      dataType: 'json',
      data: {
        stationCode: stationCode
      },
      success: function(res) {
        clearHtml();
        if (res.code === "200") {
          var customerEntry = res.data.customerEntry;
          entry = customerEntry;
          formSet = res.data.formSet;
          info2 = res.data.info2;
          partnerId = res.data.customerEntry.partnerId;
          var projectList = res.data.projectList;
          var entryDesigns = res.data.entryDesigns;

          var sysAssemblyList = res.data.sysAssemblyList;
          var sysInverterList = res.data.sysInverterList;
          var sysGridConnectionList = res.data.sysGridConnectionList;
          var sysGridSupportList = res.data.sysGridSupportList;


          var assemblyList = res.data.assemblyList;
          let inverterList = res.data.inverterList;
          let gridConnectionList = res.data.gridConnectionList;
          let gridSupportList = res.data.gridSupportList;

          let list = [];//所有物料
          list = list.concat(assemblyList);
          list = list.concat(inverterList);
          list = list.concat(gridConnectionList);
          list = list.concat(gridSupportList);

          var insta = res.data.insta;

          $("#stationId").val(customerEntry.id);
          $("input[name='status']").val(getStatus(customerEntry));
          $("input[name='traderName']").val(customerEntry.createTraderName);
          $("#projectCompanyName").text(customerEntry.projectCompanyName);
          $("input[name='partnerName']").val(customerEntry.partnerName);
          $("input[name='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);
          $("input[name='idCode']").val(customerEntry.idCode);
          $("input[name='address']").val(customerEntry.province + customerEntry.city + customerEntry.area + customerEntry.address);
          let html = "";
          // 产品信息
          let index = 1;
          for (let i = 0; i < projectList.length; i++) {
            let projectId = projectList[i].columns.projectId;
            let projectCode = projectList[i].columns.projectCode;
            let projectName = projectList[i].columns.projectName;
            let roofType = projectList[i].columns.roofType;
            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>';
            index++;
          }
          $("#projectList").empty();
          $("#projectList").append(html);

          let html1 = "";
          for (let i = 0; i <projectList.length; i++) {
            let projectCode = projectList[i].columns.projectCode;
            let roofType = projectList[i].columns.roofType;
            let projectName = projectList[i].columns.projectName;
            if (i == 0){
              html1 += '<li class="layui-this" id="'+projectCode+'">';
            }else {
              html1 += '<li id="'+projectCode+'">';
            }
            if (!isBlank(roofType)){
              html1 += roofType;
            }else {
              html1 += projectName;
            }
            html1 += '</li>';
          }
          $("#designInfoUl").empty();
          $("#designInfoUl").append(html1);

          let html2 = "";
          let j = 1;
          for (let i = 0; i < entryDesigns.length; i++) {
            let schemeTypeId = entryDesigns[i].schemeTypeId;
            let schemeTypeName = entryDesigns[i].schemeTypeName;
            let schemeName = entryDesigns[i].schemeName;
            let schemeId = entryDesigns[i].schemeId;
            let projectId = entryDesigns[i].projectId;
            let preDesignCapacity = entryDesigns[i].designCapacity / 1000;

            if (i== 0){
              html2 += '<div class="layui-tab-content designTabContent " id="designImgDiv'+j+'">';
            }else {
              html2 += '<div class="layui-tab-content designTabContent layui-hide " id="designImgDiv'+j+'">';
            }
            html2+=`<div class="layui-tab-content two-col" style="padding: 0">
                      <div class="scheme-item">
                        <div>变更前方案：</div>
                        <table class="layui-table">
                          <thead>
                          <th>方案类型</th>
                          <th>方案名称</th>
                          </thead>
                        <tbody id="contentBody`+j+`">
                          <tr>
                            <td data-id="`+schemeTypeId+`" id="schemeTypeName`+j+`">`+schemeTypeName+`</td>
                            <td data-id="`+schemeId+`" id="schemeName`+j+`">`+schemeName+`</td>
                          </tr>
                          </tbody>
                        </table>
                      </div>
                      <div class="scheme-item">
                        <div>变更后方案：</div>
                        <table class="layui-table">
                          <thead>
                          <th>方案类型</th>
                          <th>方案名称</th>
                          </thead>
                          <tbody id="contentBody2`+j+`">
                          <tr class="planbiao">
                            <td>
                              <select lay-filter="planType" name="planType" class="planType" lay-search>
                                <option value="">请选择</option>`;
                                for (let a = 0; a < insta.length; a++) {
                                  if (insta[a].columns.id == schemeTypeId){
                                    html2+='<option value="'+insta[a].columns.id+'" selected>'+insta[a].columns.name+'</option>';
                                  }else {
                                    html2+='<option value="'+insta[a].columns.id+'">'+insta[a].columns.name+'</option>';
                                  }
                                }
               html2+=`</select>
                            </td>
                            <td>
                              <select lay-filter="planName" name="planName" class="planName" lay-search>
                                <option value="">请选择</option>`;
            html2+='<option  selected value="'+schemeId+'">'+schemeName+'</option>';
                       html2+=`</select>
                            </td>
                          </tr>
                          </tbody>
                        </table>
                      </div>
                    </div>`;
            html2 += '<div class="sep"></div>';

            html2 += `<div class="layui-tab-content  two-col" style="padding: 0">
                        <div class="component-item">
                      <div>变更前物料：</div>
                      <table class="layui-table" id="preComponentTable`+j+`">
                        <thead>
                        <th>序号</th>
                        <th>物料类型</th>
                        <th width="50%">物料规格</th>
                        <th>数量</th>
                        </thead>
                        <tbody id="preComponentTableContent`+j+`">`;
                        let index  = 1;
                        for (let b = 0; b <list.length; b++) {
                          if (list[b].columns.project_id == projectId){
                            html2 += '<tr>';
                            html2 += '<td>'+index+'</td>';
                            html2 += '<td>'+list[b].columns.label+'</td>';
                            if (list[i].columns.item_type === '3') {
                              if(info2.columns.component3_model!=null&&info2.columns.component3_model.status == '1'){
                                html2 += '<td>'+list[b].columns.material_model+'Wp</td>';
                              }else {
                                html2 += '<td>'+list[b].columns.model+'Wp</td>';
                              }
                            } else if(list[i].columns.item_type === '1'){
                              if(info2.columns.component1_model!=null&&info2.columns.component1_model.status == '1') {
                                html2 += '<td>'+list[b].columns.material_model+'</td>';
                              }else {
                                html2 += '<td>'+list[b].columns.model+'</td>';
                              }
                            }else {
                              html2 += '<td>'+list[b].columns.model+'</td>';
                            }
                            html2 += '<td>'+list[b].columns.num+'</td>';
                            html2 += '</tr>';
                            index++;
                          }
                        }
            html2+=`</tbody>
                      </table>
                      <div>变更前设计安装容量：<span id="preDesignCapacity`+j+`">`+preDesignCapacity+`</span>kW</div>
                    </div>
                    <div class="component-item">
                      <div>变更后物料：</div>
                      <table class="layui-table">
                        <thead>
                        <th width="50">序号</th>
                        <th width="100">物料类型</th>
                        <th>物料规格</th>
                        <th width="80">数量</th>
                        <th width="70">操作</th>
                        </thead>
                        <tbody id="postComponentTableContent`+j+`">`;
                        let index2  = 1;
                        let nbqNum = 1;
                        for (let b = 0; b <list.length; b++) {
                          if (list[b].columns.project_id == projectId){
                            html2 += '<tr data-type="'+list[b].columns.item_type+'">';
                            html2 += '<td>'+index2+'</td>';
                            html2 += '<td><i class="mark">*</i>'+list[b].columns.label+'</td>';
                            if (list[b].columns.item_type === '3') {
                              //组件
                              html2+= `<td>
                                           <select lay-filter="spec" name="spec" lay-search>
                                                <option value="">请选择</option>`;
                              for (let c = 0; c < sysAssemblyList.length; c++) {
                                if (projectId == sysAssemblyList[c].columns.projectId){
                                  if (info2.columns.component3_model!=null&&info2.columns.component3_model.status == '1'){
                                    if (list[b].columns.item_id == sysAssemblyList[c].columns.id) {
                                      html2+= `<option data-power="`+sysAssemblyList[c].columns.model+`" selected value="`+sysAssemblyList[c].columns.id+`">`+sysAssemblyList[c].columns.material_model+"|"+sysAssemblyList[c].columns.model+`</option>`;
                                    }else {
                                      html2+= `<option data-power="`+sysAssemblyList[c].columns.model+`"  value="`+sysAssemblyList[c].columns.id+`">`+sysAssemblyList[c].columns.material_model+"|"+sysAssemblyList[c].columns.model+`</option>`;
                                    }
                                  }else {
                                    if (list[b].columns.item_id == sysAssemblyList[c].columns.id) {
                                      html2+= `<option data-power="`+sysAssemblyList[c].columns.model+`"  selected value="`+sysAssemblyList[c].columns.id+`">`+sysAssemblyList[c].columns.model+`</option>`;
                                    }else {
                                      html2+= `<option data-power="`+sysAssemblyList[c].columns.model+`"  value="`+sysAssemblyList[c].columns.id+`">`+sysAssemblyList[c].columns.model+`</option>`;
                                    }
                                  }
                                }
                              }
                              html2+=`</select>
                                           <span class="pos-a layui-span-unit" style="right: 30px;top:10px;">Wp</span></td>`;
                            } else if(list[b].columns.item_type === '1'){
                              //逆变器
                              html2+= `<td>
                                           <select lay-filter="spec" name="inverter" lay-search>
                                                <option value="">请选择</option>`;
                              for (let c = 0; c < sysInverterList.length; c++) {
                                if(info2.columns.component1_model!=null&&info2.columns.component1_model.status == '1') {
                                  if (list[b].columns.item_id == sysInverterList[c].columns.id) {
                                    html2+= `<option selected value="`+sysInverterList[c].columns.id+`">`+sysInverterList[c].columns.material_model+'|'+sysInverterList[c].columns.model+`</option>`;
                                  }else {
                                    html2 += `<option value="` + sysInverterList[c].columns.id + `">` + sysInverterList[c].columns.material_model +'|'+sysInverterList[c].columns.model+ `</option>`;
                                  }
                                }else {
                                  if (list[b].columns.item_id == sysInverterList[c].columns.id) {
                                    html2+= `<option selected value="`+sysInverterList[c].columns.id+`">`+sysInverterList[c].columns.model+`</option>`;
                                  }else {
                                    html2 += `<option value="` + sysInverterList[c].columns.id + `">` + sysInverterList[c].columns.model + `</option>`;
                                  }
                                }
                              }
                              html2+=`</select></td>`;
                            }

                            if (list[b].columns.item_type === '3') {
                              html2+= '<td><input type="text" name="num" oninput="compChange(this)" class="layui-input" value="'+list[b].columns.num+'"></td>';
                            }else {
                              html2 += '<td><input type="text" name="num" class="layui-input" value="'+list[b].columns.num+'"></td>';
                            }

                            if (list[b].columns.item_type === '1') {
                              if (nbqNum == 1){
                                html2 += `<td>
                                          <i class="layui-icon layui-icon-add-circle" style="color: #1b6073;font-size: 25px;"onclick="addRow(this)"></i>
                                          <i class="layui-icon layui-icon-delete layui-hide" style="color: #1b6073;font-size: 25px;" onclick="deleteRow(this)"></i>
                                      </td>`;
                              }else {
                                html2 += `<td>
                                          <i class="layui-icon layui-icon-add-circle" style="color: #1b6073;font-size: 25px;"onclick="addRow(this)"></i>
                                          <i class="layui-icon layui-icon-delete " style="color: #1b6073;font-size: 25px;" onclick="deleteRow(this)"></i>
                                      </td>`;
                              }
                              nbqNum++;
                            }else {
                              html2 +=`<td></td>`
                            }
                            html2 += '</tr>';
                            index2++;
                          }
                        }
            html2+=`</tbody>
                      </table>
                      <div>变更后设计安装容量：<span id="postDesignCapacity`+j+`">`+preDesignCapacity+`</span>kW</div>
                    </div>
                  </div>`;

            html2 += '<div class="sep"></div>';

            html2 += `<div class="layui-tab-content two-col" style="padding: 0;margin-top: 5px;" id="designFileTab`+i+`">
                        <div class="design-doc">
                          <div class="design_drawings change hide">
                            变更前<i class="design_drawings_name">设计图纸：</i>
                            <div id="preDocs`+i+`">

                            </div>
                          </div>
                          <div class="zjpb_img change hide">
                            变更前<i class="zjpb_img_name">组件排布图：</i>
                            <div id="zjpb_img_pre`+i+`">

                            </div>
                          </div>
                          <div  class="zjzx_img change hide">
                            变更前<i class="zjzx_img_name">组件接线图：</i>
                            <div id="zjzx_img_pre`+i+`">

                            </div>
                          </div>
                          <div  class="zjcs_img change hide">
                            变更前<i class="zjcs_img_name">组件支架图：</i>
                            <div id="zjcs_img_pre`+i+`">

                            </div>
                          </div>
                          <div  class="zjhs_img change hide">
                            变更前<i class="zjhs_img_name">一次接线图/电气图：</i>
                            <div id="zjhs_img_pre`+i+`">

                            </div>
                          </div>
                        </div>
                        <div class="design-doc">
                          <div class="design_drawings change hide">
                            变更后<i class="design_drawings_name">设计图纸：</i><span style="color:blue;cursor: pointer"  id="attachFilesUploader`+i+`">上传</span>
                            <div style="color:#aaa;font-size:12px;" id="design_drawings_reson"></div>
                            <div id="postDocs`+i+`">

                            </div>
                          </div>

                          <div class="zjpb_img change hide">
                            变更后<i class="zjpb_img_name">组件排布图：</i><span style="color:blue;cursor: pointer" id="zjpbImgUploader`+i+`">上传</span>
                            <div style="color:#aaa;font-size:12px;" id="zjpb_img_reson"></div>
                            <div id="zjpb_img_post`+i+`">

                            </div>
                          </div>
                          <div  class=" zjzx_img change hide">
                            变更后<i class="zjzx_img_name">组件接线图：</i><span style="color:blue;cursor: pointer" id="zjzxImgUploader`+i+`">上传</span>
                            <div style="color:#aaa;font-size:12px;" id="zjzx_img_reson"></div>
                            <div id="zjzx_img_post`+i+`">

                            </div>
                          </div>
                          <div  class=" zjcs_img change hide">
                            变更后<i class="zjcs_img_name">组件支架图：</i><span style="color:blue;cursor: pointer" id="zjcsImgUploader`+i+`">上传</span>
                            <div style="color:#aaa;font-size:12px;" id="zjcs_img_reson"></div>
                            <div id="zjcs_img_post`+i+`">

                            </div>
                          </div>
                          <div  class=" zjhs_img  change hide">
                            变更后<i class="zjhs_img_name">一次接线图/电气图：</i><span style="color:blue;cursor: pointer" id="zjhsImgUploader`+i+`">上传</span>
                            <div style="color:#aaa;font-size:12px;" id="zjhs_img_reson"></div>
                            <div id="zjhs_img_post`+i+`">

                            </div>
                          </div>
                        </div>
                      </div>`;




            html2+='</div>'
            j++;
          }
          $("#designInfoTab").append(html2);
          form.render('select');
          // 保存物料 pre
          savePreComponentInfo(res.data.assemblyList, res.data.inverterList, res.data.gridConnectionList, res.data.gridSupportList);
          // 设置设计文档和设计图纸
          setPreDesignDocs(entryDesigns);
          setPostDesignDocs(entryDesigns);
          calcPostDesignCapacity();
          hideOrShowHtml(formSet,partnerId,entryDesigns);
          initUploadComponent();
        } else {
          clearHtml();
          layer.msg(res.errMsg);
        }
      }
    });
  }

  function initUploadComponent(){
    for (let i = 0; i < 5; i++) {
      upload.render({
        elem: '#attachFilesUploader'+i,
        url: '${ctx}/common/uploadFile3',
        accept: 'file',
        exts: 'pdf|dwg',
        before: function (obj) {
          //预读本地文件示例，不支持ie8
          let size = $('#postDocs'+i).find('a').size();
          if (size >= 6) {
            layer.msg('最多只能上传6个文件')
            return false;
          }
        },
        done: function (res) {
          //上传完毕回调
          let imgCount = $('#postDocs'+i).find('a').size();
          if (imgCount >= 6) {
            return;
          }
          appendSingleDoc(res.path,'postDocs'+i);
        },
        error: function () {
          //请求异常回调
        }
      });
      upload.render({
        elem: '#zjhsImgUploader'+i,
        url: '${ctx}/common/uploadFile3',
        accept: 'file',
        exts: 'jpg|png|gif|doc|docx|ppt|pdf|xls|xlsx',
        before: function (obj) {
          //预读本地文件示例，不支持ie8
          let size = $('#zjhs_img_post'+i).find('a').size();
          if (size >= 6) {
            layer.msg('最多只能上传6个文件')
            return false;
          }
        },
        done: function (res) {
          //上传完毕回调
          let imgCount = $('#zjhs_img_post'+i).find('a').size();
          if (imgCount >= 6) {
            return;
          }
          appendSingleDoc(res.path,'zjhs_img_post'+i);
        },
        error: function () {
          //请求异常回调
        }
      });
      upload.render({
        elem: '#zjpbImgUploader'+i,
        url: '${ctx}/common/uploadFile3',
        accept: 'file',
        exts: 'jpg|png|gif|doc|docx|ppt|pdf|xls|xlsx',
        before: function (obj) {
          //预读本地文件示例，不支持ie8
          let size = $('#zjpb_img_post'+i).find('a').size();
          if (size >= 6) {
            layer.msg('最多只能上传6个文件')
            return false;
          }
        },
        done: function (res) {
          //上传完毕回调
          let imgCount = $('#zjpb_img_post'+i).find('a').size();
          if (imgCount >= 6) {
            return;
          }
          appendSingleDoc(res.path,'zjpb_img_post'+i);
        },
        error: function () {
          //请求异常回调
        }
      });
      upload.render({
        elem: '#zjzxImgUploader'+i,
        url: '${ctx}/common/uploadFile3',
        accept: 'file',
        exts: 'jpg|png|gif|doc|docx|ppt|pdf|xls|xlsx',
        before: function (obj) {
          //预读本地文件示例，不支持ie8
          let size = $('#zjzx_img_post'+i).find('a').size();
          if (size >= 6) {
            layer.msg('最多只能上传6个文件')
            return false;
          }
        },
        done: function (res) {
          //上传完毕回调
          let imgCount = $('#zjzx_img_post'+i).find('a').size();
          if (imgCount >= 6) {
            return;
          }
          appendSingleDoc(res.path,'zjzx_img_post'+i);
        },
        error: function () {
          //请求异常回调
        }
      });
      upload.render({
        elem: '#zjcsImgUploader'+i,
        url: '${ctx}/common/uploadFile3',
        accept: 'file',
        exts: 'jpg|png|gif|doc|docx|ppt|pdf|xls|xlsx',
        before: function (obj) {
          //预读本地文件示例，不支持ie8
          let size = $('#zjcs_img_post'+i).find('a').size();
          if (size >= 6) {
            layer.msg('最多只能上传6个文件')
            return false;
          }
        },
        done: function (res) {
          //上传完毕回调
          let imgCount = $('#zjcs_img_post'+i).find('a').size();
          if (imgCount >= 6) {
            return;
          }
          appendSingleDoc(res.path,'zjcs_img_post'+i);
        },
        error: function () {
          //请求异常回调
        }
      });
    }
  }

  function hideOrShowHtml(setData,partnerId,entryDesigns) {
    for (var i = 0; i < setData.length; i++) {
      let columnKey = setData[i].columnKey;
      if (setData[i].useFlag == '0') {//不启用
        for (var j = 0; j < entryDesigns.length; j++) {
          $("#designFileTab"+j+"  ." + columnKey).addClass("hide");
        }
      } else {
        if (columnKey.includes(partnerId)) {
          columnKey = columnKey.substring(0, columnKey.indexOf(partnerId)-1);
        }
        for (var j = 0; j < entryDesigns.length; j++) {
          $("#designFileTab"+j+"  ." + columnKey).removeClass("hide");
          $("#designFileTab"+j+"  ." + columnKey+'_name').text(setData[i].columnName);
          $("#designFileTab"+j+"  #" + columnKey+'_reson').text(setData[i].remarks);
        }
      }
    }
    resetSort();
  }


  function addImg(data, classimg, count) {
    let icons = $('#postDzImgsDiv').find('.imgSpan').size();
    const uploader = $('#dzImgsUploader');
    if(icons+1>=count){
      uploader.hide();
    }
    var html = '<div class="bj-img imgSpan"><span class="layui-icon-close layui-icon" onclick="deleteIcon(this)"></span>';
    html += '<img src="${commonStaticImgPath}' + data + '" height="700" width="1200"/>';
    html += '<input type="hidden" class="' + classimg + '" value="' + data + '"/> </div>';
    $(html).insertBefore(uploader);
    //图片查看效果
    viewer.update();
  }

  function setDzImgs(data) {
    const imgItem = "<div class=\"bj-img imgSpan\">\n" +
            "    <img src=\"\" height=\"700\" width=\"1200\"/>\n" +
            "    <input type=\"hidden\" name=\"preDzImgs\" class=\"dzImgs\" value=\"\">\n" +
            "</div>";
    const imgs = data.dzImgs.split(',');
    const imgContainer = $('#preDzImgsDiv');
    imgContainer.empty();
    for (var i=0;i<imgs.length;i++) {
      const imgItemEl = $(imgItem);
      imgItemEl.find('img').attr('src', "${commonStaticImgPath}"+imgs[i]);
      imgItemEl.find('input').val(imgs[i]);
      imgContainer.append(imgItemEl);
    }
  }

  function deleteIcon(r) {
    const uploader = $('#dzImgsUploader');
    $(r).closest('.imgSpan').remove();
    let icons = $('#postDzImgsDiv').find('.imgSpan').size();
    if (icons >= 4) {
      uploader.hide();
    } else {
      uploader.show();
    }
  }

  function setDzImgsPost(data) {
    const imgItem = "<div class=\"bj-img imgSpan\">\n" +
            "    <span class=\"layui-icon-close layui-icon\" onclick=\"deleteIcon(this)\"></span>\n" +
            "    <img src=\"\" height=\"700\" width=\"1200\"/>\n" +
            "    <input type=\"hidden\" name=\"postDzImgs\" class=\"dzImgs\" value=\"\">\n" +
            "</div>";
    const imgs = data.dzImgs.split(',');
    const imgContainer = $('#postDzImgsDiv');
    $('.imgSpan', imgContainer).remove();
    const uploader = $('#dzImgsUploader');
    for (var i=0;i<imgs.length;i++) {
      const imgItemEl = $(imgItem);
      imgItemEl.find('img').attr('src', "${commonStaticImgPath}"+imgs[i]);
      imgItemEl.find('input').val(imgs[i]);
      imgItemEl.insertBefore(uploader);
    }
    if (imgs.length >= 4) {
      uploader.hide();
    }
  }


  function savePreComponentInfo(assemblyList, inverterList, gridConnectionList, gridSupportList) {
    let ret = []
    let list = [];
    list = list.concat(assemblyList);
    list = list.concat(inverterList);
    list = list.concat(gridConnectionList);
    list = list.concat(gridSupportList);
    for (var i=0;i<list.length;i++) {
      let el = list[i].columns;
      let item = {
        type: el.item_type,
        spec: el.model,
        itemId: el.item_id,
        num: $.trim(el.num),
        projectId:el.project_id,
        flag: '0'
      }
      ret.push(item);
    }
    preComponents = ret;
  }

  function clearHtml(){
    $("#stationId").val("");
    $("input[name='status']").val("");
    $("input[name='traderName']").val("");
    $("#projectCompanyName").text("");
    $("input[name='tenantName']").val("");
    $("input[name='partnerName']").val("");
    $("#filingType").text("");
    $("#productLine").text("");
    $("input[name='idCode']").val("");
    $("input[name='address']").val("");
    for (let i = 0; i <5; i++) {
      $("#preDocs"+i).empty();
      $("#zjpb_img_pre"+i).empty();
      $("#zjzx_img_pre"+i).empty();
      $("#zjzx_img_pre"+i).empty();
      $("#zjhs_img_pre"+i).empty();
      $("#postDocs"+i).empty();
      $("#zjzx_img_post"+i).empty();
      $("#zjpb_img_post"+i).empty();
      $("#zjhs_img_post"+i).empty();
      $("#zjcs_img_post"+i).empty();
    }
    $(".change").addClass("hide");
    $("#designInfoTab").empty();
    let designInfoUlHtml = `<ul class="layui-tab-title" id="designInfoUl"></ul>`;
    $("#designInfoTab").append(designInfoUlHtml);
  }


  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 '安装自审';
    }
  }

  window.closeSelf = function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);//关闭当前页
  }

  function isBlank(val) {
    if (val == null || val == '' || val == undefined) {
      return true;
    }
    return false;
  }
  function resetSort(){
    var sort = 1;
    $("#contentBody").find("tr").each(function (index) {
      if (!$(this).hasClass("hide")) {
        $(this).find("td:first").text(sort);
        sort++;
      }
    });
  }
</script>
</body>
</html>

