/*
 * @Author: Badguy
 * @Date: 2022-03-04 11:41:55
 * @LastEditTime: 2022-05-19 11:52:28
 * @LastEditors: your name
 * @Description: 航站视图通用部分
 * have a nice day!
 */

import { queryMap, myQuery } from '@/api/dataIntegration'
// import {
//   CurrentAirportQuery,
//   RelatedAirportQuery,
//   AirCompanyQuery,
//   CraftTypeQuery,
//   FlightAttrQuery,
//   IntegratedQuery,
//   IntegratedQueryTransfer
// } from '@/api/flight'
import { mapGetters } from 'vuex'
import { commonTableCellClass } from '@/utils/table'

export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      spanArr: [],
      pos: 0,
      loading: false
    }
  },
  // created() {
  //   this.currentAirportQuery()
  // },
  mounted() {
    // this.queryDepartureAirport()
  },
  computed: {
    ...mapGetters(['clickedCells']),
    dates() {
      return [this.startDate, this.endDate]
    },
    flightQueryParams() {
      return [
        this.startDate,
        this.endDate,
        this.departureAirPort,
        this.departureAirPort,
        this.landingAirport,
        this.landingAirport,
        this.carrier,
        this.carrier,
        this.craftType,
        this.craftType,
        this.flightAttr,
        this.flightAttr,
        this.search,
        this.search
      ]
    }
  },
  methods: {
    // 数据处理
    formatData(arr) {
      arr.forEach(item => {
        item['name'] = item['name'] + item['code3']
        item.builds &&
          item.builds.forEach(p => {
            p['code3'] = p.name
          })
      })
      return arr
    },
    // 合计行
    summaryMethod({ columns, data }) {
      const sums = []
      if (columns.length > 0) {
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '航班数：' + this.tableData.length
          } else if (
            // 需要计算的列
            [
              'passagernum',
              'checkNumber',
              'not_actived',
              'expect_load',
              'security_all',
              'sortNumber',
              'loadNumber',
              'tounLoad',
              'unLoad',
              'delbag',
              'noBSM',
              'transfer_all',
              'reach',
              'did_not_arrive',
              'special',
              'claim',
              'uninstalled',
              'to_be_uninstalled',
              'terminateArrive',
              'terminatedNotArrived',
              'delivered',
              'not_shipped',
              'container',
              'bulk',
              'checkInTravellerNumber',
              'checkInNumber',
              'unActive',
              'preLoad',
              'noCheckInNumber',
              'midIn',
              'checkIns',
              'projectedLoad',
              'loadedQuantity',
              'numberOfDestinationArrivals',
              'endPointNotReached',
              'specialQuantity',
              'numberOfClaims',
              'numberToBeUninstalled',
              'terminateArrivalQuantity',
              'terminateUnreachedQuantity',
              'quantityShipped',
              'undeliveredQuantity',
              'numberOfBulk',
              'inTransferBaggageCount',
              'inTransferredBaggageCount',
              'outTransferBaggageCount',
              'outTransferredBaggageCount'
            ].includes(column.property)
          ) {
            const values = data.map(item => Number(item[column.property]))
            if (values.some(value => !isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  return Number(prev) + Number(curr)
                } else {
                  return Number(prev)
                }
              }, 0)
            } else {
              sums[index] = 0
            }
          } else {
            // 过滤某些字段不参与计算
            sums[index] = '-'
          }
        })
      }
      return sums
    },
    cellClass({ row, column, rowIndex, columnIndex }) {
      let classString = commonTableCellClass({ row, column, rowIndex, columnIndex })
      if (
        [
          'FlightNO',
          'PreFlightNO',
          'inTransferBaggageCount',
          'tounLoad',
          'unLoad',
          'checkInNumber',
          'unActive',
          'midIn',
          'noCheckInNumber'
        ].includes(column.property) &&
        row[column.property]
      ) {
        classString += 'cell-click'
        if (
          this.clickedCells.some(
            cell =>
              cell.pageName === this.$route.name &&
              Object.entries(cell.row).every(([key, value]) => row[key] === value) &&
              cell.columnProp === column.property
          )
        ) {
          classString += ' cell-clicked'
        }
      }
      return classString
    },
    cellClick(row, column, cell, event) {
      if (
        [
          'FlightNO',
          'PreFlightNO',
          'inTransferBaggageCount',
          'tounLoad',
          'unLoad',
          'checkInNumber',
          'unActive',
          'midIn',
          'noCheckInNumber'
        ].includes(column.property)
      ) {
        this.$store.dispatch('keepAlive/addClickedCell', {
          row,
          columnProp: column.property,
          pageName: this.$route.name
        })
      }
      switch (column.property) {
        case 'FlightNO':
          this.$router.push({
            path: `${this.$route.path}/flightView`,
            query: {
              FlightNO: row.FlightNO,
              FlightDate: row.FlightDate
            }
          })
          break
        case 'PreFlightNO':
          this.$router.push({
            path: '/transfer/arrival/flightView',
            query: {
              FlightNO: row.PreFlightNO,
              FlightDate: row.FlightDate
            }
          })
          break
        case 'inTransferBaggageCount':
          this.$router.push({
            path: '/advance',
            query: {
              FlightNO: row.FlightNO,
              transferArrival: row.PreFlightNO,
              startDate: row.FlightDate,
              endDate: row.FlightDate
            }
          })
          break
        case 'tounLoad':
          this.$router.push({
            path: '/advance',
            query: {
              FlightNO: row.FlightNO,
              startDate: row.FlightDate,
              endDate: row.FlightDate,
              unLoad: 0
            }
          })
          break
        case 'unLoad':
          this.$router.push({
            path: '/advance',
            query: {
              FlightNO: row.FlightNO,
              startDate: row.FlightDate,
              endDate: row.FlightDate,
              unLoad: 1
            }
          })
          break
        case 'checkInNumber':
          this.$router.push({
            path: '/advance',
            query: {
              FlightNO: row.FlightNO,
              startDate: row.FlightDate,
              endDate: row.FlightDate,
              checkIn: 1
            }
          })
          break
        case 'unActive':
          this.$router.push({
            path: '/advance',
            query: {
              FlightNO: row.FlightNO,
              startDate: row.FlightDate,
              endDate: row.FlightDate,
              active: 0
            }
          })
          break
        case 'midIn':
          this.$router.push({
            path: '/advance',
            query: {
              FlightNO: row.FlightNO,
              startDate: row.FlightDate,
              endDate: row.FlightDate,
              transferIn: 1
            }
          })
          break
        case 'noCheckInNumber':
          this.$router.push({
            path: '/advance',
            query: {
              FlightNO: row.FlightNO,
              startDate: row.FlightDate,
              endDate: row.FlightDate,
              canceled: 1
            }
          })
          break
        default:
          break
      }
    },
    // // 全部机场查询
    // async currentAirportQuery() {
    //   const params = {
    //     startDate: this.startDate,
    //     endDate: this.endDate
    //   }
    //   try {
    //     const res = await CurrentAirportQuery(params)
    //     if (res.code === 0) {
    //       if (res.returnData.length) {
    //         const datas = this.formatData(res.returnData)
    //         const datasCopy = this._.cloneDeep(datas)
    //         this.currentAirportList = datas
    //         const defaultData = datasCopy[0]
    //         // this.formData.currentAirport = [[defaultData.code3, defaultData.builds[0].code3]]
    //         // this.formData.currentAirport = [[defaultData.code3]]
    //         this.formData.currentAirport = [defaultData.code3]
    //         params.currentAirport = this.currentAirport
    //         this.getFormData(params)
    //         this.getTableData({
    //           ...params,
    //           world: this.formData.search
    //         })
    //       }
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //   }
    // },
    // // 目的站/始飞站查询
    // async relatedAirportQuery(params = {}) {
    //   try {
    //     const res = await RelatedAirportQuery(params)
    //     if (res.code === 0) {
    //       const datas = this.formatData(res.returnData)
    //       this.relatedAirportList = datas
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //   }
    // },
    // // 航司查询
    // async inboundCarrierQuery(params = {}) {
    //   try {
    //     const res = await AirCompanyQuery({
    //       ...params,
    //       type: 'IN'
    //     })
    //     if (res.code === 0) {
    //       this.carrierList = res.returnData
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //   }
    // },
    // // 航司查询
    // async outgoingAirlineQuery(params = {}) {
    //   try {
    //     const res = await AirCompanyQuery({
    //       ...params,
    //       type: 'OUT'
    //     })
    //     if (res.code === 0) {
    //       this.carrierList = res.returnData
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //   }
    // },
    // // 机型查询
    // async craftTypeQuery(params = {}) {
    //   try {
    //     const res = await CraftTypeQuery(params)
    //     if (res.code === 0) {
    //       this.craftTypeList = res.returnData.map(item => ({
    //         code3: item,
    //         name: item
    //       }))
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //   }
    // },
    // // 航线查询
    // async flightAttrQuery(params = {}) {
    //   try {
    //     const res = await FlightAttrQuery(params)
    //     if (res.code === 0) {
    //       this.flightAttrList = res.returnData
    //     } else {
    //       this.$message.error(res.message)
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //   }
    // },
    // // 综合查询
    // async integratedQuery(params = {}) {
    //   try {
    //     this.loading = true
    //     const res = await IntegratedQuery(params)
    //     if (res.code === 0) {
    //       const tableData = res.returnData
    //       this.initTableData(tableData)
    //       this.loading = false
    //     } else {
    //       this.$message.error(res.message)
    //       this.loading = false
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //     this.loading = false
    //   }
    // },
    // // 中转综合查询
    // async integratedQueryTransfer(params = {}) {
    //   try {
    //     this.loading = true
    //     const res = await IntegratedQueryTransfer(params)
    //     if (res.code === 0) {
    //       const tableData = res.returnData
    //       this.initTableData(tableData)
    //       this.loading = false
    //     } else {
    //       this.$message.error(res.message)
    //       this.loading = false
    //     }
    //   } catch (error) {
    //     console.log('出错了', error)
    //     this.loading = false
    //   }
    // },
    // 查询起飞机场 id: 31
    queryDepartureAirport() {
      return myQuery(queryMap.departureAirPort, ...this.dates)
    },
    // 查询降落机场 id: 32
    queryLandingAirport() {
      return myQuery(queryMap.landingAirport, ...this.dates)
    },
    // 按照降落机场查询起飞机场 id: 33
    queryDepartureAirportByLandingAirport() {
      return myQuery(queryMap.departureAirPortBylandingAirport, this.currentAirport, ...this.dates)
    },
    // 按照起飞机场查询降落机场 id: 34
    queryLandingAirportByDepartureAirport() {
      return myQuery(queryMap.landingAirportBydepartureAirPort, this.currentAirport, ...this.dates)
    },
    // 查询航司 id: 35
    queryCarrier() {
      return myQuery(queryMap.carrier, ...this.dates)
    },
    // 查询机型 id: 36
    queryCraftType() {
      return myQuery(queryMap.craftType, ...this.dates)
    },
    // 查询航线 id: 37
    queryAirline() {
      return myQuery(queryMap.flightAttr, ...this.dates)
    },
    // 查询进港航班 id: 38
    queryLandingFlight() {
      return myQuery(queryMap.landingFlight, ...this.flightQueryParams)
    },
    // 查询离港航班 id: 39
    queryDepartureFlight() {
      return myQuery(queryMap.departureFlight, ...this.flightQueryParams)
    },
    // 查询中转航班 id: 40
    queryTrasferFlight() {
      return myQuery(queryMap.transferFlight, ...this.flightQueryParams)
    }
  }
}
