<template>
  <div class="dashboard">
    <ComHead :tabs-index="0" />
    <div class="dashboard-content">
      <div class="dashboard-content-top flex">
        <div class="dashboard-content-top-left">
          <div class="dashboard-content-top-left-item">
            <div class="dashboard-content-top-left-item-top">
              <div class="dashboard-content-top-left-item-top-title">
                小时峰值分布
              </div>
              <div class="dashboard-content-top-left-item-top-time" @click="showDatePicker(showTimeMsg.HourlyPeak)">
                <el-icon color="#ffffff" size="18">
                  <Calendar />
                </el-icon>
                <el-icon color="#ffffff">
                  <CaretBottom />
                </el-icon>
              </div>
            </div>
            <div class="dashboard-content-top-left-item-bottom">
              <Echarts id="ww1" :option="hourlyPeakObj" />
            </div>
          </div>
          <div class="dashboard-content-top-left-item">
            <div class="dashboard-content-top-left-item-top">
              <div class="dashboard-content-top-left-item-top-title">
                运单趋势分析
              </div>
              <div class="dashboard-content-top-left-item-top-time" @click="showDatePicker(showTimeMsg.WaybillTrend)">
                <el-icon color="#ffffff" size="18">
                  <Calendar />
                </el-icon>
                <el-icon color="#ffffff">
                  <CaretBottom />
                </el-icon>
              </div>
            </div>
            <div class="dashboard-content-top-left-item-bottom">
              <Echarts id="ww12" :option="waybillTrendObj" />
            </div>
          </div>
          <div class="dashboard-content-top-left-item">
            <div class="dashboard-content-top-left-item-top">
              <div class="dashboard-content-top-left-item-top-title">
                始发站统计分析
              </div>
              <div class="dashboard-content-bottom-left-top-time" @click="showDatePicker(showTimeMsg.NodePeak)">
                <el-icon color="#ffffff" size="18">
                  <Calendar />
                </el-icon>
                <el-icon color="#ffffff">
                  <CaretBottom />
                </el-icon>
              </div>
            </div>
            <div class="dashboard-content-top-left-item-bottom">
              <Echarts id="ww4" :option="nodePeakObj" />
            </div>
          </div>
        </div>
        <!-- 地图 -->
        <div class="dashboard-content-top-center">
          <div class="dashboard-content-top-center-top">
            <div class="dashboard-content-top-center-top-list">
              <div class="dashboard-content-top-center-top-list-txt">重量</div>
              <div class="dashboard-content-top-center-top-list-num">{{ weight }}</div>
            </div>
            <div class="dashboard-content-top-center-top-list">
              <div class="dashboard-content-top-center-top-list-txt">航班数</div>
              <div class="dashboard-content-top-center-top-list-num">{{ flightNums }}</div>
            </div>
            <div class="dashboard-content-top-center-top-list">
              <div class="dashboard-content-top-center-top-list-txt">运单数</div>
              <div class="dashboard-content-top-center-top-list-num">{{ stockNums }}</div>
            </div>
          </div>
          <div class="dashboard-content-top-center-bottom">
            <div class="dashboard-content-top-center-bottom-title">航班动态跟踪</div>
            <div class="dashboard-content-top-center-bottom-content">
              <div class="dashboard-content-top-center-bottom-content-head">
                <div class="dashboard-content-top-center-bottom-content-head-list">公司/航班号</div>
                <div class="dashboard-content-top-center-bottom-content-head-list">航班状态</div>
                <div class="dashboard-content-top-center-bottom-content-head-list">经停/到站</div>
                <div class="dashboard-content-top-center-bottom-content-head-list">计划起飞</div>
                <div class="dashboard-content-top-center-bottom-content-head-list">实际起飞</div>
                <div class="dashboard-content-top-center-bottom-content-head-list">机型</div>
                <div class="dashboard-content-top-center-bottom-content-head-list">类型</div>
              </div>
              <div class="dashboard-content-top-center-bottom-content-bottom">
                <vue3-seamless-scroll :list="listData" :hover-stop="true" :hover="true" :step="0.3">
                  <div v-for="(item,index) in listData" :key="index" class="dashboard-content-top-center-bottom-content-bottom-list">
                    <div class="dashboard-content-top-center-bottom-content-bottom-list-txt">{{ item.flightNo }}</div>
                    <div class="dashboard-content-top-center-bottom-content-bottom-list-txt">{{ item.flightState }}</div>
                    <div class="dashboard-content-top-center-bottom-content-bottom-list-txt">{{ item.airport }}</div>
                    <div class="dashboard-content-top-center-bottom-content-bottom-list-txt">{{ item.planTime }}</div>
                    <div class="dashboard-content-top-center-bottom-content-bottom-list-txt">{{ item.acTime }}</div>
                    <div class="dashboard-content-top-center-bottom-content-bottom-list-txt">{{ item.planeType }}</div>
                    <div class="dashboard-content-top-center-bottom-content-bottom-list-txt">{{ item.KHT }}</div>
                  </div>
                </vue3-seamless-scroll>
              </div>
            </div>
          </div>
        </div>
        <div class="dashboard-content-top-right">
          <div class="dashboard-content-top-right-item">
            <div class="dashboard-content-top-right-item-top">
              <div class="dashboard-content-top-right-item-top-title">
                航司统计分析
              </div>
              <div class="dashboard-content-top-right-item-top-time" @click="showDatePicker(showTimeMsg.AirlineAbnormal)">
                <el-icon color="#ffffff" size="18">
                  <Calendar />
                </el-icon>
                <el-icon color="#ffffff">
                  <CaretBottom />
                </el-icon>
              </div>
            </div>
            <div class="dashboard-content-top-right-item-bottom">
              <Echarts id="a1" :option="airlineTrafficObj" />
            </div>
          </div>
          <div class="dashboard-content-top-right-item">
            <div class="dashboard-content-top-right-item-top">
              <div class="dashboard-content-top-right-item-top-title">
                航班动态统计
              </div>
              <div class="dashboard-content-top-right-item-top-time" @click="showDatePicker(showTimeMsg.AirlineTraffic)">
                <el-icon color="#ffffff" size="18">
                  <Calendar />
                </el-icon>
                <el-icon color="#ffffff">
                  <CaretBottom />
                </el-icon>
              </div>
            </div>
            <div class="dashboard-content-top-left-item-bottom">
              <Echarts id="ww44" :option="airlineObj" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <Dialog :flag="flag" msg-title="日期选择" @resetForm="resetForm" @submitForm="submitForm">
      <el-form :model="form" label-width="120px">
        <el-form-item label="开始日期">
          <el-date-picker :disabled-date="disabledStartDate" v-model="form.startDate" type="date" placeholder="请选择开始日期" size="default" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="结束日期">
          <el-date-picker :disabled-date="disabledEndDate" v-model="form.endDate" type="date" placeholder="请选择结束日期" size="default" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </el-form-item>
      </el-form>
    </Dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { Vue3SeamlessScroll } from "vue3-seamless-scroll";
import Echarts from "@/components/Echarts/commonChartsBar.vue";
import ComHead from "./components/comHead.vue";
import { Calendar, CaretBottom } from "@element-plus/icons-vue";
import Dialog from "@/components/dialog/index.vue";
import { usePublic, showTimeMsg } from "./hooks/usePublic";
import { parseTime, isValue } from "@/utils/validate";
import * as _ from "lodash";
const { optionLeft, airCompaneBaggage, airStutas, getPublicData } = usePublic();
const timePickerName = ref<number>(0);
const flag = ref<boolean>(false);
const hourlyPeakObj = ref<any>({});
const waybillTrendObj = ref<any>({});
const nodePeakObj = ref<any>({});
const airlineTrafficObj = ref<any>({});
const airlineObj = ref<any>({});
const flightNums = ref<number>(0);
const stockNums = ref<number>(0);
const weight = ref<number>(0);
const form = ref({
  startDate: parseTime(Date.now() - 24 * 60 * 60 * 1000 * 7, "{y}-{m}-{d}"),
  endDate: parseTime(Date.now(), "{y}-{m}-{d}"),
});
const listData = ref<any>([]);
const disabledStartDate = (time: Date) => {
  const timer: any = form.value.endDate;
  const data = new Date(timer);
  return time.getTime() > data.getTime();
};
const disabledEndDate = (time: Date) => {
  const timer: any = form.value.startDate;
  let data = new Date(timer);
  return data.getTime() > time.getTime();
};
const resetForm = () => {
  flag.value = false;
};

const submitForm = () => {
  flag.value = false;
  switch (timePickerName.value) {
    case showTimeMsg.HourlyPeak:
      hourlyPeakFunc();
      break;
    case showTimeMsg.WaybillTrend:
      waybillTrendFunc();
      break;
    case showTimeMsg.NodePeak:
      nodePeakFunc();
      break;
    case showTimeMsg.AirlineTraffic:
      airlineTrafficFunc();
      break;
    case showTimeMsg.AirlineAbnormal:
      airlineAbnormalFunc();
      break;
    default:
      break;
  }
};

const showDatePicker = (id?) => {
  flag.value = true;
  timePickerName.value = id;
};

const sortClass = (sortData) => {
  const groupBy = (array, f) => {
    let groups = {};
    array.forEach((o) => {
      let group = JSON.stringify(f(o));
      groups[group] = groups[group] || [];
      groups[group].push(o);
    });
    return Object.keys(groups).map((group) => {
      return groups[group];
    });
  };
  const sorted = groupBy(sortData, (item) => {
    return item.jobName;
  });
  return sorted;
};

const pubFunc = (listValues, target) => {
  const newObj = _.cloneDeep(target);
  const [jobTimes, stockNums, weights] = [<any>[], <any>[], <any>[]];
  listValues.forEach((item) => {
    jobTimes.push(item.jobTime);
    stockNums.push(item.stockNum);
    weights.push(item.weight);
  });
  newObj.xAxis.data = jobTimes;
  newObj.series[0].data = stockNums;
  newObj.series[1].data = weights;
  return newObj;
};

//获取小时峰值分布
const hourlyPeakFunc = async () => {
  const listValues = (await getPublicData(DATACONTENT_ID.jscCgHourlyPeakId, [
    { fd1: form.value.startDate, fd2: form.value.endDate },
  ])) as any;
  if (listValues && isValue(listValues)) {
    hourlyPeakObj.value = pubFunc(listValues, optionLeft);
  }
};

//运单趋势分析
const waybillTrendFunc = async () => {
  const listValues = (await getPublicData(DATACONTENT_ID.jscCgWaybillTrendId, [
    { fd1: form.value.startDate, fd2: form.value.endDate },
  ])) as any;
  if (listValues && isValue(listValues)) {
    waybillTrendObj.value = pubFunc(listValues, optionLeft);
  }
};

//始发站/目的站统计分析
const nodePeakFunc = async () => {
  const listValues = (await getPublicData(DATACONTENT_ID.jscAirlineAbnormalId, [
    { fd1: form.value.startDate, fd2: form.value.endDate, fttp: "国内进港" },
  ])) as any;
  if (listValues && isValue(listValues)) {
    const newObj = _.cloneDeep(airCompaneBaggage.option.baseOption);
    listValues.forEach((item) => {
      item.jobTime = item["airport"];
    });
    nodePeakObj.value = pubFunc(listValues, newObj);
  }
};

//航班数&运单数
const numbersFunc = async () => {
  const listValues = (await getPublicData(DATACONTENT_ID.jscCgNumbersId, [
    { fd1: form.value.startDate, fd2: form.value.endDate },
  ])) as any;
  if (listValues && isValue(listValues)) {
    const { flightNum, stockNum, weight } = listValues[0];
    flightNums.value = flightNum;
    stockNums.value = stockNum;
    weight.value = weight;
  }
};

//航司运量
const airlineTrafficFunc = async () => {
  const listValues = (await getPublicData(
    DATACONTENT_ID.jscCgAirlineTrafficId,
    [{ fd1: form.value.startDate, fd2: form.value.endDate }]
  )) as any;
  if (listValues && isValue(listValues)) {
    listValues.forEach((item) => {
      item.jobTime = item["IACACode"];
    });
    airlineTrafficObj.value = pubFunc(
      listValues,
      airCompaneBaggage.option.baseOption
    );
  }
};

//航班表格
const airlineAbnormalFunc = async () => {
  const listValues = (await getPublicData(DATACONTENT_ID.jscFlightDynamicsId, [
    {
      fd1: form.value.startDate,
      fd2: form.value.endDate,
      fttp: "国内进港",
    },
  ])) as any;
  if (listValues && isValue(listValues)) {
    listData.value = listValues;
  }
};

//航班动态统计
const airlineFunc = async () => {
  const listValues = (await getPublicData(DATACONTENT_ID.jscCgAirlineId, [
    {
      fd1: form.value.startDate,
      fd2: form.value.endDate,
    },
  ])) as any;
  if (listValues && isValue(listValues)) {
    const newObj = _.cloneDeep(airCompaneBaggage.option.baseOption);
    listValues.forEach((item) => {
      item.jobTime = item["nodeCode"];
      item.stockNum = item["flightNum"];
      item.weight = item["stockNum"];
    });
    airlineObj.value = pubFunc(listValues, newObj);
  }
};

onMounted(() => {
  hourlyPeakFunc();
  waybillTrendFunc();
  nodePeakFunc();
  numbersFunc();
  airlineTrafficFunc();
  airlineAbnormalFunc();
  airlineFunc();
});
</script>

<style lang="scss" scoped>
@import "./css/index.scss";
</style>
