<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="tableNewList" :action="action" :set="set" />
    </div>
    <div v-if="total > 0" class="pagination">
      <el-pagination background layout="prev, pager, next" @current-change="currentChange" :total="total" />
    </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 {
      loading: false,
      asShow: false,
      optiondata: [],
      picShow: true, //图片表格切换
      action: 19,
      dataid: "collection_ecahrt1",
      listqueryTemplateID: DATACONTENT_ID.modeFlightvolume,
      eledata: 1,
      set: "",
      isShow: Echarts,
      tableList: [],
      tableListcop: [],
      tableNewList: [],
      listname: "",
      listHeader: [],
      total: 0,
      tableData: {
        time: [],
        data1: [],
        data2: [],
        kg: "",
        tyol: "",
      },
      formData: {
        fttp: "",
        flighttype: "",
        dateTime: "",
      },
      titleTop: "节点明细统计",
      levelList: [
        {
          name: "加货重量统计",
        },
        {
          name: "航班量统计",
        },
        {
          name: "特货统计",
        },
        {
          name: "货量统计",
        },
        {
          name: "拉货统计",
        },
        {
          name: "国内进港保障时间统计",
        },
      ],
      formItems: [
        {
          prop: "fttp",
          inputType: "select",
          placeholder: "指标分类",
          clearable: true,
          options: [
            {
              value: "数据缺失",
              label: "数据缺失",
            },
            {
              value: "流程节点缺失",
              label: "流程节点缺失",
            },
            {
              value: "时间倒置",
              label: "时间倒置",
            },
            {
              value: "时间相同",
              label: "时间相同",
            },
          ],
        },
        {
          prop: "flighttype",
          inputType: "select",
          placeholder: "节点名称",
          clearable: true,
          options: [
            {
              value: "安检",
              label: "安检",
            },
            {
              value: "货站交接",
              label: "货站交接",
            },
            {
              value: "交接复核",
              label: "交接复核",
            },
            {
              value: "库区到达",
              label: "库区到达",
            },
            {
              value: "机下交接",
              label: "机下交接",
            },
            {
              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: {
    // 监听数据变化，重绘折线图
    newObj: {
      deep: true,
      handler (newVal) {
        const { fttp, flighttype } = newVal
        if (fttp && !flighttype) {
          const datas = this.tableList.filter(item => item.index_class == fttp)
          this.tableListcop = _.chunk(datas, 10)
          this.tableNewList = this.tableListcop[0]
          this.total = datas.length
        } else if (!fttp && flighttype) {
          const datas = this.tableList.filter(item => item.node == flighttype)
          this.tableListcop = _.chunk(datas, 10)
          this.tableNewList = this.tableListcop[0]
          this.total = datas.length
        } else if (fttp && flighttype) {
          const datas = this.tableList.filter(item => item.node == flighttype && item.index_class == fttp)
          this.tableListcop = _.chunk(datas, 10)
          this.tableNewList = this.tableListcop[0]
          this.total = datas.length
        } else {
          const datas = this.tableList
          this.tableListcop = _.chunk(datas, 10)
          this.tableNewList = this.tableListcop[0]
          this.total = datas.length
        }
      },
    },
  },
  destroyed () { },
  methods: {
    getFormData (data) {
      this.newObj = data
      this.tableData.time = [];
      this.tableData.data1 = [];
      this.tableData.data2 = [];
      let option = [{
        fd1: data.dateTime,
        fd2: data.dateTime
      }]
      this.getQuery(option, null);
      this.listname =
        "节点明细统计" + '-' +
        data.fttp + '-' +
        data.flighttype + '-' +
        data.dateTime;
      this.listHeader = ["序号", "时间", "航班(班)", "环比(%)"];
    },
    //获取表格数据
    async getQuery (data, dat) {
      this.loading = true
      try {
        const { code, returnData } = await Query({
          id: DATACONTENT_ID.detailNode,
          dataContent: data,
        });
        if (code == 0) {
          this.tableData.time = [];
          this.tableData.data1 = [];
          this.tableData.data2 = [];
          this.total = returnData.length
          this.tableList = _.cloneDeep(returnData);
          this.tableListcop = _.chunk(_.cloneDeep(returnData), 10);
          this.tableNewList = this.tableListcop[0]
          this.loading = false
        } else {
          this.loading = false
          console.log(code)
        }
      } catch (error) {
        this.loading = false
        this.page--;
      }
    },
    //导出
    tableToExcel (refName) {
      if (this.tableList.length) {
        import("@/utils/Export2Excel").then((excel) => {
          const table = this.$refs[refName].$el.cloneNode(true);
          const fileName = `${this.listname}.xlsx`;
          exportToExcel(table, '节点明细统计', fileName);
        });
      } else {
        this.$message.error('暂无数据')
      }

    },
    //分页
    currentChange (page) {
      this.tableNewList = this.tableListcop[page - 1]
    },
    upset (data) {
      this.picShow = data;
    },
  },
  components: {
    Echarts,
    StatisticsHeader,
    Tableformbrs,
  },
};
</script>
<style lang="scss" scoped>
.airportInfo {
  position: relative;
  height: 100%;
  .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;
  }

  .pagination {
    position: absolute;
    z-index: 10;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: flex-end;
    width: 100%;
    ::v-deep .el-pager li.is-active {
      background: #ac014d;
    }
  }
}
</style>
