import { lbsAmapRegion } from '@/api/common.js'
import { listPageV2, getDetail } from "@/api/engineeringMaintenance/basicData";
import { orderEnginbaseList } from "@/api/basicEngineeringData";
import geographicalPosi from '@/components/geographicalPosi/index.vue'
export default {
  data() {
    return {
      provinceList: [],
      cityList: [],
      areaList: [],
      streetList: [],
      wb_project: [],
      gc_project: []
    }
  },
  created() {
    function getTimef(sfm = "00:00:00") {
      var currentDate = new Date();
      var year = currentDate.getFullYear();
      var month = ('0' + (currentDate.getMonth() + 1)).slice(-2); // 月份从0开始，需要加1
      var day = ('0' + currentDate.getDate()).slice(-2);
      return year + '-' + month + '-' + day + ' ' + sfm
    }
    // 获取工程下拉
    orderEnginbaseList({
      "pageNum": 1,
      "pageSize": -1,
      "params": []
    }).then(res => {
      this.gc_project = res.data.records.map(item => {
        return {
          value: item.projectNo,
          label: item.projectName,
          data: item,
        }
      })
    })
    // 获取维保下拉
    listPageV2({
      "pageNum": 1,
      "pageSize": -1,
      "params": [
        { "param": "a.start_time", "compare": "<=", "value": getTimef() },
        { "param": "a.end_time", "compare": ">=", "value": getTimef() }
      ]
    }).then(res => {
      this.wb_project = res.data.records.map(item => {
        return {
          value: item.id,
          label: item.projectName,
          data: item,
        }
      })
    })
  },
  computed: {
    // 基本信息数据模型
    basicInfo() {
      return [
        ...(() => {
          if (this.workOrderType == 4) {
            return [{
              isShow: this.formOptions.rpProjectRepairId.isShow,
              name: 'el-select',
              md: 12,
              options: this.wb_project,
              attributes: {
                disabled: !this.formOptions.rpProjectRepairId.isEdit,
                placeholder: '请选择',
                clearable: true,
                filterable: true
              },
              formItemAttributes: {
                label: '维保项目',
                prop: 'rpProjectRepairId',
                rules: this.formOptions.rpProjectRepairId.isRules
              },
              events: {
                change: (val) => {
                  this.getorderDetail(() => {
                    if (val) {
                      this.orderInfo.rpProjectRepairId = val
                      this.orderInfo.rpProjectRepairName = this.wb_project.find(item => item.value == val).label
                      this.getWeiBaoDetail()
                    }
                  })
                }
              }
            }, {
              name: 'slot-component',
              md: 12,
              formItemAttributes: {
                'label-width': '0px',
                prop: '',
              },
              render: (h, { props }) => {
                return null
              }
            },]
          } else if (this.workOrderType == 2) {
            return [{
              isShow: this.formOptions.projectNo.isShow,
              name: 'el-select',
              md: 12,
              options: this.gc_project,
              attributes: {
                disabled: !this.formOptions.projectNo.isEdit,
                placeholder: '请选择',
                clearable: true,
                filterable: true
              },
              formItemAttributes: {
                label: '工程名称',
                prop: 'projectNo',
                rules: this.formOptions.projectNo.isRules
              },
              events: {
                change: (val) => {
                  this.getorderDetail(() => {
                    if (val) {
                      this.orderInfo.projectNo = val
                      var item = this.gc_project.find(item => item.value == val)
                      if (item) {
                        this.orderInfo.projectName = item.label
                        var data = item.data
                        if (data) {
                          this.orderInfo.userName = data.manger
                          this.orderInfo.linkName = data.manger
                          this.orderInfo.userMobile = data.mobile
                          this.orderInfo.provinceId = data.provinceId
                          this.orderInfo.province = data.province
                          this.orderInfo.cityId = data.cityId
                          this.orderInfo.city = data.city
                          this.orderInfo.areaId = data.areaId
                          this.orderInfo.area = data.area
                          this.orderInfo.streetId = data.streetId
                          this.orderInfo.street = data.street
                          this.orderInfo.gpsAddress = data.gpsAddress || data.address
                          this.orderInfo.lat = data.lat
                          this.orderInfo.lng = data.lng
                          this.orderInfo.address = data.address
                          this.getinitlbslist()
                        }
                      }
                    }
                  })
                }
              }
            }, {
              isShow: true,
              name: 'el-input',
              md: 12,
              attributes: {
                disabled: true,
              },
              formItemAttributes: {
                label: '工程编号',
                prop: 'projectNo',
                rules: this.formOptions.projectNo.isRules
              }
            }]
          }
          return []
        })(),
        {
          isShow: this.formOptions.userName.isShow,
          name: 'el-input',
          md: 6,
          attributes: { disabled: !this.formOptions.userName.isEdit, placeholder: '请输入' },
          formItemAttributes: {
            label: '客户名称',
            prop: 'userName',
            rules: this.formOptions.userName.isRules
          },
          events: {
            input: (v) => {
              this.orderInfo.linkName = v
              if (v) {
                this.appointVerify(["linkName"], () => { }, false)
              }
            }
          }
        },
        {
          isShow: this.formOptions.linkName.isShow,
          name: 'el-input',
          md: 6,
          attributes: { disabled: !this.formOptions.linkName.isEdit, placeholder: '请输入' },
          formItemAttributes: {
            label: '联系人',
            prop: 'linkName',
            rules: this.formOptions.linkName.isRules
          }
        },
        {
          isShow: this.formOptions.userMobile.isShow,
          name: 'el-input',
          md: 6,
          attributes: { disabled: !this.formOptions.userMobile.isEdit, placeholder: '请输入', maxlength: 11 },
          formItemAttributes: {
            label: '客户电话',
            prop: 'userMobile',
            rules: this.formOptions.userMobile.isRules
          }
        },
        {
          isShow: this.formOptions.userMobile2.isShow,
          name: 'el-input',
          md: 6,
          attributes: { disabled: !this.formOptions.userMobile2.isEdit, placeholder: '请输入', maxlength: 11 },
          formItemAttributes: {
            label: '客户电话2',
            prop: 'userMobile2',
            rules: this.formOptions.userMobile2.isRules
          }
        },
        {
          isShow: this.formOptions.gpsAddress.isShow,
          name: 'el-select',
          md: 6,
          formItemAttributes: {
            label: '客户地址', //省
            prop: 'provinceId',
            rules: this.formOptions.gpsAddress.isRules,
            errLabel: '省'
          },
          options: this.provinceList.map((v, i) => ({ value: v.id, label: v.name })),
          attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
          events: {
            change: (val) => {
              // 获取省名称
              this.orderInfo.province = this.provinceList.find(item => item.id === val)?.name || ''
              // 清除市区街道以及详细地址数据
              this.delDataK(1)
              lbsAmapRegion({ pid: val }).then(res => {
                this.cityList = res.data
              })
            }
          }
        },
        {
          isShow: this.formOptions.gpsAddress.isShow,
          name: 'el-select',
          md: 5,
          formItemAttributes: {
            'label-width': '0px',
            label: '', //市
            prop: 'cityId',
            rules: this.formOptions.gpsAddress.isRules,
            errLabel: '市'
          },
          options: this.cityList.map((v, i) => ({ value: v.id, label: v.name })),
          attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
          events: {
            change: (val) => {
              // 获取市名称
              this.orderInfo.city = this.cityList.find(item => item.id === val).name
              // 清除区街道以及详细地址数据
              this.delDataK(2)
              lbsAmapRegion({ pid: val }).then(res => {
                this.areaList = res.data
              })
            }
          }
        },
        {
          isShow: this.formOptions.gpsAddress.isShow,
          name: 'el-select',
          md: 5,
          formItemAttributes: {
            'label-width': '0px',
            label: '', //区
            prop: 'areaId',
            rules: this.formOptions.gpsAddress.isRules,
            errLabel: '区'
          },
          options: this.areaList.map((v, i) => ({ value: v.id, label: v.name })),
          attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
          events: {
            change: (val) => {
              // 获取区名称
              this.orderInfo.area = this.areaList.find(item => item.id === val).name
              // 清除街道以及详细地址数据
              this.delDataK(3)
              lbsAmapRegion({ pid: val }).then(res => {
                this.streetList = res.data
              })
            }
          }
        },
        {
          isShow: this.formOptions.gpsAddress.isShow,
          name: 'el-select',
          md: 5,
          formItemAttributes: {
            'label-width': '0px',
            label: '', //街道
            prop: 'streetId',
            rules: this.formOptions.gpsAddress.isRules,
            errLabel: '街道'
          },
          options: this.streetList.map((v, i) => ({ value: v.id, label: v.name })),
          attributes: { disabled: true, clearable: true, filterable: true, placeholder: '请选择' },
          events: {
            change: (val) => {
              // 获取街道名称
              this.orderInfo.street = this.streetList.find(item => item.id === val).name
              // 清除详细地址数据
              this.delDataK(4)
            }
          }
        },
        {
          isShow: this.formOptions.gpsAddress.isShow,
          name: 'slot-component',
          md: 3,
          formItemAttributes: {
            'label-width': '0px',
            prop: '',
          },
          render: (h, { props }) => {
            return (
              <geographicalPosi
                disabled={!this.formOptions.gpsAddress.isEdit}
                formData={this.orderInfo}
                onSelectPosi={data => {
                  // 获取经纬度
                  this.orderInfo.lng = data.center[0]
                  this.orderInfo.lat = data.center[1]
                  // 获取定位的省市区街道
                  var { province, city, district, township } = data.data.addressComponent
                  // 获取选中省名称id
                  var { id, name } = this.provinceList.find(item => item.name === province)
                  this.orderInfo.provinceId = id
                  this.orderInfo.province = name
                  // 请求市选项
                  lbsAmapRegion({ pid: this.orderInfo.provinceId }).then(res => {
                    // 赋值市选项
                    this.cityList = res.data
                    // 获取选中市名称id
                    var { id, name } = res.data.find(item => item.name === city)
                    this.orderInfo.cityId = id
                    this.orderInfo.city = name
                    // 请求区选项
                    lbsAmapRegion({ pid: this.orderInfo.cityId }).then(res => {
                      // 赋值区选项
                      this.areaList = res.data
                      // 获取选中区名称id
                      var { id, name } = res.data.find(item => item.name === district)
                      this.orderInfo.areaId = id
                      this.orderInfo.area = name
                      // 请求街道选项
                      lbsAmapRegion({ pid: this.orderInfo.areaId }).then(res => {
                        // 赋值街道选项
                        this.streetList = res.data
                        // 获取选中街道名称id
                        var { id, name } = res.data.find(item => item.name === township)
                        this.orderInfo.streetId = id
                        this.orderInfo.street = name
                        // 赋值GPS详细地址
                        this.orderInfo.gpsAddress = data.name
                        if (!this.orderInfo.address) {
                          this.orderInfo.address = data.name
                        }
                      })
                    })
                  })
                }}
              />
            )
          }
        },
        {
          isShow: this.formOptions.gpsAddress.isShow,
          name: 'el-input',
          md: 13,
          formItemAttributes: { label: '', prop: 'gpsAddress', rules: this.formOptions.gpsAddress.isRules, errLabel: 'GPS地址' },
          attributes: { disabled: true, placeholder: 'GPS地址' }
        },
        {
          isShow: this.formOptions.address.isShow,
          name: 'el-input',
          md: 11,
          formItemAttributes: { label: '', prop: 'address', 'label-width': '0px', rules: this.formOptions.address.isRules },
          attributes: { disabled: !this.formOptions.address.isEdit, placeholder: '详细地址' }
        },
        {
          isShow: this.formOptions.remark.isShow && !this.id,
          name: 'el-input',
          md: 24,
          attributes: {
            disabled: !this.formOptions.remark.isEdit,
            type: 'textarea',
            rows: 3,
            placeholder: '请输入'
          },
          formItemAttributes: {
            label: '备注',
            prop: 'remark',
            rules: this.formOptions.remark.isRules
          }
        }
      ]
    }
  },
  methods: {
    getWeiBaoDetail() {
      if (this.orderInfo.rpProjectRepairId) {
        getDetail({
          id: this.orderInfo.rpProjectRepairId
        }).then(res => {

          if (!this.id) {
            this.orderInfo.userName = res.data.manger
            this.orderInfo.linkName = res.data.manger
            this.orderInfo.userMobile = res.data.mobile
            this.orderInfo.provinceId = res.data.provinceId
            this.orderInfo.province = res.data.province
            this.orderInfo.cityId = res.data.cityId
            this.orderInfo.city = res.data.city
            this.orderInfo.areaId = res.data.areaId
            this.orderInfo.area = res.data.area
            this.orderInfo.streetId = res.data.streetId
            this.orderInfo.street = res.data.street
            this.orderInfo.gpsAddress = res.data.gpsAddress || res.data.address
            this.orderInfo.lat = res.data.lat
            this.orderInfo.lng = res.data.lng
            this.orderInfo.address = res.data.address
          }

          // 工单小类下拉
          this.orderSmallTypeData = res.data.typeList.map(item => ({
            value: item.id,
            label: item.orderSmallTypeText
          }))

          // 网点下拉数据
          this.websitList = (res.data.websitList || []).map(item => ({
            label: item.websitName,
            value: item.websitId
          }))

          // 获取师傅
          this.getWorkers()

          // 产品信息可选项
          this.productList_wb = (res.data.productList || [])

          this.getinitlbslist()
        })
      }
    },
    getinitlbslist() {
      // 初始化请求省市区街道下拉选项数据
      lbsAmapRegion({ pid: 0 }).then(res => {
        this.provinceList = res.data
        // 创建工单时获取ip地址定位赋值
        if (!this.id && !!~[1, 2, 3].indexOf(this?.workOrderType) && this.$IpAdd.province) {
          var item = this.provinceList.find(item => item.name === this.$IpAdd.province)
          if (item) {
            this.orderInfo.provinceId = item.id
            this.orderInfo.province = item.name
          }
        }
        if (this.orderInfo.provinceId) {
          lbsAmapRegion({ pid: this.orderInfo.provinceId }).then(res => {
            this.cityList = res.data
            // 创建工单时获取ip地址定位赋值
            if (!this.id && this.$IpAdd.city) {
              var item2 = this.cityList.find(item => item.name === this.$IpAdd.city)
              if (item2) {
                this.orderInfo.cityId = item2.id
                this.orderInfo.city = item2.name
              }
            }
            if (this.orderInfo.cityId) {
              lbsAmapRegion({ pid: this.orderInfo.cityId }).then(res => {
                this.areaList = res.data
              })
            }
            if (this.orderInfo.areaId) {
              lbsAmapRegion({ pid: this.orderInfo.areaId }).then(res => {
                this.streetList = res.data
              })
            }
          })
        }
      })
    },
    delDataK(num) {
      if (num <= 1) {
        // 删除市
        this.orderInfo.cityId = ''
        this.orderInfo.city = ''
        // 删除市选项
        this.cityList = []
      }
      if (num <= 2) {
        // 删除区
        this.orderInfo.areaId = ''
        this.orderInfo.area = ''
        // 删除区选项
        this.areaList = []
      }
      if (num <= 3) {
        // 删除街道
        this.orderInfo.streetId = ''
        this.orderInfo.street = ''
        // 删除街道选项
        this.streetList = []
      }
      // 删除gps地址
      this.orderInfo.gpsAddress = ''
    }
  }
}
