import { required, mobileRequired, 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: "",
  // 服务信息--------------------------------
  // 工单类型
  orderSmallType: "",
  // 工单来源
  orderChannelId: "",
  // 预约上门日期
  appointmentTime: "",
  // 销售单位
  saleCompany: "",
  source: "",
  saleType: "",
  // 产品信息------------------------
  orderProducts: [],
  // 其它信息------------------------
  // 备注
  remark: "",
  // 派单信息-----------------------------
  // 服务商网点
  websitId: "",
  // 主要工程师id
  workerId: "",
  // 工程师列表
  orderWorkers: [],
  orderWorkers_cp: []
}

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()
            })
          } else if (this.workOrderType == 4) {
            this.$nextTick(() => {
              // 获取维保单详情
              this.getWeiBaoDetail()
            })
          }
        })
        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) {
        btns = {
          // 待预约
          DYY: [],
          // 待商户派工
          DSHPG: [1, 2, 4, 5, 6],
          // 待网点派工
          DWDPG: [1, 2, 4, 5, 6],
          // 待接单
          DJD: [1, 2, 4, 5, 6],
          // 服务中
          FWZ: [1, 2, 3, 4, 5, 6],
          // 异常单
          YCD: [1, 2, 6],
          // 已完工待结算
          YWG: [7, 8, 9],
          // 已结算
          YJS: [],
          // 已取消
          YQX: [10]
        }[this.orderInfo.orderStatus]
      }

      // 已经加急
      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() {
      var finish = !!~['YWG', 'YJS', 'YQX'].indexOf(this.orderInfo.orderStatus)
      return {
        // 基础信息--------------------
        // 维保项目
        rpProjectRepairId: {
          isEdit: this.id ? false : true,
          isShow: this.workOrderType == 4,
          isRules: [...required]
        },
        // 工程项目
        projectNo: {
          isEdit: this.id ? false : true,
          isShow: this.workOrderType == 2,
          isRules: [...required]
        },
        projectName: {
          isEdit: this.id ? false : true,
          isShow: this.workOrderType == 2,
          isRules: [...required]
        },
        // 客户名称
        userName: {
          isEdit: !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...required]
        },
        // 联系人
        linkName: {
          isEdit: !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...required]
        },
        // 客户电话
        userMobile: {
          isEdit: !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...mobileRequired]
        },
        // 客户电话2
        userMobile2: {
          isEdit: !finish,
          isShow: true,
          isRules: [...mobile]
        },
        // gps地址
        gpsAddress: {
          isEdit: !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: [...required]
        },
        // 详细地址
        address: {
          isEdit: !!~[1, 3].indexOf(this?.workOrderType) && !finish,
          isShow: true,
          isRules: []
        },
        // 服务信息--------------------------------
        // 工单类型
        orderSmallType: {
          isEdit: this.id ? false : true,
          isShow: true,
          isRules: [...required]
        },
        orderChannelId: {
          isEdit: this.id ? false : true,
          isShow: true,
          isRules: [...required]
        },
        // 预约上门日期
        appointmentTime: {
          isEdit: !finish,
          isShow: true,
          isRules: []
        },
        saleType: {
          isEdit: false,
          isShow: true,
          isRules: [...required]
        },
        saleCompany: {
          isEdit: !finish,
          isShow: true,
          isRules: []
        },
        source: {
          isEdit: !finish,
          isShow: true,
          isRules: []
        },
        // 产品信息------------------------
        orderProducts: {
          isEdit: !finish,
          isShow: true,
          isRules: [...required]
        },
        // 其它信息------------------------
        // 备注
        remark: {
          isEdit: !finish,
          isShow: true,
          isRules: []
        },
        // 派单信息-----------------------------
        // 服务商网点
        websitId: {
          isEdit: !finish,
          isShow: true,
          isRules: [...required]
        },
        workerId: {
          isEdit: !finish,
          isShow: true,
          isRules: this.userInfo.type != 1 ? [...required] : []
        },
        orderWorkers: {
          isEdit: !finish,
          isShow: true,
          isRules: this.userInfo.type != 1 ? [...required] : []
        },
      }
    },
    // 表单校验规则
    formRules() {
      return {}
    }
  },
  methods: {
    // 获取工单详情
    getorderDetail(cb) {
      console.log(this.cloneWorkOrder,"oooo")
      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: "",
            }
            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)
      }
    }
  }
}
