<template>
  <div class="app-container" v-loading="pageloading">
    <el-row :gutter="24">
      <el-col :span="12">
        <el-card>
          <div slot="header" class="clearfix"><span class="tline">文档类别统计</span></div>
          <!--  -->
          <div class="tjbox2" v-if="isPieChart">
            <PieChart
              height="285px"
              :data="toolTypeData"
              :legendData="toolTypeLegendData"
              legendBottom="5"
              chartRef="toolType"
              :radius="['40%', '70%']"
            />
          </div>
        </el-card><!--el-card-->
      </el-col><!--el-col-->
      <el-col :span="12">
        <el-card>
          <div slot="header" class="clearfix"><span class="tline">文档来源统计</span></div>
          <div class="tjbox2" v-if="isPieChart">
            <PieChart
              height="285px"
              :data="toolSourceData"
              :legendData="toolSourceLegendData"
              legendBottom="5"
              :radius="['40%', '70%']"
              chartRef="toolStatus"
            />
          </div>
        </el-card><!--el-card-->
      </el-col><!--el-col-->
    </el-row><!--el-row-->


    <el-row :gutter="20">
      <el-col :span="24">
        <el-card>
          <div slot="header" class="clearfix"><span class="tline">文档下载统计</span></div>
          <div class="tjbox2">
            <el-form label-width="70px" ref="queryForm" :model="queryParams">
              <div class="search">
                <div class="sl">
                  <el-form-item label="下载时间">
                    <el-date-picker
                      v-model="dateRange"
                      value-format="yyyy-MM-dd"
                      type="daterange"
                      range-separator="-"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    ></el-date-picker>
                  </el-form-item>
                  <el-form-item label="文档编号" prop="docCode">
                    <el-input
                      v-model="queryParams.docCode"
                      placeholder="请输入文档编号"
                      clearable
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="文档名称" prop="docName">
                    <el-input
                      v-model="queryParams.docName"
                      placeholder="请输入文档名称"
                      clearable
                      @clear="handleQuery"
                      @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                </div>
                <div class="sr">
                  <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh-left" @click="reset">重置</el-button>
                </div>
              </div>
            </el-form>
            <!-- <img src="images/tjt6.jpg" /> -->
          </div>
          <div class="rt">
            <el-table v-loading="downLoading" :data="toolDownStatList">
              <el-table-column type="index" label="序号" width="50" align="center"/>
              <el-table-column label="文档编号" key="docCode" prop="docCode" />
              <el-table-column label="文档名称" key="docName" prop="docName"  :show-overflow-tooltip="true" />
              <el-table-column label="下载次数" align="center" key="docDownNum" prop="docDownNum"/>
              <el-table-column
                label="操作"
                align="center"
                width="100"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button type="text" icon="el-icon-info" @click="handleDetail(scope.row)">详情</el-button>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="downTotal>0"
              :total="downTotal"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getDocDownStatistics"
            />
          </div><!--rt 右-->
        </el-card><!--el-card-->
      </el-col><!--el-col-->
    </el-row><!--el-row-->
    <!-- <DownStatDetail :show.sync="detailDrawerOpen" :toolId="toolId" :toolName="toolName" /> -->
    <el-dialog
      :title="`${toolName}下载详情统计`"
      :visible.sync="detailDrawerOpen"
      width="85%"
      append-to-body
      @open="openEvent"
      @close="detailDrawerOpen = false"
    >
      <el-table :data="toolDownDetailStatList" v-loading="loading">
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <el-table-column label="附件名称" key="attName" prop="attName"  :show-overflow-tooltip="true" />
        <el-table-column label="下载用户" key="nickName" prop="nickName" width="120" />
        <el-table-column label="下载次数" align="center" key="toolDownNum" prop="toolDownNum" width="120" />
        <el-table-column label="下载时间" align="center" key="createTime" prop="createTime" width="200" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize"
        @pagination="getDocDownDetailStatistics"
      />
    </el-dialog>


  </div>
</template>

<script>
import { getStatistics } from "@/api/document/document";
import { listDocCount,userDownList } from "@/api/tool/downloadCount";

import PieChart from './PieChart'
export default {
  name: 'statistic',
  dicts: ['tool_type', 'flow_status', 'tool_source'],
  components: {
    PieChart
  },
  data() {
    return {
      toolDownDetailStatList: [],
      activeName: 'first',
      toolTypeData: [
        /*{ value: 320, name: '检测型' },
        { value: 240, name: '服务型' }*/
      ],
      // toolTypeLegendData:['检测型', '服务型'],
      toolTypeLegendData:[],
      toolStatusData: [
       /* { value: 212, name: '已保存' },
        { value: 76, name: '审批中' },
        { value: 32, name: '审批不通过' },
        { value: 971, name: '已发布' }*/
      ],
      // toolStatusLegendData: ['已保存','审批中','审批不通过','已发布'],
      toolStatusLegendData: [],
      toolSourceData: [
        /*{ value: 20, name: 'QQ' },
        { value: 60, name: '微信' },
        { value: 18, name: '官网' },
        { value: 47, name: '熟人介绍' }*/
      ],
      // toolSourceLegendData: ['QQ', '微信', '官网', '熟人介绍'],
      toolSourceLegendData: [],
      // 日期范围
      dateRange: [],
      toolDownStatList: [],
      downTotal: 0,
      downLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        docCode: null,
        docName: null,
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        businessId: ''
      },
      // 工具下载统计详情组件控制属性
      detailDrawerOpen: false,
      // 工具id
      toolId: '',
      // 工具名称
      toolName: '',
      total: 0,
      loading: false,
      isPieChart: false,
    }
  },
  created () {
    this.getCountData()
    this.getDocDownStatistics()
  },
  methods: {
    openEvent() {
      this.getDocDownDetailStatistics()
    },
    getDocDownDetailStatistics() {
      let self = this
      this.loading = true
      userDownList(this.addDateRange(this.queryParams1)).then(res => {
        self.total = res?.total
        self.toolDownDetailStatList = res?.rows || []
        self.loading = false
      }).catch(() => { self.loading = false });
    },
    // 工具下载重置查询
    reset() {
      this.toolDownStatList = []
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /**
     * 工具下载搜索
     */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getDocDownStatistics()
    },
    /** 统计类型、类别、状态**/
    getCountData(){
      let self = this
      // 类别
      self.toolTypeData = []
      self.toolTypeLegendData = []
      // 来源
      self.toolSourceData = []
      self.toolSourceLegendData = []
      self.pageloading = true

      getStatistics(this.queryParams).then(res => {
        if(res.data){

          const countType = JSON.parse(JSON.stringify(res.data.countType)).filter(item => item.name)
          //类别
          self.toolTypeData = countType
          self.toolTypeData.forEach(item => {
            if(item && item.name){
              self.toolTypeLegendData.push(item.name)
            }
          })


          const countSource = JSON.parse(JSON.stringify(res.data.countSource)).filter(item => item.name)

          //来源
          self.toolSourceData = countSource
          self.toolSourceData.forEach(item => {
            if(item && item.name){
              self.toolSourceLegendData.push(item.name)
            }
          })
          self.isPieChart= true
          self.pageloading= false
        }
       /* console.info("countToolType==============", this.dict.type.tool_type)
        console.info("res===========", res)*/
      }).catch((err) => {
        self.isPieChart= true
        self.pageloading= false
        console.error(err)
      });
    },
    handleDetail(row){
      this.$set(this.queryParams1, "businessId", row.businessId)
      this.detailDrawerOpen = true
    },
    /**
     * 获取工具下载列表数据
     */
    getDocDownStatistics() {
      let self = this
      this.downLoading = true
      listDocCount(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        self.downTotal = res.total
        self.toolDownStatList = res?.rows || []
        self.downLoading = false
      }).catch(() => { self.downLoading = false });
    },
  }
}
</script>

<style scoped>

</style>
