// pages/viewSchema/viewSchema.js

const {
  entryDetail,
  findAllPlan,
  IMG_BASE_URL
} = require('../../../api/api')

Page({

  /**
   * 页面的初始数
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.findAllPlan(options)
  },


  /**
   * 
   * 获取方案类型和方案名称
   * 
   * @param {*} e 
   */
  findAllPlan(options) {
    let that = this
    findAllPlan({

    }).then((res) => {
      for (let i = 0; i < res.length; i++) {
        res[i].allPlan = JSON.parse(res[i].allPlan)
        for (let j = 0; j < res[i].allPlan.length; j++) {
          var element = res[i].allPlan[j].columns;
          res[i].allPlan[j] = element
        }
      }
      that.setData({
        planList: res,
      })
      that.entryDetail(options)
    })
  },
  /**
   * 
   * 获取电站详情
   * 
   * @param {*} e 
   */
  entryDetail(e) {
    entryDetail({
      userId: wx.getStorageSync('userId'),
      entryId: e.projectId,
    }).then((res) => {

      //标准方案,组合方案
      if (res.scheme_type == 1) {
        for (let i = 0; i < this.data.planList.length; i++) {
          const element = this.data.planList[i];
          if (res.scheme_type_id && (res.scheme_type_id == element.id)) {
            this.setData({
              standardSolutionsListIndex: i,
            })
          }
        }
        let standardSolutionsList = [];
        if(res.scheme_id || res.scheme_type_name || res.scheme_type_id || res.scheme_name){
          standardSolutionsList=[{
            id:res.scheme_id,
            type:res.scheme_type_name,
            typeId:res.scheme_type_id,
            name:res.scheme_name,
          }]
        }
        this.setData({
          standardSolutionsList:standardSolutionsList
        })
      } else if (res.scheme_type == 2) {
        if (res.composePlanList.length > 0) {
          var combinedSolutionsList = []
          for (let i = 0; i < res.composePlanList.length; i++) {
            var combinedSolutionsListOB = {}
            const element = res.composePlanList[i];



            // type: "", //方案类型
            // name: "", //方案名称
            // id: "", //方案ID
            // code: "", //方案编号
            // pickerIndex: 0, //方案类型选择下标
            // drawing: "", //参考图纸
            // addType: 1, //显示新增方案按钮
            if (i == 0) {
              combinedSolutionsListOB.addType = 1;
            }
            combinedSolutionsListOB.code = element.code;
            combinedSolutionsListOB.id = element.planId;
            combinedSolutionsListOB.name = element.name;
            combinedSolutionsListOB.type = element.planType;

            for (let i = 0; i < this.data.planList.length; i++) {
              const planTypeObj = this.data.planList[i];

              if (planTypeObj.allPlan.length > 0) {
                for (let j = 0; j < planTypeObj.allPlan.length; j++) {
                  const plan = planTypeObj.allPlan[j];
                  if (plan.id == element.planId) {
                    combinedSolutionsListOB.pickerIndex = i
                  }
                }
              }
            }
            combinedSolutionsList.push(combinedSolutionsListOB)
          }
          this.setData({
            combinedSolutionsList: combinedSolutionsList
          })
        }
      }

      // 组件,逆变器,并网箱
      var componentNum = 0;
      if (res.componentList.length > 0) {
        var componentList = [];
        var converterList = [];
        var cagesList = [];
        var support = [];
        for (let i = 0; i < res.componentList.length; i++) {
          const element = res.componentList[i];
          var OB = {
            id: "",
            num: "", //数量
            model: "", //名称
          }
          if (element.item_type == 3) {
            OB.id = element.item_id;
            OB.num = element.num;
            OB.model = element.model;
            componentNum = element.num * element.model + componentNum
            componentList.push(OB)
          } else if (element.item_type == 1) {
            OB.id = element.item_id;
            OB.num = element.num;
            OB.model = element.model;
            converterList.push(OB)
          } else if (element.item_type == 2) {
            OB.id = element.item_id;
            OB.num = element.num;
            OB.model = element.model;
            cagesList.push(OB)
          } else if (element.item_type == 4) {
            OB.id = element.item_id;
            OB.num = element.num;
            OB.model = element.model;
            support.push(OB)
          }
        }
        this.setData({
          componentList: componentList, //组件
          converterList: converterList, //逆变器
          cagesList: cagesList, //并网箱
          support: support, //支架
        })
      }

      this.setData({
        componentNum: (componentNum/1000).toFixed(3),
        designDrawings: res.design_drawings ? res.design_drawings.split(",").map(itm => {
          return {
            name: itm,
            url: IMG_BASE_URL + itm
          }
        }) : [], //设计图纸
        scheme: res.scheme_type == 1 ? "标准方案" : "组合方案",
        schemeRemarks: res.scheme_remarks ? res.scheme_remarks : "无",
      })

    })
  },

  /**
   * 
   * 查看设计图纸
   * 
   */
  openDocument(res) {
    let path = res.currentTarget.dataset.path
    if (path) {
      let suffix = path.substring(path.lastIndexOf('.') + 1) //获取文件后缀
      if (suffix == 'pdf') {
        wx.showLoading({
          title: '加载中',
        })
        wx.downloadFile({
          url: IMG_BASE_URL + path,
          complete: function (res) {
            wx.hideLoading()
            const filePath = res.tempFilePath
            wx.openDocument({
              filePath: filePath,
              showMenu: true,
              success: function (res) {
                console.log('打开文档成功')
              }
            })
          }
        })
      } else {
        wx.previewImage({
          urls: [IMG_BASE_URL + path] // 需要预览的图片http链接列表
        })
      }
    }
  },
})