import {
  required,
  mobileRequired,
  mobileRequired2,
  mobileRequired3,
  mobile,
  httpUrl,
  email
} from '@/components/template/rules_verify.js'
import { orderBaseDetail, orderBaseProductList } from '@/api/workOrderPool.js'

var initdata_ = {
  // 基础信息--------------------
  // 维保项目
  rpProjectRepairId: '',
  // 工程编号
  projectNo: '',
  // 工程名称
  projectName: '',
  // 客户名称
  userName: '',
  // 联系人
  linkName: '',
  // 客户电话
  userMobile: '',
  // 客户电话2
  userMobile2: '',
  // 省
  provinceId: '',
  // 市
  cityId: '',
  // 区
  areaId: '',
  // 街道
  streetId: '',
  // gps地址
  gpsAddress: '',
  // 详细地址
  address: '',
  // 服务信息--------------------------------
  createWebsitId: '',
  // 工单类型
  orderSmallType: '',
  // 工单来源
  orderChannelId: '',
  // 预约上门日期
  appointmentTime: '',
  appointmentEndTime: '',
  // 销售单位
  saleCompany: '',
  source: '',
  saleType: '',
  // 产品信息------------------------
  orderProducts: [],
  // 其它信息------------------------
  // 备注
  remark: '',
  // 派单信息-----------------------------
  // 服务商网点
  websitId: '',
  // 是否抢单
  isQd: false,
  // 主要工程师id
  workerId: '',
  // 工程师列表
  orderWorkers: [],
  orderWorkers_cp: [],

  isCj: false
}

export default {
  props: {
    id: {
      type: [String, Number],
      default: null
    },
    workOrderType: {
      type: Number,
      default: 1 // 1普通工单, 4维保工单
    },
    cloneWorkOrder: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      orderInfo: null
    }
  },
  watch: {
    id: {
      handler(newVal, oldVal) {
        this.getorderDetail(data => {
          if (!!~[1, 2, 3].indexOf(this?.workOrderType)) {
            this.$nextTick(() => {
              // 获取地址信息
              this.getinitlbslist()
              // 获取可选师傅
              this.getWorkers()
              this.getWebsitFun()
            })
          } else if (this.workOrderType == 4) {
            this.$nextTick(() => {
              // 获取维保单详情
              this.getWeiBaoDetail()
              this.getWebsitFun()
            })
          }
        })
        this.getOrderBaseLogList()
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
    // 用户信息
    userInfo() {
      return JSON.parse(localStorage.getItem('greemall_user'))
    },
    // 判断工单状态
    orderType() {
      if (this.orderInfo) {
      }
      return -1
    },
    // 处理操作按钮是否可以操作
    btnRestrict() {
      // 1,保存 2,过程反馈 3,设为异常 4,加急 5,不加急 6,取消工单 7,回访 8,新建工单 9,工单驳回 10,激活工单
      var btns = []
      if (this?.orderInfo?.isThreeOrder) {
        return []
      } else if (['DYY'].includes(this?.orderInfo?.orderStatus)) {
        // 待预约
        btns = [20]
      } else if (['DQD'].includes(this?.orderInfo?.orderStatus)) {
        // 待抢单
        btns = [1, 2, 4, 5, 6, 20]
      } else if (
        [
          'DSHPG',
          'CJ',
          'YPD',
          'DXSPD',
          'DZBPG',
          'DWDSPGP',
          'DXSSPGP',
          'DTJXSSPGP',
          'DZBSPGP',
          'DFZXPD',
          'DFZXSPGP',
          'XSBH',
          'TJXSBH',
          'ZBBH',
          'FZXBH',
          'FL'
        ].includes(this?.orderInfo?.orderStatus)
      ) {
        // 待商户派工
        btns = [1, 2, 4, 5, 6, 20]
      } else if (['DWDPG', 'DWDPD', 'WDBH'].includes(this?.orderInfo?.orderStatus)) {
        // 待网点派工
        btns = [1, 2, 4, 5, 6, 20]
      } else if (['DJD', 'DSM', 'YZP'].includes(this?.orderInfo?.orderStatus)) {
        // 待接单
        btns = [1, 2, 4, 5, 6, 20]
      } else if (['FWZ', 'GCSZT', 'BFWG'].includes(this?.orderInfo?.orderStatus)) {
        // 服务中
        btns = [1, 2, 3, 4, 5, 6, 20]
      } else if (['YCD'].includes(this?.orderInfo?.orderStatus)) {
        // 异常单
        btns = [1, 2, 6, 20]
      } else if (['YWG', 'GCSZX', 'WDWG', 'YWGO'].includes(this?.orderInfo?.orderStatus)) {
        // 已完工待结算
        btns = [7, 8, 9, 20]
      } else if (['YJS', 'LRCD'].includes(this?.orderInfo?.orderStatus)) {
        // 已结算
        btns = [20]
      } else if (['YQX', 'FWZT', 'YCGB', 'FWQX'].includes(this?.orderInfo?.orderStatus)) {
        // 已取消
        btns = [10, 20]
      }
      // 已经加急
      if ((this.orderInfo.orderFlags || []).find(item => item.tag == 'JIA_JI')) {
        btns = btns.filter(item => !new Set([4]).has(item))
      } else {
        btns = btns.filter(item => !new Set([5]).has(item))
      }
      // 已经异常
      if ((this.orderInfo.orderFlags || []).find(item => item.tag == 'YI_CHANGE')) {
        btns = btns.filter(item => !new Set([3]).has(item))
      }
      return btns
    },
    // 处理每个字段是否可编辑
    formOptions() {
      // 3.0工单禁止编辑
      var isThreeOrder = !this?.orderInfo?.isThreeOrder

      // 已完工, 已结算, 已取消
      var finish =
        ['YWG', 'GCSZX', 'WDWG', 'YWGO'].includes(this?.orderInfo?.orderStatus) ||
        ['YJS', 'LRCD'].includes(this?.orderInfo?.orderStatus) ||
        ['YQX', 'FWZT', 'YCGB', 'FWQX'].includes(this?.orderInfo?.orderStatus)

      // 待商户派工, 待网点派工, 待接单, 待预约
      var isQD =
        [
          'DSHPG',
          'CJ',
          'YPD',
          'DXSPD',
          'DZBPG',
          'DWDSPGP',
          'DXSSPGP',
          'DTJXSSPGP',
          'DZBSPGP',
          'DFZXPD',
          'DFZXSPGP',
          'XSBH',
          'TJXSBH',
          'ZBBH',
          'FZXBH',
          'FL'
        ].includes(this?.orderInfo?.orderStatus) ||
        ['DWDPG', 'DWDPD', 'WDBH'].includes(this?.orderInfo?.orderStatus) ||
        ['DJD', 'DSM', 'YZP'].includes(this?.orderInfo?.orderStatus) ||
        ['DQD'].includes(this?.orderInfo?.orderStatus) ||
        undefined === this?.orderInfo?.orderStatus

      // 非新创建
      var notNewOrder = this.id ? false : true

      return {
        // 基础信息--------------------
        // 维保项目
        rpProjectRepairId: {
          isEdit: isThreeOrder && notNewOrder,
          isShow: this.workOrderType == 4,
          isRules: [...required]
        },
        // 工程项目
        projectNo: {
          isEdit: isThreeOrder && notNewOrder,
          isShow: this.workOrderType == 2,
          isRules: [...required]
        },
        projectName: {
          isEdit: isThreeOrder && notNewOrder,
          isShow: this.workOrderType == 2,
          isRules: [...required]
        },
        // 客户名称
        userName: {
          isEdit: isThreeOrder && !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...required]
        },
        // 联系人
        linkName: {
          isEdit: isThreeOrder && !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...required]
        },
        // 客户电话
        userMobile: {
          isEdit: isThreeOrder && !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...mobileRequired2]
        },
        // 客户电话2
        userMobile2: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: [...mobileRequired3]
        },
        // gps地址
        gpsAddress: {
          isEdit: isThreeOrder && !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...required]
        },
        // 详细地址
        address: {
          isEdit: isThreeOrder && !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: []
        },
        // 服务信息--------------------------------
        createWebsitId: {
          isEdit: !this.orderInfo.createWebsitId_cp,
          isShow: true,
          isRules: [...required]
        },
        // 工单类型
        orderSmallType: {
          isEdit: isThreeOrder && notNewOrder,
          isShow: true,
          isRules: [...required]
        },
        orderChannelId: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: [...required]
        },
        // 预约上门日期
        appointmentTime: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: this.orderInfo.appointmentTime || this.orderInfo.appointmentEndTime ? [...required] : []
        },
        saleType: {
          isEdit: false,
          isShow: true,
          isRules: [...required]
        },
        saleCompany: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        source: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        // 产品信息------------------------
        orderProducts: {
          isEdit: isThreeOrder && !finish && !this.orderInfo.isCj,
          isShow: true,
          isRules: [...required]
        },
        // 其它信息------------------------
        // 备注
        remark: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        // 派单信息-----------------------------
        // 服务商网点
        websitId: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: [...required]
        },
        isQd: {
          isEdit: isThreeOrder && isQD,
          isShow: true,
          isRules: [...required]
        },
        workerId: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: this.userInfo.type != 1 && !this.orderInfo.isQd ? [...required] : []
        },
        orderWorkers: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: this.userInfo.type != 1 && !this.orderInfo.isQd ? [...required] : []
        },
        // ------------------------------
        serviceStatus: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        salesPlatform: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        salesOrderId: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        serviceOrderId: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        urgent: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        complaint: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        evaluate: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        reqStatus: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        reqType: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        },
        reqText: {
          isEdit: isThreeOrder && !finish,
          isShow: true,
          isRules: []
        }
      }
    },
    // 表单校验规则
    formRules() {
      return {}
    }
  },
  methods: {
    // 获取工单详情
    getorderDetail(cb) {
      if (this.id) {
        // 编辑详情
        Promise.all([
          orderBaseDetail({
            orderBaseId: this.id
          }),
          orderBaseProductList({
            orderBaseId: this.id
          })
        ]).then(([res, res1]) => {
          this.orderInfo = {
            ...res.data,
            orderWorkers_cp: [...(res.data.orderWorkers || []).map(item => ({ ...item }))],
            orderProducts: res1.data || [],
            workerId_fz: '',
            createWebsitId_cp: res.data.createWebsitId
          }
          cb && cb(this.orderInfo)
        })
      }
      if (this.cloneWorkOrder) {
        // 创建工单
        this.orderInfo = Object.assign({}, JSON.parse(JSON.stringify(this.cloneWorkOrder)), {
          saleType: this?.workOrderType,
          workerId_fz: ''
        })
        cb && cb(this.orderInfo)
      } else {
        // 创建工单
        this.orderInfo = Object.assign({}, JSON.parse(JSON.stringify(initdata_)), {
          saleType: this?.workOrderType,
          workerId_fz: ''
        })
        cb && cb(this.orderInfo)
      }
    }
  }
}
