// pages/infoRegistration/infoRegistration.js
const {
  ajaxForWaitOrder,
  ajaxForWaitOrder2,
  projectList,
  getRectificationList,
  rectificationAudit,
  findRetrialPage,
  bankCardPage,
  toContractPage,
  toAuthContrac,
  getContractUrlToLook,
  getContractUrlToLookHr,
  getSignContractUrl,
  doSealContract,
  IMG_BASE_URL,
  ecordClickSign,
  findResult,
  getEsignUrl,
  getAddressLonLat,
  resetMobileVsAuthPc,
  customerManagementStatic,
  lookAuthBook,insertInterFace,cancelBankCard,addUserInfo,
  backToSurvey,huaRongContractPage,createContract,batchAdmit
} = require("../../../api/api");
const {
  checkPermission
} = require("../../../utils/permission");
const app = getApp()
const { routeLink } = require("../../../utils/route.js");
Page({
  data: {
    IMG_BASE_URL: IMG_BASE_URL,
    bgHeight: 0, //背景图片高度    

    statusBarHeight: app.globalData.statusBarHeight, //状态栏高度
    navHeight: app.globalData.navHeight, //导航栏高度
    windowWidth: app.globalData.windowWidth, //可使用窗口高度，单位px
    menuButtonLeft: app.globalData.menuButtonLeft, //菜单按钮距离左边距离
    keyWords: '', //关键字
    showFilter: false, //筛选弹框显示按钮

    secCardStatus: 0, //开二类卡，默认tab待开卡
    signStatus: 0, //合同签署，默认tab待签署

    status: [{
      name: '暂存',
      type: "0",
      select: false
    }, {
      name: '待审核',
      type: "1",
      select: false
    }, {
      name: '审核不通过',
      type: "2",
      select: false
    }, {
      name: '待下单',
      type: "3",
      select: false
    }, {
      name: '待安装',
      type: "4",
      select: false
    }, {
      name: '待安装验收',
      type: "5",
      select: false
    }, {
      name: '安装验收退回',
      type: "f",
      select: false
    }, {
      name: '待并网',
      type: "6",
      select: false
    }, {
      name: '待上传发票',
      type: "d",
      select: false
    }, {
      name: '待签署合同',
      type: "e",
      select: false
    }, {
      name: '待并网验收',
      type: "7",
      select: false
    }, {
      name: '并网验收驳回',
      type: "10",
      select: false
    }, {
      name: '已完成',
      type: "a",
      select: false
    }, {
      name: '已删除',
      type: "b",
      select: false
    }, {
      name: '准入登记',
      type: "w",
      select: false
    }],
    statusData: [],
    searchData: [],
    pageNum: 1, //分页页面
    pageSize: 10, //分页条数
    loadEnd: false, //加载到最底部
    navTitle: '',
    //信息授权
    authStatus:'0',
    //客户管理
    inforStatus:'0',
    creditStatus:[
      {
        name: '暂存',
        value: "0",
        select: true
      }, {
        name: '待预审',
        value: "1",
        select: true
      }, {
        name: '预审中',
        value: "2,3",
        select: true
      }, {
        name: '预审驳回',
        value: "5",
        select: true
      }, 
      // {
      //   name: '预审通过',
      //   value: "4",
      //   select: true
      // }
    ],
    backToSurveShow:false,//撤回勘测对话框
    backToSurveReson:'',//撤回勘测原因
  },
  onLoad(options) {
    console.log('type',options.type)
    this.setData({
      type: options.type, //存贮状态l
      // userType: wx.getStorageSync('userType') == "TRADER" ? 0 : 1, //0:代理商 1:平台
      userType: wx.getStorageSync('userType'),
      traderType: wx.getStorageSync('traderType'),//代理商等级
      traderId:wx.getStorageSync('traderId') ? wx.getStorageSync('traderId') : '-1',//代理商登录的traderId
      PTpermission:wx.getStorageSync('PTpermission'),//设计审核的权限值：HYXCX_SJSH_B；安装验收的权限值：HYXCX_ANYS_B；并网验收的权限值：HYXCX_BWYS_B
      permission: wx.getStorageSync('permission'),
      partnerId: wx.getStorageSync('capitalId'), //资方
      // signStatus: wx.getStorageSync('capitalId') == '04' ? 'f' : 0
    })

    wx.setNavigationBarTitle({
      title: options.name,
    });
    this.setData({
      credit:options.type==1 ? "0,1,2,3,5" : '',
      authorization:options.type==2 ? "2" : '',
      navTitle: options.name
    });
  },
  onShow() {
    if(this.data.type == 1 || this.data.type == 2){
      this.tabCount();
    }
    this.setData({
      pageNum:1,
    });
    // wx.pageScrollTo({
    //   scrollTop: 0,
    // })
    if (this.data.type == 15) {
      this.findRetrialPage()
    } else {
      this.ajaxForWaitOrder()
    }
    // this.getProjectList()
    this.getRectificationList()
  },
  /** 获取tab统计 **/
  tabCount(){
    customerManagementStatic({
      userId:wx.getStorageSync('userId'),
      traderId: wx.getStorageSync('userType')=='PLATFORM_EMP'?wx.getStorageSync('traderId'):'',
      partner: wx.getStorageSync('capitalId'), //资方
    }).then(res=>{
      this.setData({
        tabCount:res.data.columns
      });
    });
  },
  /** 下拉刷新 **/
  onPullDownRefresh(e) {
    this.setData({
      pageNum: 1,
      pageSize: 10,
      loadEnd: false,
    })

    if (this.data.type == 15) {
      this.findRetrialPage()
    } else {
      this.ajaxForWaitOrder()
    }

  },
  /** 页面触底上拉刷新 **/
  onReachBottom(e) {
    if (!this.data.loadEnd) {
      if (this.data.type == 15) {
        this.findRetrialPage()
      } else {
        this.ajaxForWaitOrder()
      }
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 500
      })
    }
  },
  /** 获取电站列表 **/
  ajaxForWaitOrder(e) {
    var status = "";
    var credit = "";
    var authorization = "";
    var signStatus = "";//农户合同
    var secCardStatus = "";
    var designAuditFlag = "";//电站初勘 设计审核
    var orgFlag = "";//1.电站档案 2.档案审核 6.接入逆变器
    var ifStateGridStatus = ""; //国网授权状态（0:未授权 1:授权中 2:已授权）
    var onGridFlag = ""; //并网状态 0.未开始 1.待并网 2.并网验收驳回   3.资方审核驳回 4.已并网
    if (this.data.type == 1) { //客户管理
      status = "0";
      credit = this.data.credit;
    } else if (this.data.type == 2) { //信息授权
      status = "0";
      authorization = this.data.authorization;
      if(authorization =='3'){//已授权
        status = "";
      }
      // credit = "1";
    } else if (this.data.type == 3) { //电站初勘
      status = "0,2"
      credit = "4";
      designAuditFlag = "1,3,7,a";
    } else if (this.data.type == 4) { //开二类卡
      if (this.data.secCardStatus == 0) {
        secCardStatus = "0,3,4"; // 待开卡
        credit = "4";
      } else if (this.data.secCardStatus == 1) {
        secCardStatus = "1"; // 已开卡
        credit = "4";
      }
    } else if (this.data.type == 5) { //农户合同      
      if (this.data.signStatus == 0) {
        if(this.data.partnerId == '03'){//广发
          signStatus = "1,6,b,h,i,j,k,c"; //广发资方，待签约（6.待生成; 1.待签约; b.签署中; i.已撤销; k.已拒签; j.已过期; h.作废;c.待盖章）
        }else if(this.data.partnerId == '04'){//华融待签约
          signStatus = "1,2,f,6" // 待签约
        }else{
          signStatus = "1,4" // 待签约
        }
      } else if (this.data.signStatus == '1') {// 已签约        
        if(this.data.partnerId == '03'){
          signStatus = "c,d,e"; //广发资方，已签约（c.待盖章;d.盖章中;e.已完成）
        }else if(this.data.partnerId == '04'){
          signStatus = "5,g"; // 华融已签约
        }else{
          signStatus = "2,3,5"; // 越秀
        }
      }else {
        // signStatus = this.data.signStatus //华融资方 f:待上传 g：已上传
      }
    } else if (this.data.type == 6) { //设计审核
      status = "1"
      designAuditFlag = "2,4,5,7,8";
    } else if (this.data.type == 7) { //物料下单
      status = "3"
    } else if (this.data.type == 8) { //电站安装
      status = "4"
      // status = "4"
    } else if (this.data.type == 9) { //安装验收
      status = "5"
    }else if (this.data.type == 20) { //接入逆变器
      status = "6,10"
      orgFlag = '6'
    } else if (this.data.type == 11) { //合同签署
      status = "e"
    } else if (this.data.type == 12) { //并网验收
      status = "7"
    } else if (this.data.type == 13) { //项目归档
      status = "a"
    } else if (this.data.type == 14) { //项目查询
      status = this.data.statusData.length > 0 ? this.data.statusData + "" : ""
    } else if (this.data.type == 15) { //电站整改
      status = "2"
    } else if (this.data.type == 16) { //电站并网
      status = "6,10"
    } else if (this.data.type == 17) { //电站档案
      orgFlag = "1"
    }else if (this.data.type == 18) { //档案审核
      orgFlag = "2"
    }else if (this.data.type == 19) { //国网授权
      status = ""
      ifStateGridStatus = "0"
      onGridFlag = "0,1,2,3"
    }
    wx.showLoading({
      title: '加载中',
    })
    //农户合同特殊接口
    if(this.data.type == '5'){      
    ajaxForWaitOrder2({
      userId: wx.getStorageSync('userId'),
      traderId: wx.getStorageSync('userType')=='PLATFORM_EMP'?wx.getStorageSync('traderId'):'',
      keyword: this.data.searchData.length > 0 ? this.data.searchData + "" : "",
      // status: status,
      credit: credit,
      // authorization: authorization,
      signStatus: signStatus,
      secCardStatus: secCardStatus,
      orgFlag:orgFlag,
      ifStateGridStatus: ifStateGridStatus,
      // onGridFlag: onGridFlag,
      // designAuditFlag:designAuditFlag,
      page: this.data.pageNum,
      limit: this.data.pageSize,
      partner: wx.getStorageSync('capitalId'), //资方
    }).then((res) => {
      wx.hideLoading()
      wx.stopPullDownRefresh()
      if (res.code == 0) {        
        this.data.loadEnd = res.data.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.rows = res.data
        } else {
          this.data.rows = this.data.rows.concat(res.data)
        }
        if (!this.data.loadEnd) {
          this.data.pageNum++;
        }

        this.data.rows.forEach(item => {
          item.isSign = 0
          item.showMobile = item.mobile && item.mobile.indexOf('*') == -1 //判断是否显示手机号码
        })

        this.setData({
          list: this.data.rows,
          showFilter: false,
          loadEnd: this.data.loadEnd,
        })
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }

    })
  }else{
    ajaxForWaitOrder({
      userId: wx.getStorageSync('userId'),
      traderId: wx.getStorageSync('userType')=='PLATFORM_EMP'?wx.getStorageSync('traderId'):'',
      keyword: this.data.searchData.length > 0 ? this.data.searchData + "" : "",
      status: status,
      credit: credit,
      authorization: authorization,
      signStatus: signStatus,
      secCardStatus: secCardStatus,
      orgFlag:orgFlag,
      ifStateGridStatus: ifStateGridStatus,
      onGridFlag: onGridFlag,
      designAuditFlag:designAuditFlag,
      page: this.data.pageNum,
      limit: this.data.pageSize,
      partner: wx.getStorageSync('capitalId'), //资方
    }).then((res) => {
      wx.hideLoading()
      wx.stopPullDownRefresh()
      if (res.code == 200) {
        this.data.loadEnd = res.data.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.rows = res.data
        } else {
          this.data.rows = this.data.rows.concat(res.data)
        }
        if (!this.data.loadEnd) {
          this.data.pageNum++;
        }

        this.data.rows.forEach(item => {
          item.isSign = 0
          item.showMobile = item.mobile && item.mobile.indexOf('*') == -1 //判断是否显示手机号码
        })

        this.setData({
          list: this.data.rows,
          showFilter: false,
          loadEnd: this.data.loadEnd,
        })
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }

    })
  }
  },
  /** 获取电站整改 **/
  findRetrialPage(e) {
    findRetrialPage({
      userId: wx.getStorageSync('userId'),
      page: this.data.pageNum,
      limit: this.data.pageSize,
    }).then((res) => {
      wx.hideLoading()
      wx.stopPullDownRefresh()
      if (res.code == 200) {
        this.data.loadEnd = res.data.length < this.data.pageSize
        if (this.data.pageNum == 1) {
          this.data.rows = res.data
        } else {
          this.data.rows = this.data.rows.concat(res.data)
        }
        if (!this.data.loadEnd) {
          this.data.pageNum++;
        }

        this.data.rows.forEach(item => {
          item.showMobile = item.mobile && item.mobile.indexOf('*') == -1 //判断是否显示手机号码
        })

        this.setData({
          list: this.data.rows,
          loadEnd: this.data.loadEnd,
        })

      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    })
  },
  /** 获取产品编号 **/
  getProjectList(e) {
    projectList({
      userId: wx.getStorageSync('userId'),
      traderId: wx.getStorageSync('userType')=='PLATFORM_EMP'?wx.getStorageSync('traderId'):''
    }).then((res) => {
      res.forEach(element => {
        element.select = false
      });
      this.setData({
        projectList: res
      })
    })
  },
  /** 搜索 **/
  keyWordsSearch(res) {
    this.setData({
      searchData: [],
      statusData: [],
    })
    const search = res.detail; //搜索内容    
    // this.data.status.forEach(element => {
    //   element.select = false
    // });
    // this.data.projectList.forEach(element => {
    //   element.select = false
    // });

    this.setData({
      searchData: this.data.searchData.concat(search),
      pageNum:1
    })
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.ajaxForWaitOrder()
  },
  /** 筛选 **/
  bindFilter() {
    this.setData({
      showFilter: true
    });
  },
  /** 关闭筛选 **/
  closeFilter(e) {
    this.setData({
      showFilter: false
    })
  },
  /** 筛选 **/
  filtrate() {
    let type = this.data.type;
    //客户管理
    if(type == 1){
      let credit = [];
      this.data.creditStatus.forEach(itm=>{
        if(itm.select){
          credit.push(itm.value);
        }
      });
      credit = credit.join(",");
      console.log("credit",credit);
      this.setData({
        credit,
        pageNum:1
      });
      this.ajaxForWaitOrder()
      return;
    }
    ///
    this.setData({
      searchData: [],
      statusData: [],
    })

    var data = [];
    var statusData = []
    this.data.projectList.forEach(element => { //产品编号
      if (element.select) {
        data.push(element.code)
      }
    });
    this.data.status.forEach(element => { //状态
      if (element.select) {
        statusData.push(element.type)
      }
    });

    this.setData({
      searchData: data,
      statusData: statusData,
      pageNum:1
    })
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.ajaxForWaitOrder()
  },
  /** 点击筛选图标 **/
  onCheckItem(event) {
    //当前操作的数组名
    var arrName = event.currentTarget.dataset.arrName
    //当前操作的数组中的index
    var current = event.currentTarget.dataset.index
    this.setData({
      [`${arrName}[${current}].select`]: !this.data[arrName][current].select
    })
  },
  /** 编辑操作 **/
  async bindEdit(res) {
    //检验权限
    let perm = res.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    let navtitle =  res.currentTarget.dataset.navtitle, type = res.currentTarget.dataset.type, partnerId = res.currentTarget.dataset.partnerid;
    if(type == '3'){ //勘测
      if(partnerId == '04'){//华融
        wx.navigateTo({
          url: '/home/pages/surveyFormHr/surveyFormHr?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type + "&surveyType=" + res.currentTarget.dataset.surveytype + "&navTitle=" + navtitle,
        })
      }else{
        wx.navigateTo({
          url: '/home/pages/surveyForm/surveyForm?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type + "&surveyType=" + res.currentTarget.dataset.surveytype + "&navTitle=" + navtitle,
        })
      }
    }else if(type == '17'){//档案
      wx.navigateTo({
        url: '/home/pages/filesForm/filesForm?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type + "&navTitle=" + navtitle,
      })
    }else{//信息编辑
      wx.navigateTo({
        url: '/home/pages/entryInfoBT/entryInfoBT?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type + "&surveyType=" + res.currentTarget.dataset.surveytype + "&navTitle=" + navtitle,
      })
    }
  },
  /** 完工操作 **/
  async bindFinish(res) {
    //检验权限
    let perm = res.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    if (!res.currentTarget.dataset.isused > 0) {
      wx.showToast({
        title: '请先安装电站',
        icon: 'none'
      })
      return
    }
    if(res.currentTarget.dataset.partnerid == '04'){   //华融 
      wx.navigateTo({
        url: '/home/pages/completionHR/completionHR?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type,
      })
    }else{
      wx.navigateTo({
        url: '/home/pages/completion/completion?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type,
      })
    }
  },
  /** 安装操作 **/
  async bindInstall(res) {
    //检验权限
    let perm = res.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    if (res.currentTarget.dataset.dispatchstatus == 0) {
      wx.showToast({
        title: '先派工才可安装',
        icon: 'none'
      })
      return
    }
    if(res.currentTarget.dataset.partnerid == '04'){//华融
      wx.navigateTo({
        url: '/home/pages/installHR/installHR?projectId=' + res.currentTarget.dataset.projectid
      })
    }else{
      wx.navigateTo({
        url: '/home/pages/install/install?projectId=' + res.currentTarget.dataset.projectid
      })
    }
  },
  /** 安装自审操作 **/
  async bindSelfAudit(res) {
    //检验权限
    let perm = res.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.navigateTo({
      url: '/home/pages/installSelfAudit/installSelfAudit?projectId=' + res.currentTarget.dataset.projectid
    })
  },
  /** 并网操作 **/
  async bindGridConnection(res) {
    //检验权限
    let perm = res.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.navigateTo({
      url: '/home/pages/bwForm/bwForm?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type,
    })
  },
  /** 设计审核 **/
  audit(res) {
    let partnerId = res.currentTarget.dataset.partnerid;
    if(partnerId == '04'){//华融
      wx.navigateTo({
        url: '/home/pages/entryInfoAuditHR/entryInfoAuditHR?projectId=' + res.currentTarget.dataset.projectid + '&type=' + this.data.type,
      })
    }else{
      wx.navigateTo({
        url: '/home/pages/entryInfoAudit/entryInfoAudit?projectId=' + res.currentTarget.dataset.projectid + '&type=' + this.data.type,
      })
    }
  },
  /** 设计自审 **/
  designAudit(res) {
    let partnerId = res.currentTarget.dataset.partnerid
    if(partnerId == '04'){//华融
      wx.navigateTo({
        url: '/home/pages/surveyFormHr/surveyFormHr?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type + "&surveyType=" + res.currentTarget.dataset.surveytype + "&navTitle=" + res.currentTarget.dataset.navtitle,
      })
    }else{
      wx.navigateTo({
        url: '/home/pages/surveyForm/surveyForm?projectId=' + res.currentTarget.dataset.projectid + "&type=" + res.currentTarget.dataset.type + "&surveyType=" + res.currentTarget.dataset.surveytype + "&navTitle=" + res.currentTarget.dataset.navtitle,
      })
    }   
  },
  /** 档案审核 **/
  auditFiles(res) {
    wx.navigateTo({
      url: '/home/pages/filesForm/filesForm?projectId=' + res.currentTarget.dataset.projectid + "&type=" + this.data.type + "&navTitle=" + '档案审核',
    })
  },
  /** 点击列表 **/
  details(e) {
    // console.log(66, this.data.type)
    if(this.data.type =='1' || this.data.type =='3' || this.data.type =='6' || this.data.type =='8' || this.data.type =='12' || this.data.type =='14' || this.data.type =='16' || this.data.type =='9' || this.data.type =='17' || this.data.type =='18'){//客户详情
      wx.navigateTo({
        url: '/home/pages/customDetails/customDetails?projectId=' + e.currentTarget.dataset.projectid + "&type=" + this.data.type,
      })
    }else{
      wx.navigateTo({
        url: '/home/pages/customDetails/customDetails?projectId=' + e.currentTarget.dataset.projectid + "&type=" + this.data.type,
        // url: '/home/pages/entryInfoDetails/entryInfoDetails?projectId=' + e.currentTarget.dataset.projectid + "&type=" + this.data.type,
      })      
    }
    
  },
  /** 复制电站编码 **/
  copy(e) {
    const code = e.currentTarget.dataset.code;
    wx.setClipboardData({
      data: code,
      success(res) {
        wx.showToast({
          title: '电站编码已复制',
          icon: 'none'
        })
      }
    })
  },
  /** 查看方案 **/
  watch(e) {
    wx.navigateTo({
      url: '/home/pages/viewSchema/viewSchema?projectId=' + e.currentTarget.dataset.projectid,
    })
  },
  /** 电站整改 **/
  rectifyPop(e) {
    const type = e.currentTarget.dataset.type
    const changeReason = e.currentTarget.dataset.changereason
    const projectId = e.currentTarget.dataset.projectid
    this.setData({
      rectifyShow: type == 0 ? false : true,
      changeReason: changeReason ? changeReason : "",
      projectId: projectId,
      changeTypeName: "",
      changeTypeOverName: "",
      rectificationId: "",
    })
  },
  /** 验收报告 **/
  acceptance(e) {
    wx.navigateTo({
      url: '/home/pages/acceptance/acceptance?projectId=' + e.currentTarget.dataset.projectid,
    })
  },
  /** 联系租户 **/
  phone(e) {
    const phone = e.currentTarget.dataset.phone;
    if (!phone) {
      wx.showToast({
        title: '暂无租户联系方式',
        icon: 'none'
      })
      return
    }
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  /** 变更类型 **/
  getRectificationList(e) {
    getRectificationList({

    }).then((res) => {
      if (res.code == 200) {
        var list = []
        if(res.data){
          res.data.forEach(element => {
            list.push(element.columns)
          });
        }
        this.setData({
          getRectificationList: list
        })
      }
    })
  },
  /** 点击变更类型 **/
  bindgetRectificationList(e) {
    const index = e.detail.value;
    const changeType = this.data.getRectificationList[index].status
    const changeTypeName = this.data.getRectificationList[index].name
    const rectificationId = this.data.getRectificationList[index].id
    var changeTypeOverName
    switch (changeType) {
      case "0":
        changeTypeOverName = "暂存"
        break
      case "1":
        changeTypeOverName = "设计审核"
        break
      case "2":
        changeTypeOverName = "待安装"
        break
      case "3":
        changeTypeOverName = "待安装验收"
        break
      case "4":
        changeTypeOverName = "待并网"
        break
      case "5":
        changeTypeOverName = "待上传发票"
        break
      case "6":
        changeTypeOverName = "待签署合同"
        break
      case "7":
        changeTypeOverName = "待并网验收"
        break
    }

    this.setData({
      changeTypeName: changeTypeName,
      changeTypeOverName: changeTypeOverName,
      rectificationId: rectificationId,
    })

  },
  /** 电站整改审核 **/
  rectify(e) {
    if (!this.data.rectificationId) {
      wx.showToast({
        title: "请选择变更类型",
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    rectificationAudit({
      id: this.data.projectId,
      type: 1,
      userId: wx.getStorageSync('userId'),
      auditComments: this.data.auditComments,
      rectificationId: this.data.rectificationId
    }).then((res) => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: '确定审核',
          icon: 'none',
          duration: 2000,
          success: function () {
            this.setData({
              rectifyShow: false,
              changeTypeName: "",
              changeTypeOverName: "",
              rectificationId: "",
              changeReason: ""
            })
          }
        })
      } else {
        wx.showToast({
          title: res.msg || res.errMsg,
          icon: "none"
        })
      }
    })
  },
  /** 开卡 **/
  async bankCardPage(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    bankCardPage({
      entryId: e.currentTarget.dataset.projectid
    }).then((resApi) => {
      wx.hideLoading()
      console.log("开卡web", resApi)
      if (resApi.code == 200) {
        wx.navigateTo({
          url: '../webView/webView',
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', {
              data: resApi.data.gdUrl
            })
          }
        })
      } else {
        wx.showToast({
          title: resApi.errMsg,
          icon: 'none'
        })
      }
    })
  },
  /** 签约 
   * 00签约  01共签
   * **/
  async toContractPage(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    const type = e.currentTarget.dataset.type
    const ifclicksign = e.currentTarget.dataset.ifclicksign
    const format = e.currentTarget.dataset.format  //link复制地址

    if (type == "01" && ifclicksign != 1) {
      wx.showToast({
        title: '请先签约',
        icon: "none"
      })
      return
    }

    wx.showLoading({
      title: '加载中',
    })
    toContractPage({
      entryId: e.currentTarget.dataset.projectid,
      type: type,
    }).then((resApi) => {
      wx.hideLoading()
      if (resApi.code == 200) {
        if (type == '00') {          
          ecordClickSign({
            entryId: e.currentTarget.dataset.projectid,
          }).then((res) => {
            if (res.code == 200) {
              if(format == 'link'){
                wx.setClipboardData({
                  data: resApi.data.contractUrl,
                  success(res) {
                    wx.showToast({
                      title: '链接已复制',
                      icon: 'none'
                    })
                  }
                });
                this.ajaxForWaitOrder()
              }else{
                wx.navigateTo({
                  url: '../webView/webView',
                  success: function (res) {
                    // 通过eventChannel向被打开页面传送数据
                    res.eventChannel.emit('acceptDataFromOpenerPage', {
                      data: resApi.data.contractUrl
                    })
                  }
                })
              }
            } else {
              wx.showToast({
                title: res.errMsg,
                icon: 'none'
              })
            }
          })
        } else {
          if(format == 'link'){
            wx.setClipboardData({
              data: resApi.data.contractUrl,
              success(res) {
                wx.showToast({
                  title: '链接已复制',
                  icon: 'none'
                })
              }
            });
            this.ajaxForWaitOrder()
          }else{
            wx.navigateTo({
              url: '../webView/webView',
              success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', {
                  data: resApi.data.contractUrl
                })
              }
            })
          }
        }
      } else {
        wx.showToast({
          title: resApi.errMsg,
          icon: 'none'
        })
      }
    })
  },

  /** 华融签约 
   * 00签约  01共签
   * **/
  async huaRongContractPage(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    const type = e.currentTarget.dataset.type
    const ifclicksign = e.currentTarget.dataset.ifclicksign
    const format = e.currentTarget.dataset.format  //link复制地址

    // if (type == "01" && ifclicksign != 1) {
    //   wx.showToast({
    //     title: '请先签约',
    //     icon: "none"
    //   })
    //   return
    // }
    wx.showLoading({
      title: '加载中',
    })
    huaRongContractPage({
      entryId: e.currentTarget.dataset.projectid     
    }).then((resApi) => {
      wx.hideLoading()
      if (resApi.code == 200) {        
        if(format == 'link'){
          wx.setClipboardData({
            data: resApi.data.contractUrl,
            success(res) {
              wx.showToast({
                title: '链接已复制',
                icon: 'none'
              })
            }
          });
          this.ajaxForWaitOrder()
        }else{
          wx.navigateTo({
            url: '../webView/webView',
            success: function (res) {
              // 通过eventChannel向被打开页面传送数据
              res.eventChannel.emit('acceptDataFromOpenerPage', {
                data: resApi.data.contractUrl
              })
            }
          })
        }
      } else {
        wx.showToast({
          title: resApi.errMsg,
          icon: 'none'
        })
      }
    })
  },
  /** 华融发起签约 **/
  async huaRongContractAdd(e) {
    let that = this
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showModal({
      title: '',
      content: '您确定要发起合同吗？',
      complete: (res) => {
        if (res.cancel) {}    
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
          })
          createContract({
            userId: wx.getStorageSync('userId'),
            entryId: e.currentTarget.dataset.projectid
          }).then((resApi) => {
            wx.hideLoading()
            if (resApi.code == 200) {        
              wx.showToast({
                title: '操作成功',
                icon: 'none'
              })
              that.setData({
                pageNum: 1,
                rows: [],
                list: [],
              });
              wx.pageScrollTo({
                scrollTop: 0,
              })
              that.ajaxForWaitOrder();
            } else {
              wx.showToast({
                title: resApi.errMsg,
                icon: 'none'
              })
            }
          })          
        }
      }
    })    
  },
  /** 授权 **/
  async toAuthContrac(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    toAuthContrac({
      entryId: e.currentTarget.dataset.projectid
    }).then((res) => {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: '授权成功',
          icon: "none"
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  /** 查看农户合同 **/
  async getContractUrlToLook(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    var that=this
    if(this.data.partnerId == '04'){
      getContractUrlToLookHr({
        entryId: e.currentTarget.dataset.projectid
      }).then((resApi) => {
        if (resApi.code == 200) {
          //需要拼接图片前缀
          let fileUrl = that.data.IMG_BASE_URL + resApi.data.url;
          let suffix = resApi.data.url.substring(resApi.data.url.lastIndexOf('.') + 1) //获取文件后缀
          if (suffix.search(/jpg|png|jpeg/) == -1) {
            wx.downloadFile({ //将文档下载到本地
              url: fileUrl,//pdf链接
              success(res) {
                wx.openDocument({ //打开文档
                  filePath: res.tempFilePath,//本地文档路径
                  fileType: "pdf",//文档类型
                  showMenu: true,
                  success: function (res) {
                    wx.hideLoading()
                  },
                  fail: function (res) {
                    wx.showToast({
                      title: '打开文档失败',
                    })
                  },
                })
              },
            });
          }else{
            wx.previewImage({
              urls: [fileUrl] // 需要预览的图片http链接列表
            })
          }
        } else {
          wx.hideLoading()
          wx.showToast({
            title: resApi.errMsg,
            icon: 'none'
          })
        }
      })
      return
    }
    
    getContractUrlToLook({
      entryId: e.currentTarget.dataset.projectid
    }).then((resApi) => {
      if (resApi.code == 200) {
        if(e.currentTarget.dataset.str == 'yulan'){
          // wx.setClipboardData({
          //   data: res.data,
          //   success(res) {
          //     wx.showToast({
          //       title: '链接已复制',
          //       icon: 'none'
          //     })
          //   }
          // });
          wx.navigateTo({
            url: '../webView/webView',
            success: function (res2) {
              // 通过eventChannel向被打开页面传送数据
              res2.eventChannel.emit('acceptDataFromOpenerPage', {
                data: resApi.data
              })
            }
          })
        }else{
          //需要拼接图片前缀
          let fileUrl = that.data.IMG_BASE_URL + resApi.data.url;
          let suffix = resApi.data.url.substring(resApi.data.url.lastIndexOf('.') + 1) //获取文件后缀
          if (suffix.search(/jpg|png|jpeg/) == -1) {
            wx.downloadFile({ //将文档下载到本地
              url: fileUrl,//pdf链接
              success(res) {
                wx.openDocument({ //打开文档
                  filePath: res.tempFilePath,//本地文档路径
                  fileType: "pdf",//文档类型
                  showMenu: true,
                  success: function (res) {
                    wx.hideLoading()
                  },
                  fail: function (res) {
                    wx.showToast({
                      title: '打开文档失败',
                    })
                  },
                })
              },
            });
          }else{
            wx.previewImage({
              urls: [fileUrl] // 需要预览的图片http链接列表
            })
          }
        }
      } else {
        wx.hideLoading()
        wx.showToast({
          title: resApi.errMsg,
          icon: 'none'
        })
      }
    })
  },
  /** 跳转上传农户合同页面 **/
  async uploadContract(e){
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    routeLink("/home/pages/uploadContract/uploadContract?projectId=" + e.currentTarget.dataset.projectid);
  },
  /** 生成合同-按钮 **/
  async contractScFn(e){
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    getSignContractUrl({entryId: e.currentTarget.dataset.projectid}).then(ret=>{
      wx.hideLoading()
      if (ret.code == 200) {
        wx.navigateTo({
          url: '../webView/webView',
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', {
              data: ret.data.contractUrl
            })
          }
        })
      } else {
        wx.showToast({
          title: ret.errMsg,
          icon: 'none'
        })
      }
    });
  },
  /** 合同落章-按钮 **/
  async contractLzFn(e){
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    doSealContract({entryId: e.currentTarget.dataset.projectid}).then(res=>{
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "none",
          duration: 2000
        })
        this.setData({
          pageNum: 1,
          rows: [],
          list: [],
        });
        wx.pageScrollTo({
          scrollTop: 0,
        })
        this.ajaxForWaitOrder();
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    });
  },
  /** 开二类卡tab **/
  changeSecCardStatus(e) {
    const index = e.currentTarget.dataset.index
    if (index == this.data.secCardStatus) {
      return
    }
    this.setData({
      pageNum: 1,
      rows: [],
      list: [],
      secCardStatus: this.data.secCardStatus == 0 ? 1 : 0
    })
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.ajaxForWaitOrder()
  },
  /** 合同签署tab **/
  changeSignStatus(e) {
    const index = e.currentTarget.dataset.index
    if (index == this.data.signStatus) {
      return
    }
    this.setData({
      pageNum: 1,
      rows: [],
      list: [],
      signStatus: index
    })
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.ajaxForWaitOrder()
  },
  /** 预审重新提交 **/
  async ysReSubmit(e){
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    let id = e.currentTarget.dataset.projectid;
    batchAdmit({
      userId: wx.getStorageSync('userId'),
      entryId:id
    }).then(res=>{
      if(res.code == 200){
        wx.showToast({
          title: '操作成功',
        })
        setTimeout(()=>{
          this.ajaxForWaitOrder()
        },1000)        
      }else{
        wx.showModal({
          title: '提示',
          content: res.errMsg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {}
          }
        })
      }
    });
  },
  /** 预审查询 **/
  async query(e){
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    let id = e.currentTarget.dataset.projectid;
    findResult({
      entryId:id
    }).then(res=>{
      if(res.code == 200){
        this.setData({
          queryShow:true,
          queryObj:res.data
        });
      }else{
        wx.showModal({
          title: '提示',
          content: res.errMsg,
          showCancel: false,
          complete: (res) => {
            if (res.confirm) {}
          }
        })
      }
    });
  },
  /** 预审查询弹框关闭 **/
  queryClose(){
    this.setData({
      queryShow:false,
    });
  },
  /** 并网验收 **/
  acceptBW(e){
    routeLink("/home/pages/acceptanceBW/acceptanceBW?projectId=" + e.currentTarget.dataset.projectid + '&status=7');
  },
  /** 安装验收 **/
  acceptAZ(e){
    routeLink("/home/pages/acceptanceAZ/acceptanceAZ?projectId=" + e.currentTarget.dataset.projectid + '&type=' + this.data.type);
  },
  /** 卡信息 **/
  async bankCardDetail(e){
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    routeLink("/home/pages/bankCardDetail/bankCardDetail?managementType=" + e.currentTarget.dataset.managementtype + "&idCard=" + e.currentTarget.dataset.idcard);
  },
  /** 获取授权链接 **/
  async getEsignUrlFn(e){
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    let idCode = e.currentTarget.dataset.idcode;
    let mobile = e.currentTarget.dataset.mobile;
    let entryId = e.currentTarget.dataset.id;
    getEsignUrl({
      idCode:idCode,
      mobile:mobile,
      patnerId: wx.getStorageSync('capitalId')
    }).then(res=>{
      if(res.code == 200){
        wx.navigateTo({
          url: '../webView/webView',
          success: function (res2) {
            // 通过eventChannel向被打开页面传送数据
            res2.eventChannel.emit('acceptDataFromOpenerPage', {
              data: res.data.url
            })
          }
        })
        // wx.setClipboardData({
        //   data: res.data.url,
        //   success(res) {
        //     wx.showToast({
        //       title: '授权链接已复制',
        //       icon: 'none'
        //     })
        //   }
        // });
      }else if(res.code == 800){
        //发起变更手机号
        wx.showModal({
          content: res.errMsg,
          success (rep) {
            if (rep.confirm) {
              console.log('用户点击确定')
              wx.showLoading({
                title: '提交中',
                mask: true
              });
              resetMobileVsAuthPc({
                entryId:entryId,//电站id
                idCode:idCode,//身份证号
                mobile:mobile,//手机号
                userId:wx.getStorageSync('userId'),
              }).then(ret=>{
                wx.hideLoading()
                if(ret.code == 200){
                  wx.showToast({
                    title: "发起变更成功",
                    icon: 'none'
                  });
                }else{
                  wx.showToast({
                    title: ret.errMsg,
                    icon: 'none'
                  });
                }
              });
            } else if (rep.cancel) {
              console.log('用户点击取消')
            }
          }
        });
      }else{
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      }
    });
  },
  /** 农户合同地址导航 **/
  addrFn(e){
    let obj = e.currentTarget.dataset.obj;
    wx.showLoading({
      title: '加载中',
    });
    getAddressLonLat({
      address:obj.address
    }).then(res=>{
      wx.hideLoading()
      if (res.code == 200 && res.data) {
        var data = res.data.split(",")
        wx.openLocation({
          latitude:Number(data[1]),
          longitude:Number(data[0]),
          name:obj.address,
          scale: 18,
        });
      } else {
        wx.showToast({
          title: '获取经纬度失败',
          icon: 'none'
        })
      }
    });
  },
  /** 客户管理tab **/
  changeInforStatus(e){
    let index = e.currentTarget.dataset.index;
    if(index == 0){
      this.setData({
        credit: "0,1,2,3,5",
        creditStatus:[{
          name: '暂存',
          value: "0",
          select: true
        }, {
          name: '待预审',
          value: "1",
          select: true
        }, {
          name: '预审中',
          value: "2,3",
          select: true
        }, {
          name: '预审驳回',
          value: "5",
          select: true
        }],
      });
    }else if(index == 1){
      this.setData({
        credit: "0",
      });
    }else if(index == 2){
      this.setData({
        credit: "1,2,3,5",
        creditStatus:[{
          name: '暂存',
          value: "0",
          select: false
        }, {
          name: '待预审',
          value: "1",
          select: true
        }, {
          name: '预审中',
          value: "2,3",
          select: true
        }, {
          name: '预审驳回',
          value: "5",
          select: true
        }],
      });
    }
    this.setData({
      inforStatus:index,
      pageNum: 1,
    });
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.ajaxForWaitOrder();
  },
  /** 信息授权tab **/
  changeAuthStatus(e){
    let index = e.currentTarget.dataset.index;
    if(index==0){
      this.setData({
        authorization:'2'
      });
    }else if(index==1){
      this.setData({
        authorization:'3'
      });
    }
    this.setData({
      authStatus:index,
      pageNum: 1,
    });
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.ajaxForWaitOrder();
  },
  /** 查看信息授权书 **/
  async getAuthUrlToLook(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    var that=this
    lookAuthBook({
      idCode: e.currentTarget.dataset.idCode,
      mobile:e.currentTarget.dataset.mobile,
      partnerId: wx.getStorageSync('capitalId'), //资方
    }).then((resApi) => {
      if (resApi.code == 200) {
        //需要拼接图片前缀
        let fileUrl = that.data.IMG_BASE_URL + resApi.data.pathUrl;
        wx.downloadFile({ //将文档下载到本地
          url: fileUrl,//pdf链接
          success(res) {
            wx.openDocument({ //打开文档
              filePath: res.tempFilePath,//本地文档路径
              fileType: "pdf",//文档类型
              showMenu: true,
              success: function (res) {
                wx.hideLoading()
              },
              fail: function (res) {
                wx.showToast({
                  title: '打开文档失败',
                })
              },
            })
          },
        });
      } else {
        wx.hideLoading()
        wx.showToast({
          title: resApi.errMsg,
          icon: 'none'
        })
      }
    })
  },
  /** 派工-按钮 **/
  bindDispatch(e){
    routeLink("/home/pages/dispatch/dispatch?projectId=" + e.currentTarget.dataset.projectid);
  },
  /** 接入逆变器--接入按钮 **/
  async bindAccess(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    var that=this    
    insertInterFace({
      entryId: e.currentTarget.dataset.projectid
    }).then((resApi) => {
      wx.hideLoading()
      if (resApi.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "none",
          duration: 2000
        })
        this.setData({
          // inforStatus:index,
          pageNum: 1,
          rows: [],
          list: [],
        });
        wx.pageScrollTo({
          scrollTop: 0,
        })
        this.ajaxForWaitOrder();
        // console.log(1111,resApi)
        
      } else {
        wx.hideLoading()
        wx.showModal({
          title: '',
          content: resApi.errMsg,
          showCancel: false,
          complete: (res) => { 
            if (res.confirm) {}
          }
        })
        // wx.showToast({
        //   title: resApi.errMsg,
        //   icon: 'none'
        // })
      }
    })
  },
  /** 国网授权--授权按钮 **/
  async bindImport(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    var that=this    
    addUserInfo({
      powerGenerationNo: e.currentTarget.dataset.powerid
    }).then((resApi) => {
      wx.hideLoading()
      if (resApi.code == 200) {
        wx.showToast({
          title: resApi.data,
          icon: "none",
          duration: 2000
        })
        this.setData({
          pageNum: 1,
          rows: [],
          list: [],
        });
        wx.pageScrollTo({
          scrollTop: 0,
        })
        setTimeout(()=>{
          this.ajaxForWaitOrder();
        },2000)
      } else {
        wx.hideLoading()
        wx.showToast({
          title: resApi.errMsg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  /** 开二类卡--销卡按钮 **/
  async bindCancelCard(e) {
    //检验权限
    let perm = e.currentTarget.dataset.perm;
    if(!(await checkPermission(perm))){
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    var that=this    
    cancelBankCard({
      entryId: e.currentTarget.dataset.projectid
    }).then((resApi) => {
      wx.hideLoading()
      if (resApi.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "none"
        })
        this.setData({
          pageNum: 1,
          rows: [],
          list: [],
          // inforStatus:index,
        });
        wx.pageScrollTo({
          scrollTop: 0,
        })
        this.ajaxForWaitOrder();
      } else {
        wx.hideLoading()
        wx.showToast({
          title: resApi.errMsg,
          icon: 'none'
        })
      }
    })
  },
  /** 撤回勘测 **/
  bindBackToSurvey(e){
    const projectId = e.currentTarget.dataset.projectid
    this.setData({
      backToSurveShow:!this.data.backToSurveShow,
      backToSurveReson:'',
      projectId:projectId
    });
  },
  backToSurvePop(){
    this.setData({
      backToSurveShow:!this.data.backToSurveShow
    });
  },
  backToSurve(){
    if(!this.data.backToSurveReson){
      wx.showToast({
        title: '请输入撤回原因',
        icon:'none'
      })
      return
    }
    backToSurvey({
      remark:this.data.backToSurveReson,
      userId:wx.getStorageSync('userId'),
      id:this.data.projectId
    }).then(res=>{
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "none",
          duration: 2000
        })
        this.setData({
          backToSurveShow:!this.data.backToSurveShow,
        });
        this.setData({
          pageNum: 1,
          rows: [],
          list: [],
        });
        wx.pageScrollTo({
          scrollTop: 0,
        })
        this.ajaxForWaitOrder();
      } else {
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
})