<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="项目" prop="projectId">
        <el-select filterable v-model="queryParams.projectId" placeholder="请选择项目" clearable size="small">
          <el-option
            v-for="dict in projectLists"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属公司" prop="companyName">
        <el-select
          v-model="queryParams.companyId"
          placeholder="请选择所属公司"
          clearable
          filterable
          size="small"
        >
          <el-option
            v-for="item in companyQueryOptions"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="统计日期" prop="createDate">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
          @change="handleDateChange"
          clearable
          size="small">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['course:statistics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="statisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="统计日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="会员数量" align="center" prop="userCount" />
      <el-table-column label="会员黑名单数量" align="center" prop="userBlacklistCount" />
      <el-table-column label="观看次数" align="center" prop="watchCount" />
      <el-table-column label="完播次数" align="center" prop="completeWatchCount" />
      <el-table-column label="完播率" align="center" prop="completeRate" />
      <el-table-column label="答题人次" align="center" prop="answerCount" />
      <el-table-column label="正确人次" align="center" prop="correctCount" />
      <el-table-column label="正确率" align="center" prop="correctRate" />
      <el-table-column label="领取次数" align="center" prop="receiveCount" />
      <el-table-column label="领取金额" align="center" prop="receiveAmount" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listStatistics, getStatistics, exportStatistics } from "@/api/course/statistics";
import {getCompanyList, getCompanyUserList} from "@/api/company/companyUser";

export default {
  name: "Statistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员每日看课统计表格数据
      statisticsList: [],

      // 日期范围选择器值
      dateRange: [],

      // 日期选择器配置
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.pickerMinDate = minDate.getTime();
          if (maxDate) {
            this.pickerMinDate = '';
          }
        },
        disabledDate: (time) => {
          if (this.pickerMinDate !== '') {
            const day30 = 30 * 24 * 3600 * 1000;
            const maxTime = this.pickerMinDate + day30;
            const minTime = this.pickerMinDate - day30;
            return time.getTime() > maxTime || time.getTime() < minTime;
          }
          return false;
        }
      },

      //所属公司
      companyQueryOptions: [],
      //项目
      projectLists: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        watchCourseCount: null,
        completeWatchCount: null,
        watchCount: null,
        completeRate: null,
        answerCount: null,
        correctCount: null,
        correctRate: null,
        receiveCount: null,
        receiveAmount: null,
        userCount: null,
        userBlacklistCount: null,
        companyId: null,
        companyName: null,
        createDate: null,
        beginTime: null,
        endTime: null
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_course_project").then(response => {
      this.projectLists = response.data;
    })
    getCompanyList().then(response => {
      if (response.code === 200) {
        this.companyQueryOptions = response.data;
      }});
  },
  methods: {
    /** 查询会员每日看课统计列表 */
    getList() {
      this.loading = true;

      listStatistics(this.queryParams).then(response => {
        this.statisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch((error) => {
        this.loading = false;
      });
    },

    /** 处理日期范围变化 */
    handleDateChange(value) {
      this.dateRange = value;
      if (value && value.length === 2) {
        this.queryParams.beginTime = value[0];
        this.queryParams.endTime = value[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      // 重置日期范围
      this.dateRange = [];
      // 手动重置 companyId
      this.queryParams.companyId = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = {...this.queryParams};
      // 确保不传递dateRange参数
      delete queryParams.dateRange;

      this.$confirm('是否确认导出所有会员每日看课统计数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportStatistics(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
