<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="96px">
      <el-form-item label="日期" prop="statDate">
        <el-date-picker
          v-model="queryParams.statDate"
          type="date"
          value-format="yyyyMMdd"
          placeholder="请选择日期"
          clearable
        />
      </el-form-item>
      <el-form-item label="团队名称" prop="deptId">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择团队"
          clearable
          filterable
          style="width: 220px"
        >
          <el-option
            v-for="item in deptOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="日期" align="center" prop="dateKey" />
      <el-table-column label="团队名称" align="center" prop="deptName" />
      <el-table-column label="实际充值积分（充值-退款）" align="center" prop="rechargeScore" />
      <el-table-column label="消耗积分" align="center" prop="score" />
      <el-table-column label="实际订单数量（成功）" align="center" prop="orderCount" />
      <el-table-column label="三方消耗tokens数量" align="center" prop="useTokens" />
    </el-table>

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

  </div>
</template>

<script>
import { listData } from "@/api/ai/data"
import { listDept } from "@/api/ai/dept"

export default {
  name: "TeamConsumeData",
  data() {
    return {
      // 遮罩层：首屏不自动查询，默认不展示加载状态
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 团队每日消耗统计表格数据
      dataList: [],
      // 团队下拉选项（仅二级部门）
      deptOptions: [],
      // 首次进入默认不查询
      searched: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        statDate: null,
        deptId: null
      },
    }
  },
  created() {
    this.loadSecondLevelDeptOptions()
  },
  methods: {
    /** 加载二级部门选项 */
    loadSecondLevelDeptOptions() {
      listDept({ status: "0" }).then(response => {
        const allDeptList = response.data || []
        this.deptOptions = allDeptList.filter(item => this.isSecondLevelDept(item))
      })
    },
    /** 判断是否为二级部门（一级部门的直属子部门） */
    isSecondLevelDept(dept) {
      if (!dept || !dept.ancestors) {
        return false
      }
      return String(dept.ancestors).split(",").filter(Boolean).length === 2
    },
    /** 查询团队每日消耗统计列表 */
    getList() {
      if (!this.searched) {
        this.loading = false
        this.dataList = []
        this.total = 0
        return
      }
      this.loading = true
      listData(this.queryParams).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (!this.queryParams.statDate || !this.queryParams.deptId) {
        this.$modal.msgWarning("请先填写日期和团队名称后再搜索")
        return
      }
      this.searched = true
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.searched = false
      this.loading = false
      this.dataList = []
      this.total = 0
    }
  }
}
</script>
