// pages/install/install.js
const {
  entryDetail,
  getUsedMaterialInfo,  
  acceptZSRefuse,acceptZS
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    entryId: '',
    remarks: '',
    converterList: [],
    tabIndex:'0',
    childObj:{},//子组件详情数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.entryId = options.projectId;   
    this.getUsedMaterialInfo()
    this.entryDetail()
  },

  onShow() {
    
  },
   /** tab切换 **/
   tabChange(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex:index
    });
  },
  /** 获取电站详情 **/
  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,//子组件详情数据       
      });
      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() 
        }
      }
    })
  },

  /**
   * 
   *  获取使用物料列表
   * 
   * @param {*} e 
   */
  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
            })
            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
            })
            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)
          })
          // console.log(5555, this.data.converterList)
        } 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)
          })
        }

      }
    })    
  },
  
  //保存
  submit(e){
    let that = this
    let type = e.currentTarget.dataset.type
    if(type == '0'){//驳回
      wx.showModal({
        title: '提示',
        content: '确认要驳回吗？',
        complete: (res) => {
          if (res.cancel) {}      
          if (res.confirm) {
            if(that.data.remarks == ''){
              wx.showToast({
                title: '审核意见不能为空',
                icon: 'none'
              })
              return
            }
            acceptZSRefuse({
              userId: wx.getStorageSync('userId'),
              id: that.data.entryId,
              remarks: that.data.remarks
            }).then((res) => {
              wx.hideLoading()
              if (res.code == 200) {
                wx.showToast({
                  title: '操作成功',
                  icon: 'none'
                })
                setTimeout(function () {
                  wx.navigateBack()
                }, 1500)
              } else {
                wx.showToast({
                  title: res.msg || res.errMsg,
                  icon: 'none'
                })
              }
            })            
          }
        }
      })      
    }else{//通过
      acceptZS({
        userId: wx.getStorageSync('userId'),
        id: this.data.entryId
      }).then((res) => {
        wx.hideLoading()
        if (res.code == 200) {
          wx.showToast({
            title: '操作成功',
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateBack()
          }, 1500)
        } else {
          wx.showToast({
            title: res.msg || res.errMsg,
            icon: 'none'
          })
        }
      })
    }
  }
})