import { getStorage } from '@/common/utils/storage.js'
import moment from 'moment-timezone'
// 设置时区为中国
moment.tz.setDefault('Asia/Shanghai')

export default {
  methods: {
    isTodayOrYesterday(timeString) {
      const date = moment(timeString)
      const now = moment()
      // 获取今天和昨天的日期
      const today = now.startOf('day')
      const yesterday = today.clone().subtract(1, 'days')

      // 判断给定日期是否在今天或昨天
      return date.isSame(today, 'day') || date.isSame(yesterday, 'day')
    },
    orderStatus_v_zh(orderStatus, item) {
      if (item?.isImportExcel && item?.isMeet === false) {
        return 'DJD'
      }
      if (['DYY'].includes(orderStatus)) {
        // 待预约
        return 'DYY'
      } else if (['DQD'].includes(orderStatus)) {
        // 待抢单
        return 'DQD'
      } else if (
        [
          'DSHPG',
          'CJ',
          'YPD',
          'DXSPD',
          'DZBPG',
          'DWDSPGP',
          'DXSSPGP',
          'DTJXSSPGP',
          'DZBSPGP',
          'DFZXPD',
          'DFZXSPGP'
        ].includes(orderStatus)
      ) {
        // 待商户派工
        return 'DSHPG'
      } else if (['DWDPG', 'DWDPD'].includes(orderStatus)) {
        // 待网点派工
        return 'DWDPG'
      } else if (['DJD'].includes(orderStatus)) {
        // 待接单
        return 'DJD'
      } else if (
        ['FWZ', 'GCSZT', 'BFWG', 'DSM', 'YZP', 'WDBH', 'XSBH', 'TJXSBH', 'ZBBH', 'FZXBH'].includes(orderStatus)
      ) {
        // 服务中
        return 'FWZ'
      } else if (['YCD'].includes(orderStatus)) {
        // 异常单
        return 'YCD'
      } else if (['YWG', 'GCSZX', 'WDWG', 'YWGO'].includes(orderStatus)) {
        // 已完工待结算
        return 'YWG'
      } else if (['YJS', 'LRCD'].includes(orderStatus)) {
        // 已结算
        return 'YJS'
      } else if (['YQX', 'FWZT', 'YCGB', 'FWQX', 'FL'].includes(orderStatus)) {
        // 已取消
        return 'YQX'
      }
      return ''
    },
    // 接单按钮
    JIEDAN(item, userInfo) {
      // ---------------
      return (
        (item.isImportExcel &&
          !item.isMeet &&
          item.attr == 'SELF' &&
          (item.orderType == 'INSTALL' ? !!userInfo.workerNumber : true) &&
          !['YQX'].includes(this.orderStatus_v_zh(item.orderStatus, item))) ||
        // ---------------
        (item.isImportExcel &&
          !item.isMeet &&
          item.attr != 'SELF' &&
          !['YQX'].includes(this.orderStatus_v_zh(item.orderStatus, item))) ||
        // ---------------
        (!item.isImportExcel &&
          item.attr == 'SELF' &&
          ['DYY', 'DJD'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
          (item.orderType == 'INSTALL' ? !!userInfo.workerNumber : true)) ||
        // ---------------
        (!item.isImportExcel &&
          item.attr != 'SELF' &&
          (['DYY'].includes(this.orderStatus_v_zh(item.orderStatus, item)) ||
            (['DJD'].includes(this.orderStatus_v_zh(item.orderStatus, item))
              ? item.orderType == 'INSTALL'
                ? !!userInfo.workerNumber
                : true
              : false)))
      )
    },
    // 抢单按钮
    QIANGDAN(item, userInfo) {
      // ---------------
      return (
        (!item.isImportExcel &&
          item.attr == 'SELF' &&
          ['DQD'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
          (item.orderType == 'INSTALL' ? !!userInfo.workerNumber : true)) ||
        // ---------------
        (!item.isImportExcel && item.attr != 'SELF' && ['DQD'].includes(this.orderStatus_v_zh(item.orderStatus, item)))
      )
    },
    // 配件申请
    PEIJIANSHENQING(item, userInfo) {
      // ---------------
      return (
        ((item.isImportExcel &&
          item.orderType != 'INSTALL' &&
          ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item))) ||
          // ---------------
          (item.isImportExcel &&
            item.orderType != 'INSTALL' &&
            ['YWG'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
            this.isTodayOrYesterday(item.overTime)) ||
          // ---------------
          (!item.isImportExcel && ['DYY'].includes(this.orderStatus_v_zh(item.orderStatus, item))) ||
          // ---------------
          (!item.isImportExcel &&
            ['FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
            item.orderType != 'INSTALL')) &&
        (item.isImportExcel ? item.isMeet : true)
      )
    },
    // 变更小工
    BIANGENGXIAOGONG(item, userInfo) {
      // ---------------
      return (
        ((item.isImportExcel &&
          ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
          (item.orderType == 'INSTALL' ? !!userInfo.workerNumber : true)) ||
          // ---------------
          (!item.isImportExcel &&
            ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
            (item.orderType == 'INSTALL' ? !!userInfo.workerNumber : true))) &&
        (item.isImportExcel ? item.isMeet : true)
      )
    },
    // 辅材收费
    FUCAISHOUFEI(item, userInfo) {
      return (
        ((item.isImportExcel &&
          ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
          (item.orderType == 'INSTALL' ? !!userInfo.workerNumber : true)) ||
          //----------
          (!item.isImportExcel &&
            ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
            !!userInfo.workerNumber) ||
          //----------
          (((item.isImportExcel &&
            ['YWG'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
            (item.orderType == 'INSTALL' ? !!userInfo.workerNumber : true)) ||
            (!item.isImportExcel &&
              ['YWG'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
              !!userInfo.workerNumber)) &&
            this.isTodayOrYesterday(item.overTime))) &&
        (item.isImportExcel ? item.isMeet : true)
      )
    },
    // 配件收费
    PEIJIANSHOUFEI(item, userInfo) {
      return (
        ((item.isImportExcel &&
          item.orderType != 'INSTALL' &&
          ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item))) ||
          //----------
          (!item.isImportExcel &&
            item.orderType != 'INSTALL' &&
            ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
            !!userInfo.workerNumber) ||
          //----------
          (((item.isImportExcel &&
            item.orderType != 'INSTALL' &&
            ['YWG'].includes(this.orderStatus_v_zh(item.orderStatus, item))) ||
            (!item.isImportExcel &&
              item.orderType != 'INSTALL' &&
              ['YWG'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
              !!userInfo.workerNumber)) &&
            this.isTodayOrYesterday(item.overTime))) &&
        (item.isImportExcel ? item.isMeet : true)
      )
    },
    // 过程反馈
    GUOCHENFANKUI(item, userInfo) {
      return (
        ['DYY', 'FWZ'].includes(this.orderStatus_v_zh(item.orderStatus, item)) &&
        (item.isImportExcel ? item.isMeet : true)
      )
    },
    // 总部下载
    ZONGBUXIAZAI(item, userInfo) {
      return item.isImportExcel
    },
    ZONGBUXIAZAIFUNC(item, userInfo) {
      if (!item.lastClickTime || item.lastClickTime + 60000 < new Date().getTime()) {
        this.$api
          .post('/changeOrder/dow', {
            orderBaseId: item.id
          })
          .then(res => {
            item.lastClickTime = new Date().getTime()
            this.$successToast()
          })
      } else {
        this.$successToast()
      }
    }
  }
}
