<template>
  <!-- 给魏演示使用 2023-20-16 -->
  <div class="AbstractProgram">
    <br />
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>稿前校对统计</el-breadcrumb-item>
    </el-breadcrumb>
    <br />
    <el-card class="box-card">
      <el-form
        :model="form"
        size="small"
        :inline="true"
        label-width="120px"
        class="demo-form-inline"
      >
        <el-form-item label="日期">
          <el-date-picker
            v-model="form.time"
            type="datetimerange"
            value-format="YYYY-MM-DD HH:mm:ss"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :disabled-date="disabledDate"
            placeholder="日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关键词">
          <el-input v-model="form.keyword" placeholder="检索关键词" clearable />
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="primary" @click="onSubmit" :loading="load">
            查询
          </el-button>
          <el-button type="primary" @click="onExport" :loading="load">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <br />
    <el-card>
      <div id="echartsEle"></div>
      <br />
      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{ backgroundColor: '#f4f5f7', color: '#606266' }"
      >
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="displayName"
          label="用户名称"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="createTime"
          label="校对时间"
        />
        <el-table-column
          show-overflow-tooltip
          align="center"
          prop="textLength"
          label="校对字数"
        />
      </el-table>
      <br />
      <el-pagination
        @current-change="change"
        background
        :default-current-page="page"
        layout="prev, pager, next"
        :total="total"
      />
    </el-card>
  </div>
</template>

<script>
// @ is an alias to /src
import { getYoumeiList, getYoumeiProportion } from '@/api/newMdeiaApi';
import dayjs from 'dayjs';

import config from "@/config/index";
import * as echarts from 'echarts/core';
import { PieChart } from 'echarts/charts';
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  ToolboxComponent,
  LegendComponent,
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  PieChart,
  CanvasRenderer,
  ToolboxComponent,
  LegendComponent,
]);
let T = undefined;
export default {
  name: 'Proofread',
  data() {
    return {
      page: 1,
      size: 10,
      total: 0,
      load: false,
      tableData: [],
      form: {
        keyword: '',
        time: [],
      },
      myChart: undefined,
    };
  },
  mounted() {
    let h = Date.now();
    h = h - 86400000;
    const Time = dayjs(h);
    const time1 = Time.format('YYYY-MM-DD') + ' 00:00:00';
    const time2 = Time.format('YYYY-MM-DD') + ' 23:59:59';
    this.form = {
      keyword: '',
      time: [time1, time2],
    };
    this.getDate();
    this.getEcharts();
  },
  computed: {},
  methods: {
    getDate() {
      if (T) window.clearTimeout(T);
      T = setTimeout(() => {
        this.load = true;
        if (T) window.clearTimeout(T);
      }, 200);
      getYoumeiList({
        page: this.page,
        size: this.size,
        keyword: this.form.keyword,
        startTime: this.form.time[0],
        endTime: this.form.time[1],
      })
        .then(r => {
          if (T) window.clearTimeout(T);
          this.load = false;
          this.total = r.total || 0;
          this.tableData = r.records || [];
        })
        .catch(() => {
          if (T) window.clearTimeout(T);
          this.tableData = [];
          this.load = false;
        });
    },
    getEcharts() {
      getYoumeiProportion({
        keyword: this.form.keyword,
        startTime: this.form.time[0],
        endTime: this.form.time[1],
      }).then(r => {
        const li = r || [];
        const ele = document.getElementById('echartsEle');
        !this.myChart && (this.myChart = echarts.init(ele));
        this.myChart.resize({
          height: 400,
        });
        this.myChart.setOption({
          legend: {
            top: 'bottom',
          },
          title: {
            text: '调用次数占比',
          },
          series: [
            {
              type: 'pie',
              radius: [50, 120],
              center: ['50%', '50%'],
              data: li.map(v=>{
                return {
                    name: v.userId,
                    value: v.count
                }
              }),
            },
          ],
        });
      });
    },
    disabledDate(time) {
      return time.getTime() > Date.now();
    },
    change(page) {
      this.page = page;
      this.getDate();
    },
    onSubmit() {
      this.page = 1;
      this.getDate();
      this.getEcharts();
    },
    onExport() {
        window.open(config.base.youmei + "/export-log?keyword=" +this.form.keyword + '&startTime=' + this.form.time[0] + '&endTime=' + this.form.time[1]);
    },
  },
  components: {},
};
</script>

<style>
.AbstractProgram {
    padding: 1em;
}
.el-pagination .btn-next .el-icon,
.el-pagination .btn-prev .el-icon {
  margin: 0 auto;
}
#echartsEle {
  width: 100%;
  max-width: 800px;
  min-width: 400px;
  margin: 0 auto;
}
</style>
