// pages/acceptance/acceptance.js
const {
  entryDetail,
  IMG_BASE_URL,
  WU_TU_PIAN_URL,
  toNetAccept,
  getUsedMaterialInfo
} = require('../../../api/api')
Page({
  data: {
    tabIndex:'0',
    childObj:{},//子组件详情数据
    checked:false,//物料安装完成状态是否安装已完成
    otherFiles: [], //其他资料
  },
  onLoad(options) {
    this.setData({
      entryId: options.projectId,
      type: options.type,
    });
    this.entryDetail()
  },
  /** 获取电站详情 **/
  entryDetail(e) {
    entryDetail({
      userId: wx.getStorageSync('userId'),
      entryId: this.data.entryId,
    }).then((res) => {
      this.setData({
        detail: res,
        id: res.id, //修改数据的时候传此ID
        projectId: res.project_id,
        tenant_type: res.tenant_type=='1' ? '个人' : '单位',//1.个人，2单位
        //业主信息参数
        name: res.tenant_name,
        phone: res.mobile,
        idCard: res.id_code,
        regno: res.regno,
        provinceName: res.province,
        cityName: res.city,
        areaName: res.area,
        address: res.address,
        childObj:res,//子组件详情数据
        /*********/
        [`photo.filingDocumentPhoto[0].url`]:res.filing_document_photo ? IMG_BASE_URL + res.filing_document_photo : WU_TU_PIAN_URL,//备案证
        otherFiles: res.report && res.report.otherFiles?res.report.otherFiles.split(","):[]
      });
      const detailObj = JSON.parse(JSON.stringify(this.data))
      this.setData({
        detailObj,
      });
      if(res.management == '04'){//华融
        if (res.projectDetailList[0].componentList.length > 0) {
          this.getUsedMaterialInfo() 
        }
      }else{
        if (res.componentList.length > 0) {
          this.getUsedMaterialInfo() 
        }
      }
    })
  },
  /** 业主信息 **/
  userFn(e){
    wx.navigateTo({
      url: '/home/pages/ownerForm/ownerForm?projectId=' + this.data.entryId,
    })
  },
  /** 拨打业主联系电话 **/
  phoneFn(e) {
    const phone = e.currentTarget.dataset.phone;
    if (!phone) {
      wx.showToast({
        title: '暂无联系方式',
        icon: 'none'
      })
      return
    }
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  /** tab切换 **/
  tabChange(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex:index
    });
  },
  /** 物料安装完成状态勾选 **/
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  /**
   * 提交
   */
  passFn(e) {
    let type  = e.currentTarget.dataset.type, txt = '';
    if(type == '2'){
      if (!this.data.fixConclusionSh) {
        wx.showToast({
          title: '请输入不通过验收意见',
          icon: 'none'
        })
        return
      }
      txt = '不通过成功';
    }else{
      if(!this.data.checked){
        wx.showToast({
          title: '请先勾选物料安装完成状态',
          icon: 'none'
        })
        return
      }
      txt = '通过成功';
    }
    // console.log("执行5", this.data.fixConclusionSh);
    // return
    wx.showLoading({
      title: "提交中",
      mask: true
    });
    toNetAccept({
      id: this.data.entryId,
      type:type,//1.通过 2.驳回
      fixConclusionSh: this.data.fixConclusionSh ? this.data.fixConclusionSh : "",
      materialFixFlag : this.data.checked ? '1' : '0',//物料安装完成状态:0.未完成 1.已完成
      userId: wx.getStorageSync('userId')
    }).then((res) => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: txt,
          icon: 'none',
          success: function () {
            setTimeout(function () {
              wx.navigateBack()
            }, 1500)
          }
        })
      } else {
        wx.showToast({
          title: res.msg || res.errMsg,
          icon: 'none',
        })
      }
    })
  },
  /** 获取使用物料列表 **/
  getUsedMaterialInfo(e) {
    getUsedMaterialInfo({
      entryId: this.data.entryId,
    }).then((res) => {
      var componentList = []; //组件
      var converterList = []; //逆变器
      var cagesList = []; //并网箱
      var supportList = []; //支架
      if (res.code == 200) {
        //华融安装多产品
        if(res.data.projectSettingList){
          this.setData({
            productList: JSON.parse(res.data.projectSettingList)
          })
        }
        res.data.materialEntryList = JSON.parse(res.data.materialEntryList)
        if (res.data.materialEntryList.length > 0) {
          res.data.materialEntryList.forEach(element => {
            if (element.type == "组件") {
              componentList.push(element)
            } else if (element.type == "逆变器") {
              converterList.push(element)
            } else if (element.type == "并网箱") {
              cagesList.push(element)
            } else if (element.type == "支架") {
              supportList.push(element)
            }
          });

          var list = [];
          componentList.forEach(element => {
            var pos = list.findIndex((res) => {
              return res.materialId == element.materialId && res.projectId == element.projectId
            })
            if (pos == -1) {
              element.itemList = [JSON.parse(JSON.stringify(element))]
              list.push(element)
            } else {
              list[pos].itemList.push(element)
            }
          });

          var list1 = [];
          converterList.forEach(element => {
            var pos = list1.findIndex((res) => {
              return res.materialId == element.materialId && res.projectId == element.projectId
            })
            if (pos == -1) {
              element.itemList = [JSON.parse(JSON.stringify(element))]
              list1.push(element)
            } else {
              list1[pos].itemList.push(element)
            }
          });

          this.setData({
            componentList: list,
            converterList: list1,
            cagesList: cagesList,
            supportList: supportList,
            nbqList: res.data.nbqMap?JSON.parse(res.data.nbqMap):[],
            zjMap: res.data.zjMap?JSON.parse(res.data.zjMap):''
            // nbqList: JSON.parse(res.data.nbqMap),
            // zjMap: JSON.parse(res.data.zjMap)
          })
        } else {
          this.setData({
            componentList: [],
            converterList: [],
            cagesList: [],
            supportList: [],
            nbqList: res.data.nbqMap?JSON.parse(res.data.nbqMap):[],
            zjMap: res.data.zjMap?JSON.parse(res.data.zjMap):''
            // nbqList: JSON.parse(res.data.nbqMap),
            // zjMap: JSON.parse(res.data.zjMap)
          })
        }

      }
    })
  },
})