// pages/workBenchNew/components/workBenchMeter/index.js

import { alertInfo, alertSuccess, loadingFunc } from "../../../../utils/index";
import { getParkMeterList, handleOperateMeterMode } from "../../../../service/meter"
import { uploadMeterForce } from "../../../../service/public"
import request from "../../../../utils/request"
const { OK } = request

Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    keywordTemp: "",
    page: 1,
  },

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

  },
  onParkFocus(e) {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  onModeFocus(e) {
    this.setData({
      show: true,
      title: "强控类型",
      type: 'meterMode'
    })
  },
  onMeterFocus(e) {
    const { park } = this.data;
    if (!park) {
      alertInfo("请先选择园区")
      return;
    }
    this.setData({
      show: true,
      title: "电表",
      type: 'meter'
    })
  },
  onConfirm(e) {
    const { data, type, way } = e.detail;
    const that = this;
    switch(type) {
      case "park":
        this.setData({
          parkName: data.name,
          park: data.id,
        })
        break;
      case "meterMode":
        this.setData({
          modeName: data,
          mode: way,
        })
        break;
      case "meter":
        this.setData({
          meterName: data.address,
          meter: data.id,
        }, () => {
          loadingFunc(async () => {
            await that.init();
          })
        })
        break;
    }
    
    this.onCancel();
  },
  onChangeKeyword(e) {
    this.setData({ keywordTemp: e.detail })
  },
  changeDays(e) {
    this.setData({
      days: e.detail,
    })
  },
  handleSubmit() {
    const that = this;
    const { mode, days, reason, meter, meterName, url } = this.data;
    if (mode === 2 && days == null) {
      alertInfo("请填写天数")
      return;
    }
    wx.showModal({
      title: '提示',
      content: `确定要对${meterName}进行${["预付费", "强控", "临时强控"][mode]}操作吗？`,
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          loadingFunc(async () => {
            const { code, message } = await handleOperateMeterMode({ ids: [meter], mode, days: days == null ? undefined : Number(days), reason, images: url })
            if (code !== OK) {
              alertInfo(message)
              await that.init();
              return
            }
            alertSuccess("操作成功")
          
            await that.init();
            this.setData({
              mode: null, 
              modeName: null,
              days: null, 
              reason: null, 
              url: null,
            })
          })
        }
      }
    })
  },
  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 uploadMeterForce(tempFilePaths)
          if (code !== OK) {
            alertError(message)
            return
          }
          that.setData({
            url: url
          })
        }) 
      }
    })
  },
  goback() {
    wx.navigateBack();
  },
  clearImage() {
    this.setData({
      url: ""
    })
  },
  changeReason(e) {
    this.setData({
      reason: e.detail,
    })
  },
  onSearch() {
    const that = this;
    that.setData({
      keyword: that.data.keywordTemp
    }, () => {
      loadingFunc(async () => {
        await that.init();
      })
    })
  },
  async init() {
    const { page, park, meter } = this.data;
    if (!park) {
      return;
    }
    const { code, message, data, total } = await getParkMeterList({ park, keyword: meter, page })
    if (code !== OK) {
      alertInfo(message);
      return;
    }
    this.setData({
      list: data,
      total: total,
      totalPage: Math.ceil(total / 20)
    })
  },
  async onChangePage(e) {
    const page = e.detail.currentIndex;
    const that = this;
    this.setData({
      page
    }, () => {
      loadingFunc(async () => {
        await that.init();
      })
    })
  },
  onCancel() {
    this.setData({
      show: false,
      title: "",
      type: "",
    })
  },
  onChangeSelectMeter(e) {
    this.setData({
      meter: e.detail
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})