// pages/workBenchTodoList/index.js
import request from "../../utils/request"
import { getWorkOrderList, doNoticeFlow } from "../../service/workBench"
import { alertInfo, alertSuccess, loadingFunc } from "../../utils/index";
import { noticeFlowType as noticeFlowTypeList } from "../../utils/data"
const { OK } = request
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    size: 20,
    noticeFlowTypeList,
    actionItems: [{
      name: '详情',
      value: 'detail'
  }],
  },
  showActionMenu(e) {
    const { id, status } = e.currentTarget.dataset;
    this.setData({
        currentActionSheet: id,
        actionItems: status === 0 ? [{
          name: '详情',
          value: 'detail'
      }, {
        name: '确认',
          value: 'ok'
      }] : [{
        name: '详情',
        value: 'detail'
    }]
    });
},
onClearPark() {
  const that = this;
  this.setData({ park: "", parkName: "", meter: "", meterName: "" }, () => {
    loadingFunc(async () => {
      await that.init()
    })
  })
},
onClearMeter() {
  const that = this;
  this.setData({ meter: "", meterName: "" }, () => {
    loadingFunc(async () => {
      await that.init()
    })
  })
},
onClearType() {
  const that = this;
  this.setData({ noticeFlowType: "", noticeFlowTypeName: "" }, () => {
    loadingFunc(async () => {
      await that.init()
    })
  })
},
onClearStatus() {
  const that = this;
  this.setData({ noticeFlowStatus: "", noticeFlowStatusName: "" }, () => {
    loadingFunc(async () => {
      await that.init()
    })
  })
},
hideActionMenu() {
  this.setData({
      currentActionSheet: ""
  });
},
onMenuSelect(e) {
  const {
      value
  } = e.detail;
  const id = e.currentTarget.dataset.id;

  switch(value) {
    case "detail":
      this.jumpToDetail(id);
      break;
    case "ok":
      this.handleDoNotice(id);
      break;
  }
},
handleDoNoticeBtn(e) {
  const id = e.currentTarget.dataset.id;
  this.handleDoNotice(id)
},
  async handleDoNotice(id) {
    const { code, message } = await doNoticeFlow(id)
    if (code !== OK) {
      alertInfo(message)
      return
    }
    alertSuccess("确认成功")
    const that = this;
    setTimeout(() => {
      that.init();
    }, 300)
  },
  jumpToDetail(id) {
    
    wx.navigateTo({
        url: '/pages/workOrderDetail/index?id=' + id,
    })
  },
  listJumpToDetail(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
        url: '/pages/workOrderDetail/index?id=' + id,
    })
  },
  onConfirm(e) {
    const { type, data = {}, way } = e.detail;
    const that = this;
    switch(type) {
      case "park":
        this.setData({
          park: data?.id,
          parkName: data?.name,
        })
        break;
      case "tenement":
        this.setData({
          tenement: data.id,
          tenementName: data.name,
        })
        break;
      
      case "noticeFlowType":
        this.setData({
          noticeFlowType: way,
          noticeFlowTypeName: data,
          page: 1,
        }, () => {
          loadingFunc(async () => {
            await that.init()
          })
        });
        break;
      case "noticeFlowStatus":
        this.setData({
          noticeFlowStatus: way,
          noticeFlowStatusName: data,
          page: 1,
        }, () => {
          loadingFunc(async () => {
            await that.init()
          })
        });
        break;
      case "workOrderMeter":
        this.setData({
          meter: data?.meter_id,
          meterName: data?.address,
          meterInfo: data,
          page: 1,
        }, () => {
          loadingFunc(async () => {
            await that.init()
          })
        });
        break;
    }
    this.onCancel();
  },
  onCancel() {
    this.setData({
      show: false,
      title: "",
      type: "",
    })
  },
  onParkFocus(e) {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  onNoticeFlowFocus() {
    this.setData({
      show: true,
      title: "类型",
      type: 'noticeFlowType'
    })
  },
  onNoticeFlowStatusFocus() {
    this.setData({
      show: true,
      title: "确认状态",
      type: 'noticeFlowStatus'
    })
  },
  onMeterFocus(e) {
    const { park } = this.data;
    if (!park) {
      alertInfo("请先选择园区")
      return;
    }
    this.setData({
      show: true,
      title: "电表",
      type: 'workOrderMeter'
    })
  },
  onChangePage(e) {
    const that = this;
    this.setData({
      page: e.detail.currentIndex,
    }, () => {
      loadingFunc(async () => {
        await that.init()
      })
    })
  },
  async init() {
    const { page, size, park, tenement = "", meter = "", noticeFlowType, noticeFlowStatus, time } = this.data;
    
    const { code, message, data, total } = await getWorkOrderList({ page, size, park, tenement, meter, type: noticeFlowType, time, status: noticeFlowStatus })
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    this.setData({
      list: Array.isArray(data) ? data?.map(item => {
        item.time = item?.created_at?.slice(0, 10)
        item.address = item?.target?.meter_data?.address;
        return item;
      }) : [],
      totalPage: Math.ceil(total / size),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    loadingFunc(async () => {
      await this.init()
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})