// pages/workBenchNew/components/uninstallMeter/index.js
import { uninstallMeter,  } from "../../../../service/meter";
import dayjs from "../../../../utils/dayjs"
import request from "../../../../utils/request"
import { alertInfo, alertSuccess, loadingFunc, wxModal} from "../../../../utils/index"
import { uploadUninstallMeter } from "../../../../service/public"

const { OK } = request;

Page({

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

  },
  onParkFocus(e) {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  onMeterFocus(e) {
    const { park } = this.data;
    if (!park) {
      alertInfo("请先选择园区")
      return;
    }
    this.setData({
      show: true,
      title: "电表",
      type: 'meter'
    })
  },
  onConfirm(e) {
    const { type, data = {} } = e.detail;
    const {collection} = this.data;
    switch(type) {
      case "park":
        this.setData({
          park: data.id,
          parkName: data.name,
          meter: null,
          meterInfo: null,
          meterName: null,
        })
        break;
      case "tenement":
        this.setData({
          tenement: data.id,
          tenementName: data.name,
        })
        break;
      case "building":
        this.setData({
          building: data.id,
          buildingName: data.name,
        });
        break;
      case "meterBox":
        this.setData({
          meterBox: data.id,
          meterBoxName: data.address,
        });
        break;
      case "meterType":
        this.setData({
          meterType: data.way,
          meterTypeName: data,
        });
        break;
      case "meter":
        this.setData({
          meter: data.id,
          meterName: data.address,
          meterInfo: data,
        });
        break;
      case "card":
        this.setData({
          card: data.id,
          cardName: data.sim_number,
        });
        break;
      case "collection":
        let newData = {}
        if (collection === "A") {
          newData = {
            collectionA: data.id,
            collectionAName: data.name,
          }
        }
        if (collection === "B") {
          newData = {
            collectionB: data.id,
            collectionBName: data.name,
          }
        }
        if (collection === "C") {
          newData = {
            collectionC: data.id,
            collectionCName: data.name,
          }
        }
        this.setData(newData);
        break;
    }
    this.onCancel();
  },
  onCancel() {
    this.setData({
      show: false,
      title: "",
      type: "",
    })
  },
  changeCritical(e) {
    this.setData({
      critical: e.detail,
    })
  },
  changePeak(e) {
    this.setData({
      peak: e.detail,
    })
  },
  changeFlat(e) {
    this.setData({
      flat: e.detail,
    })
  },
  changeValley(e) {
    this.setData({
      valley: e.detail,
    })
  },
  changeOverall(e) {
    this.setData({
      overall: e.detail,
    })
  },
  changeDeppValley(e) {
    this.setData({
      deepOverall: e.detail,
    })
  },
  async onSubmit() {
    const { meter, reason, url, meterInfo, critical, peak, flat, valley, overall, deepOverall } = this.data;
    if (!meter) {
      alertInfo("请选择表号")
      return;
    }
    if (!meterInfo?.tenement) {
      const { code, message } = await uninstallMeter({sn: meter, images: url, reason, tenement: meterInfo.tenement, critical, peak, flat, valley, overall, deepOverall})
      if (code !== OK) {
        alertInfo(message)
        return
      }
      alertSuccess("拆除成功")
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/workBenchTodoList/index',
        })
      }, 300)
      return;
    }
    wx.showModal({
      title: '提示',
      content: '拆出后电表和商户关系，电表和卡的关系会自动解绑，是否拆出',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          const { code, message } = await uninstallMeter({sn: meter, images: url, reason, tenement: meterInfo.tenement, critical, peak, flat, valley, overall, deepOverall})
          if (code !== OK) {
            alertInfo(message)
            return
          }
          alertSuccess("拆除成功")
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/workBenchTodoList/index',
            })
          }, 300)
          return;
        }
      }
    })
  },
  uploadImage() {
    const that = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: async function(res) {
        const path = res.tempFiles?.[0]?.tempFilePath.toLowerCase()
        if (!/(\.jpg|\.png|\.jpeg)$/.test(path)) {
          wx.showToast({
            title: '请上传jpg、png或jpeg格式的图片',
            icon: 'none',
          });
          return;
        }
        var tempFilePaths = path;
        loadingFunc(async () => {
          const { code, message, url } = await uploadUninstallMeter(tempFilePaths)
          if (code !== OK) {
            alertError(message)
            return
          }
          that.setData({
            url: url
          })
        }) 
      }
    })
  },
  clearImage() {
    this.setData({
      url: ""
    })
  },
  showImage() {
    this.setData({
      imageShow: true
    })
  },
  onClose() {
    this.setData({
      imageShow: false
    })
  },
  goback() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      time: dayjs().format("YYYY-MM-DD HH:mm:ss")
    })
  },
  changeReason(e) {
    this.setData({
      reason: e.detail,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})