<template>
  <div class="airportInfo">
    <div class="header">
      <div
        :class="action === index ? 'navs' : 'nav'"
        v-for="(item, index) in levelList"
        :key="index"
        @click="up(index)"
      >
        {{ item.name }}
      </div>
    </div>
    <div class="variable">
      <StatisticsHeader
        :title="titleTop"
        :items="formItems"
        :data="formData"
        :eledata="eledata"
        with-setting
        :withSetting="false"
        :withExport="true"
        @getFormData="getFormData"
        @export="tableToExcel"
      />
    </div>
    <div class="echart">
      <Echarts :id="dataid" :option="tableData" />
    </div>
  </div>
</template>

<script>
import Echarts from "../components/echart/index.vue";
import StatisticsHeader from "../components/echart/statisticsHeader.vue";
import { export_json_to_excel } from "@/utils/Export2Excel";
import { Query } from "@/api/webApi";
export default {
  name: "ChartsBar",
  data() {
    return {
      action: 0,
      dataid: "collection_ecahrt0",
      listqueryTemplateID: DATACONTENT_ID.modeLoadingweight,
      eledata: null,
      set: "",
      isShow: Echarts,
      tableList: [],
      listname: "",
      listHeader: [],
      tableData: {
        time: [],
        data1: [],
        data2: [],
        kg: "",
        tyol: "",
      },
      formData: {
        airport: "",
        dateTime: "",
      },
      titleTop: "加货重量统计",
      levelList: [
        {
          name: "加货重量统计",
        },
        {
          name: "航班量统计",
        },
        {
          name: "特货统计",
        },
        {
          name: "货量统计",
        },
      ],
      formItems: [
        {
          prop: "fttp",
          inputType: "select",
          placeholder: "国内国际",
          requiredWarning: "请先选择国内国际",
          clearable: true,
          options: [
            {
              value: "国内离港",
              label: "国内离港",
            },
            {
              value: "国际离港",
              label: "国际离港",
            },
          ],
        },
        {
          prop: "kht",
          inputType: "select",
          placeholder: "客货类型",
          requiredWarning: "请先选择客货类型",
          clearable: true,
          options: [
            {
              value: "客机",
              label: "客机",
            },
            {
              value: "货机",
              label: "货机",
            },
            {
              value: "其它",
              label: "其它",
            },
            {
              value: "全部",
              label: "全部",
            },
          ],
        },
        {
          prop: "td",
          inputType: "select",
          placeholder: "统计维度",
          requiredWarning: "请先选择统计时间维度",
          clearable: true,
          options: [
            {
              value: "月",
              label: "月",
            },
            {
              value: "年",
              label: "年",
            },
          ],
        },
        {
          prop: "spe",
          inputType: "select",
          placeholder: "特货类型",
          requiredWarning: "",
          clearable: true,
          disabled: true,
          options: [
            {
              value: "贵重物品",
              label: "贵重物品",
            },
            {
              value: "鲜活",
              label: "鲜活",
            },
            {
              value: "动物",
              label: "动物",
            },
            {
              value: "邮件",
              label: "邮件",
            },
            {
              value: "锂电池",
              label: "锂电池",
            },
            {
              value: "全部",
              label: "全部",
            },
          ],
        },
        {
          prop: "set",
          inputType: "select",
          placeholder: "统计运单",
          requiredWarning: "",
          clearable: true,
          disabled: true,
          options: [
            {
              value: "1",
              label: "统计运单",
            },
            {
              value: "2",
              label: "统计重量",
            },
          ],
        },
        {
          prop: "dateTime",
          inputType: "datePicker",
          clearable: true,
          width: "240px",
          options: [],
        },
      ],
    };
  },
  mounted() {
    // this.getQuery([]);
  },
  watch: {
    // 监听数据变化，重绘折线图
    option: {
      deep: true,
      handler(newVal) {},
    },
  },
  destroyed() {},
  methods: {
    up(index) {
      this.action = index;
      if (index === 0) {
        this.eledata = 0;
        this.dataid = "collection_ecahrt0";
        this.listqueryTemplateID = DATACONTENT_ID.modeLoadingweight;
        this.titleTop = "加货重量统计";
        this.tableData.time = [];
        this.tableData.data1 = [];
        this.tableData.data2 = [];
        this.formItems[3].disabled = true;
        this.formItems[4].disabled = true;
        this.formItems[0].options = [
          {
            value: "国内离港",
            label: "国内离港",
          },
          {
            value: "国际离港",
            label: "国际离港",
          },
        ];
      } else if (index === 1) {
        this.eledata = 1;
        this.dataid = "collection_ecahrt1";
        this.formItems[3].disabled = true;
        this.formItems[4].disabled = true;
        this.formItems[0].options = [
          {
            value: "国内离港",
            label: "国内离港",
          },
          {
            value: "国际离港",
            label: "国际离港",
          },
          {
            value: "国内进港",
            label: "国内进港",
          },
          {
            value: "国际进港",
            label: "国际进港",
          },
        ];
        this.tableData.time = [];
        this.tableData.data1 = [];
        this.tableData.data2 = [];
        this.listqueryTemplateID = DATACONTENT_ID.modeFlightvolume;
        // this.getQuery();
        this.titleTop = "航班量统计";
      } else if (index === 2) {
        this.dataid = "collection_ecahrt2";
        this.formItems[3].disabled = false;
        this.formItems[4].disabled = false;
        this.tableData.time = [];
        this.tableData.data1 = [];
        this.tableData.data2 = [];
        this.formItems[0].options = [
          {
            value: "国内离港",
            label: "国内离港",
          },
          {
            value: "国际离港",
            label: "国际离港",
          },
          {
            value: "国内进港",
            label: "国内进港",
          },
          {
            value: "国际进港",
            label: "国际进港",
          },
        ];
        this.eledata = 2;
        this.listqueryTemplateID = DATACONTENT_ID.modeSpecialgoods;
        this.titleTop = "特货统计";
      } else if (index === 3) {
        this.dataid = "collection_ecahrt3";
        this.tableData.time = [];
        this.tableData.data1 = [];
        this.tableData.data2 = [];
        this.formItems[0].options = [
          {
            value: "国内离港",
            label: "国内离港",
          },
          {
            value: "国际离港",
            label: "国际离港",
          },
          {
            value: "国内进港",
            label: "国内进港",
          },
          {
            value: "国际进港",
            label: "国际进港",
          },
        ];
        this.formItems[3].disabled = true;
        this.formItems[4].disabled = true;
        this.eledata = 3;
        this.titleTop = "货量统计";
        this.listqueryTemplateID = DATACONTENT_ID.modeCargostatistics;
      }
    },
    getFormData(data) {
      this.tableData.time = [];
      this.tableData.data1 = [];
      this.tableData.data2 = [];
      if (this.action === 2) {
        this.set = data.set;
        let option = [
          {
            fttp: data.fttp,
            kht: data.kht,
            td: data.td,
            fd1: data.dateTime[0],
            fd2: data.dateTime[1],
            spe: data.spe,
          },
        ];
        this.getQuery(option, data.set);
      } else {
        let option = [
          {
            fttp: data.fttp,
            kht: data.kht,
            td: data.td,
            fd1: data.dateTime[0],
            fd2: data.dateTime[1],
          },
        ];
        this.getQuery(option, null);
      }
      if (this.action === 0) {
        this.listname =
          "加货重量统计" +
          data.fttp +
          data.kht +
          data.td +
          data.dateTime[0] +
          "--" +
          data.dateTime[1];
        this.listHeader = ["序号", "时间", "重量"];
      } else if (this.action === 1) {
        this.listname =
          "航班量统计" +
          data.fttp +
          data.kht +
          data.td +
          data.dateTime[0] +
          "--" +
          data.dateTime[1];
        this.listHeader = ["序号", "时间", "重量"];
      } else if (this.action === 2) {
        this.listname =
          "特货统计" +
          data.fttp +
          data.kht +
          data.td +
          data.spe +
          data.dateTime[0] +
          "--" +
          data.dateTime[1];
        console.log();
        if (data.set == 1) {
          this.listHeader = ["序号", "时间", "单"];
        } else if (data.set == 2) {
          this.listHeader = ["序号", "时间", "重量"];
        }
      } else if (this.action === 3) {
        this.listname =
          "货量统计" +
          data.fttp +
          data.kht +
          data.td +
          data.dateTime[0] +
          "--" +
          data.dateTime[1];
        this.listHeader = ["序号", "时间", "重量"];
      }
    },
    //获取表格数据
    async getQuery(data, dat) {
      try {
        const { code, returnData } = await Query({
          id: this.listqueryTemplateID,
          dataContent: data,
        });
        if (code == 0) {
          this.tableList = returnData.listValues;
          returnData.listValues.forEach((element) => {
            if (this.action === 0) {
              this.tableData.data2.push(element.weight ? element.weight : 0);
              this.tableData.time.push(element.dat);
              this.tableData.kg = "单位：吨";
            } else if (this.action === 1) {
              this.tableData.data2.push(
                element.flightNum ? element.flightNum : 0
              );
              this.tableData.time.push(element.dat);
              this.tableData.kg = "单位：班";
            } else if (this.action === 2) {
              this.tableData.time.push(element.dat);
              if (dat == 1) {
                this.tableData.data2.push(
                  element.flightNum ? element.flightNum : 0
                );
                this.tableData.kg = "单位：单";
              } else if (dat == 2) {
                this.tableData.kg = "单位:吨";
                this.tableData.data2.push(element.weight ? element.weight : 0);
              }
            } else if (this.action === 3) {
              this.tableData.data2.push(element.weight ? element.weight : 0);
              this.tableData.time.push(element.fdt);
              this.tableData.kg = "单位：吨";
            }
            this.tableData.data1 = [];
          });
          let ar = [];
          this.tableData.data1.push(0);
          for (let index = 0; index < this.tableData.data2.length; index++) {
            // const element =
            //   (this.tableData.data2[index + 1] - this.tableData.data2[index]) /
            //   this.tableData.data2[index];
            // console.log(element);
            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));
            }
          }
          // this.tableData = returnData.listValues;
          // console.log(this.tableData[0])
        }
      } catch (error) {
        this.page--;
      }
    },
    tableToExcel() {
      import("../../../utils/Export2Excel").then((excel) => {
        // 设置导出表格的头部
        const tHeader = this.listHeader;
        // 将要导出的数据进行一个过滤
        /**
         * 源数据导入到excel的数据每一条重新拼成一个数组,数组里的每个元素就是filterVal里的每个字段
         */
        const data = this.tableList.map((item, index) => {
          if (this.action === 0) {
            return [index + 1, item.dat, item.weight];
          } else if (this.action === 1) {
            return [index + 1, item.dat, item.flightNum];
          } else if (this.action === 2) {
            if (this.set == 1) {
              return [index + 1, item.dat, item.flightNum];
            } else if (this.set == 2) {
              return [index + 1, item.dat, item.weight];
            }
          } else if (this.action === 3) {
            return [index + 1, item.fdt, item.weight];
          }
        });
        // 调用我们封装好的方法进行导出Excel
        excel.export_json_to_excel({
          // 导出的头部
          header: tHeader,
          // 导出的内容
          data,
          // 导出的文件名称
          filename: this.listname,
          // 导出的表格宽度是否自动
          autoWidth: true,
          // 导出文件的后缀类型
          bookType: "xlsx",
        });
      });
    },
  },
  components: {
    Echarts,
    StatisticsHeader,
  },
};
</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: 64px;
  }
  .echart {
    width: 100%;
    height: 710px;
    position: absolute;
    background: #ffffff;
    box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.1);
    border-radius: 4px;
  }
}
</style>
