<!--
 * @Author: zk
 * @Date: 2022-01-17 10:39:22
 * @LastEditTime: 2022-03-15 16:21:43
 * @LastEditors: your name
 * @Description: 离港01
-->
<template>
  <div class="departure-one">
    <!--功能区-表单-->
    <div class="departure-form">
      <el-form
        ref="form"
        :inline="true"
        :model="formData"
        :rules="rules"
        class="form"
      >
        <el-form-item prop="currentAirport">
          <!-- <el-cascader
            v-model="formData.currentAirport"
            style="width:144px;margin-left:10px"
            placeholder="全部机场"
            size="small"
            :options="currentAirportList"
            :props="currentAirportProps"
            collapse-tags
            clearable
            filterable
            @change="setCurrentAirport"
          /> -->
          <el-select v-model="formData.currentAirport"  @change="airPortChange" placeholder="请选择机场">
            <el-option
              v-for="(item,index) in AirportList"
              :key="index"
              :label="item.PlanDepartureApt"
              :value="item.PlanDepartureApt"
             >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="startDate">
          <el-date-picker
            v-model="formData.startDate"
            style="width:216px;"
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="开始时间"
            @change="setStartDate"
          />
        </el-form-item>
        <el-form-item prop="endDate">
          <el-date-picker
            v-model="formData.endDate"
            style="width:216px;"
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="结束时间"
            @change="setEndDate"
          />
        </el-form-item>
        <div style="float:right">
            <el-form-item prop="search">
          <el-input
            v-model="formData.search"
            style="width:240px;margin-left:105px;"
            size="small"
            placeholder="请输入内容"
            prefix-icon="el-icon-search"
            clearable
            @clear="inputClear"
            @keyup.enter="onSubmit"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            @click="onSubmit"
          >搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="formData.switch"
            style="margin-left:40px;"
            active-text="显示中转"
          />
        </el-form-item>
        <el-form-item v-is="['i_timeIcon']">
          <el-dropdown>
            <img
              class="checkTime msgImg"
              src="../../../../assets/departure/ic_time.png"
            >
            <el-dropdown-menu
              slot="dropdown"
              class="time-zone"
            >
              <el-dropdown-item>国内Local/国际UTC</el-dropdown-item>
              <el-dropdown-item>Local</el-dropdown-item>
              <el-dropdown-item>UTC</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
        <el-form-item v-is="['i_columnSettings']">
          <img
            class="msgImg"
            src="../../../../assets/departure/ic_setting.png"
            @click="show"
          >
        </el-form-item>
        </div>

      </el-form>
    </div>
    <!--表格-->
    <div
      v-loading="loading"
      class="departure-table"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <el-table
        ref="table"
        :data="tableData"
        class="table"
        :height="computedTableHeight"
        :header-cell-class-name="tableCellClassName"
        :row-class-name="tableRowClassName"
        show-summary
        :summary-method="summaryMethod"
        border
        stripe
        @row-click="rowClick"
      >
        <el-table-column
          v-for="(item,index) in tableColsCopy"
          :key="index"
          :prop="item.statCode"
          :label="item.statName"
          :width="item.width"
          :filters="flightBaggageTableFilters[item.statCode]"
          :filter-method="flightBaggageTableFilters[item.statCode] && filterHandler"
        />

      </el-table>
    </div>
    <!--列设置-->
    <Dialog
      :flag="dialogFlag"
      class="dialog-check-cols"
    >
      <div class="col-dialog">
        <div class="title">列设置</div>
        <div class="content">
          <el-tree
            :data="tableCols"
            :class="colsCheckClass"
            show-checkbox
            node-key="index"
            :default-expand-all="true"
            :props="{
              label: 'statName',
              children: 'children'
            }"
            :default-checked-keys="checkedKeysTemp"
            @check="handleCheck"
          />
        </div>
        <div class="foot right t30">
          <el-button
            size="medium"
            class="r24"
            type="primary"
            @click="onCheck"
          >确定</el-button>
          <el-button
            size="medium"
            @click="hide"
          >取消</el-button>
        </div>
      </div>
    </Dialog>
  </div>
</template>

<script>
import Dialog from '@/layout/components/Dialog'
import terminalMixin from '../../mixins/terminal'
import formMixin from '../../mixins/form'
import tableColsMixin from '../../mixins/tableCols'
import { getQuery } from '@/api/flight'

export default {
  name: 'DepartureTerminalView',
  components: { Dialog },
  mixins: [terminalMixin,formMixin, tableColsMixin],
  data() {
    return {
      // 初始表头
      tableCols: [
        {
          statCode: 'FlightNO',
          statName: '航班号',
          width: 100,
        },
        {
          statCode: 'FlightDate',
          statName: '执飞日期',
          width: 105,
        },
        {
          statCode: 'PlanDepartureTime',
          statName: '预计起飞时间',
          width: 135,
        },
        {
          statCode: 'TargetAirport',
          statName: '目的站',
          width: 71,
        },
        {
          statCode: 'DepartureBuild',
          statName: '航站楼',
          width: 65,
        },
        {
          statCode: 'BordingGate',
          statName: '登机口',
          width: 68,
        },
        {
          statCode: 'StandForDepartrue',
          statName: '停机位',
          width: 60,
        },
        {
          statCode: 'checkInTravellerNumber',
          statName: '托运旅客',
          width: 88
        },
        {
          statCode: 'checkInNumber',
          statName: '值机数',
          width: 72
        },
        {
          statCode: 'unActive',
          statName: '未激活',
          width: 76
        },
        {
          statCode: 'preLoad',
          statName: '预计装载',
          width: 101
        },
        {
          statCode: 'checkNumber',
          statName: '安检',
          width: 88
        },
        {
          statCode: 'sortNumber',
          statName: '分拣',
          width: 83
        },
        {
          statCode: 'loadNumber',
          statName: '装车',
          width: 83
        },
        {
          statCode: 'loadNumber',
          statName: '装机',
          width: 80
        },
        {
          statCode: 'waitfanj',
          statName: '待翻减',
          width: 79
        },
        {
          statCode: 'unLoad',
          statName: '已翻减',
          width: 82
        },
        {
          statCode: 'noCheckInNumber',
          statName: '取消托运',
          width: 89
        },
        {
          statCode: 'noBSM',
          statName: '无BSM',
          width: 68
        },
        {
          statCode: 'riskWarning',
          statName: '风险预警',
          width: 75
        },
        {
          statCode: 'departureAnomaly',
          statName: '离港异常',
          width: 72
        },
        {
          statCode: 'midIn',
          statName: '中转进行李'
        }
      ],
      AirportList:[],
      flightBaggageTableFilters:{
        FlightNO: [],
        FlightDate:[],
        PlanDepartureTime:[],
        TargetAirport:[],
        BordingGate:[],
        StandForDepartrue:[],
        DepartureBuild:[]
      },
      loopEvent:null,
      leaveCount:0,

    }
  },
  created(){
    this.getAirPortData();
  },
  methods: {
    airPortChange(){
      this.getTableData()
    },
    async getAirPortData(){
      try {
        const res = await getQuery({
          id: 65,
          dataContent:[]
        })
        if (res.code == 0) {
         this.AirportList = res.returnData
         this.formData.currentAirport = "PEK"
         this.getTableData()
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log('出错了', error)
      }
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.hasTakenOff == 0) {
        if(rowIndex == this.leaveCount-1){
          return 'bgl-hui redBorder'
        }
        else{
          return 'bgl-hui'
        }
      }
    },
    tableCellClassName({ row, column }) {
      if (
        column.property === 'transfer_all' ||
        column.property === 'departureAnomaly' ||
        column.property === 'riskWarning'
      ) {
        return 'bgl-huang'
      }
    },
    // 获取表单下拉框数据
    // getFormData(params) {
    //   this.relatedAirportQuery({
    //     ...params,
    //     type: 'OUT'
    //   })
    //   this.outgoingAirlineQuery(params)
    //   this.craftTypeQuery(params)
    //   this.flightAttrQuery(params)
    // },
    // 获取表格数据
    async getTableData() {
      let arr = [this.formData.currentAirport,this.formData.startDate,this.formData.endDate]
      try {
        const res = await getQuery({
          id: 66,
          dataContent:[...arr,...arr,...arr]
        })
        if (res.code == 0) {
         this.initTableData(res.returnData);
        } else {
         console.log(res.message)
        }
      } catch (error) {
        clearInterval(this.loopEvent);
        console.log('出错了', error)
      }
    },
    initTableData(tableData) {
      this.leaveCount = 0;
      tableData.forEach(item => {
        if(item.hasTakenOff == 0){
          this.leaveCount++
        }
        item['waitfanj'] = item['noCheckInNumber'] - item['unLoad']
      })
      this.tableData = this._.sortBy(tableData, ['FlightDate', 'PlanDepartureTime'])
      this.setTableFilters()
    },
    // 表格添加过滤条件
    setTableFilters() {
      this.tableData.forEach(item => {
        Object.keys(this.flightBaggageTableFilters).forEach(key => {
          if ((item[key] ?? '') !== '' && this.flightBaggageTableFilters[key].every(obj => obj.value !== item[key])) {
            this.flightBaggageTableFilters[key].push({
              text: item[key],
              value: item[key]
            })
          }
          this.flightBaggageTableFilters[key] = this._.sortBy(this.flightBaggageTableFilters[key],['value'])
        })
      })
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
  },
  mounted(){
    let that = this;
    this.loopEvent = setInterval(function(){
      that.getTableData();
    },3000)
  },
  beforeDestroy(){
    clearInterval(this.loopEvent);
  }
}
</script>

<style lang="scss" scoped>
.departure-form {
  padding-top: 11px;
  ::v-deep .form {
    .el-form-item {
      margin-bottom: 0px;
      margin-right: 8px;
      button,
      input,
      optgroup,
      select,
      textarea {
        font-family: Helvetica, 'Microsoft YaHei';
        font-size: 14px;
      }
      .el-switch__label {
        color: #303133;
      }
      .el-form-item__error {
        z-index: 10;
      }
    }
    .checkTime {
      margin: 0 16px;
    }
    .msgImg {
      cursor: pointer;
      position: relative;
      top: 6px;
    }
  }
}
.departure-table {
  width: 100%;
  ::v-deep .table {
    width: 100%;
    .cell {
      padding: 0;
      text-align: center;
      font-size: 14px;
      font-family: Helvetica, 'Microsoft YaHei';
      letter-spacing: 0;
    }
    .el-table__header-wrapper {
      .cell {
        font-weight: bold;
        color: #101116;
      }
      .has-gutter {
        tr {
          .bgl-huang {
            background: #fcf0b1;
          }
        }
      }
    }
    .el-table__body-wrapper {
      tr.bgl-hui {
        background: #d2d6df;
        td {
          background: #d2d6df;
        }
        &.redBorder {
          position: relative;
          &::after {
            content: '';
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 2px;
            background: #e83f82;
          }
        }
      }
    }
  }
}
::v-deep .dialog-check-cols .el-tree {
  &.has-children .el-tree-node > .el-tree-node__children {
    display: flex;
    flex-wrap: wrap;
  }
  &.no-children {
    display: flex;
    flex-wrap: wrap;
  }
}
::v-deep .time-zone .el-dropdown-menu__item {
  font-family: Helvetica, 'Microsoft YaHei';
  font-size: 12px;
  color: #101116;
}
</style>
