<template>
  <div class="airportInfo">
    <div class="variable">
      <StatisticsHeader :title="titleTop" :items="formItems" :data="formData" :eledata="eledata" with-setting :withSetting="false" :withExport="true" :set="set" :action="action" :asShow="asShow" @getFormData="getFormData" @export="tableToExcel('table')" @upset="upset" />
    </div>
    <div v-loading="loading" element-loading-text="数据加载中..." class="echart">
      <!-- <Echarts :id="dataid" :option="tableData" v-if="picShow == true ? true : false" /> -->
      <Tableformbrs ref="table" :tableList="tableList" :action="action" :set="set" />
    </div>
  </div>
</template>

<script>
import Echarts from "../../components/echart/index.vue";
import Tableformbrs from "../../components/echart/tableforms.vue";
import StatisticsHeader from "../../components/echart/statisticsHeader.vue";
import { export_json_to_excel, exportToExcel } from "@/utils/Export2Excel";
import { Query } from "@/api/webApi";
import _ from 'lodash'
export default {
  name: "ChartsBar",
  data () {
    return {
      asShow: false,
      optiondata: [],
      picShow: true, //图片表格切换
      action: 18,
      dataid: "collection_ecahrt1",
      listqueryTemplateID: DATACONTENT_ID.modeFlightvolume,
      eledata: 1,
      set: "",
      isShow: Echarts,
      tableList: [],
      tableListcop: [],
      listname: "",
      listHeader: [],
      tableData: {
        time: [],
        data1: [],
        data2: [],
        kg: "",
        tyol: "",
      },
      loading: false,
      formData: {
        flighttype: "",
        dateTime: "",
      },
      titleTop: "部门维度统计",
      levelList: [
        {
          name: "加货重量统计",
        },
        {
          name: "航班量统计",
        },
        {
          name: "特货统计",
        },
        {
          name: "货量统计",
        },
        {
          name: "拉货统计",
        },
        {
          name: "国内进港保障时间统计",
        },
      ],
      formItems: [
        {
          prop: "fttp",
          inputType: "select",
          placeholder: "国内国际",
          requiredWarning: "请先选择国内国际",
          clearable: true,
          options: [
            {
              value: DATACONTENT_ID.dimensionGnDeparture,
              label: "国内出港",
            },
            {
              value: DATACONTENT_ID.dimensionGjDeparture,
              label: "国际出港",
            },
            {
              value: DATACONTENT_ID.dimensionGnArrival,
              label: "国内进港",
            },
            {
              value: DATACONTENT_ID.dimensionGjArrival,
              label: "国际进港",
            },
          ],
        },
        {
          prop: "flighttype",
          inputType: "select",
          placeholder: "客货类型",
          requiredWarning: "请先选择客货类型",
          clearable: true,
          options: [
            {
              value: "客机",
              label: "客机",
            },
            {
              value: "货机",
              label: "货机",
            },
            {
              value: "全部",
              label: "全部",
            },
          ],
        },
        {
          prop: "dateTime",
          inputType: "erdatime",
          requiredWarning: "请先选择日期",
          clearable: true,
          width: "240px",
          options: [],
          haveDisabled: true,
          disabledDate: function (time) {
            return !(time.getTime() < Date.now() - 3600 * 1000 * 24)
          }
        },
      ],
      newObj: {}
    };
  },
  mounted () {
    // this.getQuery([]);
  },
  watch: {
    // 监听数据变化，重绘折线图
    option: {
      deep: true,
      handler (newVal) { },
    },
  },
  destroyed () { },
  methods: {
    getFormData (data) {
      this.tableData.time = [];
      this.tableData.data1 = [];
      this.tableData.data2 = [];
      this.newObj = _.cloneDeep(data)
      let option = [
        {
          flighttype: data.flighttype,
          fd1: data.dateTime,
          fd2: data.dateTime
        },
      ];
      this.getQuery(data.fttp, option, null);
      let typeName = null
      switch (data.fttp) {
        case 52229:
          typeName = '国内出港'
          break;
        case 52230:
          typeName = '国内进港'
          break;
        case 52231:
          typeName = '国际出港'
          break;
        case 52232:
          typeName = '国际进港'
          break;
        default:
          typeName = '国内出港'
          break;
      }
      this.listname =
        "部门维度统计" + '-' +
        typeName + '-' +
        data.flighttype + '-' +
        data.dateTime;
      this.listHeader = ["序号", "时间", "航班(班)", "环比(%)"];
    },
    //获取表格数据
    async getQuery (id, data, dat) {
      this.loading = true
      try {
        const { code, returnData } = await Query({
          id: Number(id),
          dataContent: data,
        });
        if (code == 0) {
          this.tableData.time = [];
          this.tableData.data1 = [];
          this.tableData.data2 = [];
          const copyDatas = _.cloneDeep(returnData);
          let typeName = null
          switch (data.fttp) {
            case 52229:
              typeName = '国内'
              break;
            case 52230:
              typeName = '国内'
              break;
            case 52231:
              typeName = '国际'
              break;
            case 52232:
              typeName = '国际'
              break;
            default:
              typeName = '国内'
              break;
          }
          copyDatas.forEach(item => {
            item.typeName = typeName
          });
          this.tableList = copyDatas;
          this.tableListcop = copyDatas;
          this.loading = false
          // returnData.listValues.forEach((element) => {
          //   this.tableData.data2.push(
          //     element.flightNum ? element.flightNum : 0
          //   );
          //   this.tableData.time.push(element.dat);
          //   this.tableData.kg = "单位：班";
          //   this.tableData.data1 = [];
          // });
          // this.tableData.data1.push(0);
          // for (let index = 0; index < this.tableData.data2.length; index++) {
          //   if (index > 0) {
          //     const element =
          //       (this.tableData.data2[index] -
          //         this.tableData.data2[index - 1]) /
          //       this.tableData.data2[index - 1];
          //     this.tableData.data1.push(
          //       element.toFixed(2) ? element.toFixed(2) : 0
          //     );
          //     this.tableData.data1.forEach((element) => {
          //       element = Number(element);
          //       if (typeof element !== "number") {
          //         element = 0;
          //       }
          //     });
          //   }
          // }
        } else {
          this.loading = false
          console.log(code)
        }
      } catch (error) {
        this.loading = false
        this.page--;
      }
    },
    //导出
    tableToExcel (refName) {
      import("@/utils/Export2Excel").then((excel) => {
        const table = this.$refs[refName].$el.cloneNode(true);
        const fileName = `${this.listname}.xlsx`;
        exportToExcel(table, '部门维度统计', fileName);
      });
    },
    upset (data) {
      this.picShow = data;
    },
  },
  components: {
    Echarts,
    StatisticsHeader,
    Tableformbrs,
  },
};
</script>
<style lang="scss" scoped>
.airportInfo {
  position: relative;
  .header {
    width: 103%;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f7f7f7;
    position: relative;
    left: -23px;
    > .nav {
      padding: 0 30px 0 30px;
      cursor: pointer;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #101116;
    }
    > .navs {
      height: 100%;
      padding: 0 30px 0 30px;
      cursor: pointer;
      display: flex;
      align-items: center;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #ac014d;
      border-bottom: 2px solid #ac014d;
    }
  }
  .variable {
    width: 100%;
    height: 52px;
  }
  .echart {
    width: 100%;
    height: 746px;
    position: absolute;
    background: #ffffff;
    box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
  }
}
</style>
