<template>
  <div class="container">
    <div class="flex">
      <div class="card2">
        <div class="card-header">
          <i class="icon-line" />
          <div class="card-title">知识类型统计</div>
        </div>
        <div class="card-content">
          <Chart :option="chartOption1" rotation style="flex: 1" />
        </div>
      </div>
      <div class="card2">
        <div class="card-header">
          <i class="icon-line" />
          <div class="card-title">来源单位统计</div>
        </div>
        <div class="card-content">
          <div class="box2">
            <Chart :option="chartOption2" rotation style="flex: 1" />
            <div class="box3">
              <div v-for="(item, index) in legendData" :key="index" class="box-item">
                <i class="icon" :style="{ background: item.itemStyle.color }" />
                <div class="text1">{{ item.name }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="flex" style="margin-top: 15px">
      <div class="card2">
        <div class="card-header">
          <i class="icon-line" />
          <div class="card-title">发布时间统计</div>
        </div>
        <div class="card-content">
          <div class="box1">
            <div class="date-box">
              <div
                v-for="(item, index) in dateOption"
                :key="index"
                :class="item.value === dateType ? 'date-tag tag-active' : 'date-tag'"
                @click="handleChangeDate(item.value)"
              >
                {{ item.name }}
              </div>
            </div>
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              clearable
              class="date-picker"
            />
          </div>
          <Chart :option="chartOption3" rotation style="flex: 1" />
        </div>
      </div>
      <div class="card2">
        <div class="card-header" style="justify-content: space-between">
          <div class="title-box1">
            <i class="icon-line" />
            <div class="card-title">知识查阅TOP5</div>
          </div>
          <div class="text1" style="padding-right: 8px;">统计时间：{{ statisticsDate }}</div>
        </div>
        <div class="card-content" style="padding: 0 8px">
          <div class="data-list">
            <div v-for="(item, index) in dataList" :key="index" class="data-list-item">
              <i :class="getIconClass(index)" />
              <div class="td1" :title="item.reportName">{{ item.reportName }}</div>
              <div class="td2">来源单位：{{ item.publishingUnit }}</div>
              <div class="td3">
                <i class="icon-eye" />
                {{ item.time }}次
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup name="knowledgeAnalysis">
import { option1, option2, option3 } from './options';
import { deepClone } from '@/utils';
import { toRefs } from 'vue';
import { fetchReports } from '@/api/knowledge';

const proxy = getCurrentInstance()?.proxy;
// 事件类型统计
const dateOption = ref([
  { name: '今日', value: '1' },
  { name: '本周', value: '2' },
  { name: '本月', value: '3' },
  { name: '全年', value: '4' }
]);
let dateType = ref('1');
let dateRange = ref([]);
let chartOption1 = reactive(deepClone(option1));
const handleChangeDate = (type: string) => {
  dateType.value = type;
  getEventTypeData();
};

const getEventTypeData = () => {
  chartOption1.xAxis.data = ['自然灾害', '事故灾害', '突发卫生公\n共事件', '突发社会安\n全事件'];
  chartOption1.series[0].data = [45, 123, 87, 83];
};

let chartOption2 = reactive(option2);
let legendData = ref([]);
let chartOption3 = reactive(deepClone(option3));
// 知识查询
let loading = ref(false);
let statisticsDate = ref('');
let queryParams = reactive({
  pageNum: 1,
  pageSize: 5,
  sortBy: 'publishDate',
  sortOrder: 'asc'
});
let dataList = ref([]);

const getDataList = () => {
  loading.value = true;
  fetchReports(queryParams)
    .then((res) => {
      let arr = [263, 231, 198, 167, 123];
      res.data.forEach((item, index) => {
        item.time = arr[index];
      });
      dataList.value = res.data;
    })
    .finally(() => {
      loading.value = false;
    });
};
const getIconClass = (index) => {
  let classList = ['icon-top1', 'icon-top2', 'icon-top3', 'icon-top4', 'icon-top5'];
  return classList[index];
};
const initData = () => {
  getEventTypeData();
  const data1 = [
    { value: 154, name: '市应急管理局', percent: '45.56%', itemStyle: { color: '#2c81ff' } },
    { value: 45, name: '市自然资源局', percent: '13.31%', itemStyle: { color: '#62d9ab' } },
    { value: 62, name: '市医疗保障局', percent: '18.34%', itemStyle: { color: '#feb20d' } },
    { value: 77, name: '市教育局', percent: '22.79%', itemStyle: { color: '#fe6a6a' } }
  ];
  legendData.value = data1;
  chartOption2.series[0].data = data1;
  chartOption3.xAxis[0].data = ['2020年', '2021年', '2022年', '2023年', '2024年'];
  chartOption3.series[0].data = [48, 64, 79, 63, 84];
  getDataList();
  const date = new Date();
  const year = date.getFullYear();
  let month = date.getMonth() + 1; // 月份从0开始，需要加1
  let day = date.getDate();
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  statisticsDate.value = `${year}年${month}月${day}日`;
};

onMounted(() => {
  initData();
});
</script>

<style lang="scss" scoped>
.container {
  padding: 15px 0;
  .flex {
    display: flex;
    justify-content: space-between;
    padding: 0 17px;
  }
  .card2 {
    flex: 1;
    height: 367px;
    border: 1px solid #e6f2ff;
    background-image: linear-gradient(to bottom, #ffffff 0%, #ebf4ff 100%);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    padding-bottom: 10px;
    &:nth-child(2) {
      margin-left: 20px;
    }
    .card-header {
      flex-shrink: 0;
      display: flex;
      align-items: center;
      height: 40px;
      padding: 10px 5px 16px;
      .icon-line {
        display: inline-block;
        width: 6px;
        height: 17px;
        background-image: linear-gradient(180deg, #00fce7 0%, #2c81ff 100%);
        border-radius: 0 3px 0 0;
        margin-right: 7px;
      }
      .card-title {
        font-size: 16px;
        color: rgba(0, 0, 0, 0.85);
      }
      .title-box1 {
        display: flex;
        align-items: center;
      }
      .text1 {
        font-size: 14px;
        color: rgba(0, 0, 0, 0.85);
      }
    }
    .card-content {
      flex: 1;
      display: flex;
      flex-direction: column;
      padding: 0 22px;
      overflow: hidden;
      .box1 {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        .date-box {
          width: 289px;
          height: 40px;
          background-color: #edf1f8;
          border-radius: 4px;
          display: flex;
          align-items: center;
          padding: 0 4px;
          .date-tag {
            height: 32px;
            width: 60px;
            font-size: 14px;
            color: #7a8293;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            margin-left: 13px;
            &:first-child {
              margin-left: 0;
            }
            &:hover {
              color: #2c81ff;
              background: #ffffff;
              box-shadow: 0 2px 1px 0 rgba(36, 41, 51, 0.04);
              border-radius: 3px;
            }
          }
          .tag-active {
            color: #2c81ff;
            background: #ffffff;
            box-shadow: 0 2px 1px 0 rgba(36, 41, 51, 0.04);
            border-radius: 3px;
          }
        }
        :deep(.date-picker) {
          margin-left: 29px;
          width: 242px;
          height: 40px;
          border-radius: 3px;
        }
      }
      .box2 {
        height: 100%;
        display: flex;
        overflow: hidden;
        .box3 {
          display: flex;
          flex-direction: column;
          justify-content: center;
          width: 200px;
          .box-item {
            display: flex;
            align-items: center;
            padding: 11px 0;
            .icon {
              width: 8px;
              height: 8px;
              border-radius: 50%;
              margin-right: 10px;
            }
            .text1 {
              font-size: 14px;
              color: rgba(0, 0, 0, 0.65);
              margin-right: 23px;
            }
            .text2 {
              font-size: 14px;
              color: rgba(0, 0, 0, 0.65);
              width: 35px;
              margin-right: 35px;
            }
            .text3 {
              font-size: 14px;
              color: rgba(0, 0, 0, 0.65);
            }
          }
        }
      }
      .data-list {
        .data-list-item {
          display: flex;
          align-items: center;
          width: 100%;
          height: 48px;
          border-radius: 0 4px 4px 0;
          margin-top: 14px;
          background-image: linear-gradient(270deg, rgba(36, 104, 242, 0.08) 0%, rgba(119, 161, 246, 0) 100%);
          &:nth-child(1) {
            background-image: linear-gradient(270deg, rgba(249, 74, 61, 0.08) 0%, rgba(253, 129, 112, 0) 100%);
          }
          &:nth-child(2) {
            background-image: linear-gradient(270deg, rgba(242, 117, 12, 0.08) 0%, rgba(246, 169, 104, 0) 100%);
          }
          &:nth-child(3) {
            background-image: linear-gradient(270deg, rgba(242, 186, 0, 0.08) 0%, rgba(249, 225, 146, 0) 100%);
          }
          .icon-top1 {
            background: url('@/assets/images/top1.png') no-repeat;
          }
          .icon-top2 {
            background: url('@/assets/images/top2.png') no-repeat;
          }
          .icon-top3 {
            background: url('@/assets/images/top3.png') no-repeat;
          }
          .icon-top4 {
            background: url('@/assets/images/top4.png') no-repeat;
          }
          .icon-top5 {
            background: url('@/assets/images/top5.png') no-repeat;
          }
          .icon-top1,
          .icon-top2,
          .icon-top3,
          .icon-top4,
          .icon-top5 {
            width: 28px;
            height: 34px;
            background-size: 100% 100%;
            margin-right: 5px;
            flex-shrink: 0;
          }
          .td1 {
            flex: 3;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            padding: 0 5px;
            font-size: 14px;
            color: rgba(0, 0, 0, 0.65);
          }
          .td2 {
            flex: 2;
            padding: 0 5px;
            font-size: 12px;
            color: rgba(0, 0, 0, 0.45);
          }
          .td3 {
            flex-shrink: 0;
            width: 100px;
            padding: 0 20px 0 5px;
            font-size: 12px;
            color: rgba(0, 0, 0, 0.65);
            display: flex;
            align-items: center;
          }
          .icon-eye {
            width: 16px;
            height: 12px;
            background: url('@/assets/images/eye.png') no-repeat;
            background-size: 100% 100%;
            margin-right: 4px;
          }
        }
      }
    }
  }
  .table {
    margin-top: 10px;
    :deep(.el-table__inner-wrapper) {
      .el-table__header-wrapper th {
        height: 35px !important;
      }
    }
    :deep(.el-table__body) {
      td.el-table__cell {
        height: 35px !important;
        padding: 0 !important;
      }
    }
  }
}
</style>
