<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker
          v-model="queryParams.startDate"
          type="date"
          placeholder="选择开始日期"
          value-format="yyyy-MM-dd"
          clearable
        />
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker
          v-model="queryParams.endDate"
          type="date"
          placeholder="选择结束日期"
          value-format="yyyy-MM-dd"
          clearable
        />
      </el-form-item>
      <el-form-item label="部门路径" prop="departmentPath">
        <el-input
          v-model="queryParams.departmentPath"
          placeholder="请输入部门路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="20" class="mb20" style="background: #f8f9fa; padding: 15px; border-radius: 4px; margin-bottom: 20px;">
      <el-col :span="8">
        <div class="summary-item">
          <span class="label">整体成单数：</span>
          <span class="value">{{ summaryData.totalOrders || 0 }}</span>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="summary-item">
          <span class="label">历史整体进粉量：</span>
          <span class="value">{{ summaryData.totalIns || 0 }}</span>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="summary-item">
          <span class="label">渗透率：</span>
          <span class="value">{{ summaryData.penetrationRate || '0%' }}</span>
        </div>
      </el-col>
    </el-row>

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

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="statDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门路径" align="center" prop="departmentPath" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="当日总承接数" align="center" prop="dailyTotalAccepted" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="当日总成单数" align="center" prop="dailyTotalOrders" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="当日转化率" align="center" prop="dailyConversionRate" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="当日及时单占比" align="center" prop="dailyTimelyOrderRatio" width="180" :show-overflow-tooltip="true" />
      <el-table-column label="当日非及时单占比" align="center" prop="dailyNonTimelyOrderRatio" width="180" :show-overflow-tooltip="true" />
    </el-table>

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

<script>
import { listDepartmentStatistics, exportDepartmentStatistics, getDepartmentStatisticsSummary } from "@/api/wecom/departmentStatistics"

export default {
  name: "DepartmentStatistics",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门统计数据表格数据
      dataList: [],
      // 汇总数据
      summaryData: {
        totalOrders: 0,
        totalIns: 0,
        penetrationRate:  0
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startDate: undefined,
        endDate: undefined,
        departmentPath: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询部门统计数据列表 */
    getList() {
      this.loading = true
      listDepartmentStatistics(this.queryParams).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
      getDepartmentStatisticsSummary(this.queryParams).then(response => {
        this.summaryData = response.data
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/wecom/departmentStatistics/export', {
        ...this.queryParams
      }, `部门统计数据_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
