/*
 * @Author: Badguy
 * @Date: 2022-03-04 14:45:03
 * @LastEditTime: 2022-05-18 09:34:03
 * @LastEditors: your name
 * @Description: 航站视图通用表单部分
 * have a nice day!
 */

import { parseTime } from '@/utils'
// const defaultStartTime = new Date(new Date(new Date().toLocaleDateString()).getTime())
// const defaultEndTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
const defaultDate = parseTime(new Date(), '{y}-{m}-{d}')
export default {
  data() {
    return {
      formData: {
        // 搜索表单数据
        currentAirport: [],
        relatedAirport: [],
        inboundCarrier: [],
        outgoingAirline: [],
        craftType: [],
        flightAttr: [],
        startDate: defaultDate,
        endDate: defaultDate,
        search: '',
        switch: true
      },
      currentAirportList: [],
      relatedAirportList: [],
      carrierList: [],
      craftTypeList: [],
      flightAttrList: [],
      currentAirportProps: {
        // multiple: true,
        checkStrictly: true,
        expandTrigger: 'hover',
        value: 'code3',
        label: 'name',
        children: 'builds'
      },
      relatedAirportProps: {
        multiple: true,
        value: 'code3',
        label: 'name'
      },
      carrierProps: {
        multiple: true,
        value: 'code2',
        label: 'name'
      },
      craftTypeProps: {
        multiple: true,
        value: 'code3',
        label: 'name'
      },
      flightAttrProps: {
        multiple: true,
        value: 'code',
        label: 'name'
      },
      // 表单规则
      rules: {
        currentAirport: [{ required: true, message: '请选择当前机场', trigger: 'change' }],
        startDate: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
        endDate: [{ required: true, message: '请选择结束时间', trigger: 'change' }]
      }
    }
  },
  computed: {
    currentAirport() {
      return this.getSingleData(this.formData.currentAirport)
    },
    relatedAirport() {
      return this.formData.relatedAirport.map(item => item[0])
    },
    inboundCarrier() {
      return this.formData.inboundCarrier.map(item => item[0])
    },
    outgoingAirline() {
      return this.formData.outgoingAirline.map(item => item[0])
    },
    craftType() {
      return this.formData.craftType.map(item => item[0])
    },
    flightAttr() {
      return this.formData.flightAttr.map(item => item[0])
    },
    startDate() {
      // return parseTime(this.formData.startDate).split(' ')[0]
      return this.formData.startDate
    },
    endDate() {
      // return parseTime(this.formData.endDate).split(' ')[0]
      return this.formData.endDate
    }
    // computedTableHeight() {
    //   const { relatedAirport, inboundCarrier, outgoingAirline, craftType, flightAttr } = this.formData
    //   return relatedAirport.length ||
    //     inboundCarrier.length ||
    //     outgoingAirline.length ||
    //     craftType.length ||
    //     flightAttr.length
    //     ? 'calc(100vh - 191px)'
    //     : 'calc(100vh - 177px)'
    // }
  },
  methods: {
    // 机场数据处理（多选）
    getMultipleData(arr) {
      const newArr = []
      arr.length &&
        arr.forEach(airport => {
          const temp = this._.cloneDeep(this.currentAirportList.find(airport1 => airport1.code3 === airport[0]))
          if (temp) {
            temp.builds = airport[1] ? [{ name: airport[1] }] : []
            const item = newArr.find(item => item.code3 === temp.code3)
            if (item) {
              item.builds.push(...temp.builds)
            } else {
              newArr.push(temp)
            }
          }
        })
      return newArr
    },
    // 机场数据处理（单选）
    getSingleData(arr) {
      const newArr = []
      if (arr.length > 0) {
        const temp = this._.cloneDeep(this.currentAirportList.find(airport1 => airport1.code3 === arr[0]))
        if (temp) {
          temp.builds = arr[1] ? [{ name: arr[1] }] : []
          newArr.push(temp)
        }
      }
      return newArr
    },
    // 清除表单数据
    formClear(range) {
      if (range === 'all') {
        this.formData.currentAirport = []
        this.currentAirportList = []
      }
      this.formData.relatedAirport = []
      this.relatedAirportList = []
      this.formData.inboundCarrier = []
      this.formData.outgoingAirline = []
      this.carrierList = []
      this.formData.craftType = []
      this.craftTypeList = []
      this.formData.flightAttr = []
      this.flightAttrList = []
    },
    // 文本框清除
    inputClear() {
      this.onSubmit()
    },
    // 当前机场变更
    setCurrentAirport(data) {
      this.formClear()
      if (data.length === 0) {
        return
      }
      const params = {
        currentAirport: this.currentAirport,
        startDate: this.startDate,
        endDate: this.endDate
      }
      this.getFormData(params)
    },
    // 日期限制
    setStartDate(val) {
      // this.formClear('all')
      if (!val) {
        return
      }
      if (this.formData.endDate && this.formData.endDate < val) {
        this.formData.endDate = ''
        this.$message.error('结束时间不能小于开始时间，请重新选择')
        return false
      } else {
        this.getTableData()
        // this.currentAirportQuery()
        return true
      }
    },
    setEndDate(val) {
      // this.formClear('all')
      if (!val) {
        return
      }
      if (this.formData.startDate && this.formData.startDate > val) {
        this.formData.endDate = ''
        this.$message.error('结束时间不能小于开始时间，请重新选择')
        return false
      } else {
        this.getTableData()
        // this.currentAirportQuery()
        return true
      }
    },
    // 搜索
    onSubmit(data) {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const az = /^[a-zA-Z]+$/
          const azNum = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$/
          // const top2 = /^[a-zA-Z]{2}\w*$/
          const top2 = /^([a-zA-Z][0-9])|([0-9][a-zA-Z])|([a-zA-Z]{2})/
          const num = /^[0-9]+$/
          const queryData = {
            startDate: this.formData.startDate,
            endDate: this.formData.endDate
          }
          if (Number(data) === 1) {
            queryData['destination'] = this.formData.currentAirport
          } else {
            queryData['station'] = this.formData.currentAirport
          }
          if (az.test(this.formData.search)) {
            // 纯字母则为旅客姓名
            queryData['name'] = this.formData.search
          } else if (azNum.test(this.formData.search) && top2.test(this.formData.search)) {
            // 字母加数字且前两位为字母则为航班号
            queryData['FlightNO'] = this.formData.search
          } else if (num.test(this.formData.search) && this.formData.search.length === 10) {
            // 纯数字且位数等于10则为行李牌号
            queryData['grade'] = this.formData.search
          } else {
            // this.$message.error('请输入有效查询信息如航班号、旅客姓名首字母、行李牌号')
          }
          this.$router.push({
            path: '/advance',
            query: queryData
          })
        } else {
          return false
        }
      })
    }
  }
}
