<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      扣费记录
    </h2>
    <save-form
      :inline="true"
      class="searchForm"
      ref="searchForm"
      @submit="search"
      @reset="reset"
      :saveKey="'tenantTradeManager'"
      :model.sync="searchForm"
    >
      <el-form-item prop="search">
        <el-input
          placeholder="课程名/编号"
          clearable
          type="text"
          v-model.trim="searchForm.search"
        ></el-input>
      </el-form-item>
      <el-form-item prop="organId">
        <el-select
          class="multiple"
          v-model.trim="searchForm.organId"
          filterable
          multiple
          collapse-tags
          clearable
          placeholder="请选择分部"
          @change="onBranchChange"
        >
          <el-option
            v-for="(item, index) in organList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="type">
        <el-select
          v-model.trim="searchForm.type"
          clearable
          filterable
          @clear="onClear('type')"
          placeholder="课程类型"
        >
          <el-option
            v-for="(item, index) in courseType"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="teacherId">
          <remote-search
            :commit="'setTeachers'"
            v-model="searchForm.teacherId"
          />
        </el-form-item>

      <el-form-item prop="status">
        <el-select
          v-model.trim="searchForm.status"
          clearable
          filterable
          @clear="onClear('status')"
          placeholder="课程状态"
        >
          <el-option
            v-for="(item, index) in courseStatus"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="deductState">
        <el-select
          v-model.trim="searchForm.deductState"
          clearable
          filterable
          @clear="onClear('deductState')"
          placeholder="扣费状态"
        >
          <el-option v-for="(item, key) of chargingStatus" :key="key" :label="item" :value="key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="createTimer">
        <el-date-picker
          v-model.trim="searchForm.createTimer"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="课程开始时间"
          end-placeholder="课程结束时间"
          :picker-options="{ firstDayOfWeek: 1 }"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button native-type="submit" type="danger">搜索</el-button>
        <el-button native-type="reset" type="primary">重置</el-button>
      </el-form-item>
    </save-form>
    <!-- <div style="font-size: 14px; padding-bottom: 10px">
      扣费总额：<span style="color: var(--color-primary)">1,000元</span>
      <i style="width: 10px; display: inline-block"></i>
      冻结金额：<span style="color: var(--color-primary)">1,000元</span>
    </div> -->
    <!-- 列表 -->
    <div class="tableWrap">
      <el-table
        :data="tableList"
        :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
      >
        <el-table-column align="center" label="课程编号" prop="courseId">
        </el-table-column>
        <el-table-column align="center" label="所属分部" prop="organName">
        </el-table-column>
        <el-table-column align="center" label="课程类型" prop="type">
          <template slot-scope="scope">
            {{ scope.row.type | courseTypeFormat }}
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="姓名" prop="tenantId">
        </el-table-column> -->
        <el-table-column align="center" label="老师" prop="teacherId">
        </el-table-column>
        <el-table-column align="center" label="课程人数" prop="studentNum">
        </el-table-column>
        <el-table-column align="center" label="课程时间">
          <template slot-scope="scope">
            {{ scope.row.classDate }}
            {{ scope.row.startClassTime }}~{{ scope.row.endClassTime }}
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="网络教室剩余时长" prop="productName">
        </el-table-column> -->
        <el-table-column align="center" label="课程状态">
          <template slot-scope="scope">
            {{ scope.row.actualAmount | coursesStatus }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="扣费状态">
          <template slot-scope="scope">
            {{ scope.row.deductState | chargingStatus }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="冻结金额（元）">
          <template slot-scope="scope">
            {{ scope.row.amount | moneyFormat }}
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :saveKey="'tenantTradeManager'"
        sync
        :total.sync="pageInfo.total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.limit"
        :page-sizes="pageInfo.page_size"
        @pagination="getList"
      />
    </div>

    <el-dialog title="订单详情" :visible.sync="orderVisible" width="1200px">
      <order-info />
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import { tenantInfoQueryPage } from "../organManager/api";
import { tenantCloudCourseRecord } from "./api";
import { courseType, courseStatus } from "@/utils/searchArray";
import { chargingStatus } from '@/constant'
import OrderInfo from './model/orderInfo'
import { getTimes } from "@/utils";
const initSearch = {
  type: null,
  organId: null,
  type: null,
  teacherId: null,
  status: null,
  deductState: null,
  createTimer: [],
};
export default {
  components: { pagination, OrderInfo },
  data() {
    const baseTenantId = sessionStorage.getItem('baseTenantId')
    return {
      baseTenantId,
      courseType,
      courseStatus,
      chargingStatus,
      tableList: [],
      orderVisible: false,
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      searchForm: { ...initSearch },
      tenantList: []
    };
  },
  async mounted() {
    await this.getTenant()
    this.getList();
  },
  methods: {
    async getTenant() {
      try {
        const res = await tenantInfoQueryPage({ page: 1, rows: 999 }, )
        this.tenantList = res.data?.rows || []
      } catch(e) {}
    },
    async getList() {
      try {
        let { createTimer, ...reset } = this.searchForm;
        const res = await tenantCloudCourseRecord({
          ...reset,
          ...getTimes(createTimer, ["startDate", "endDate"], "YYYY-MM-DD"),
          page: this.pageInfo.page,
          rows: this.pageInfo.limit,
        });
        this.pageInfo.total = res.total;
        this.tableList = res.rows;
      } catch (e) {}
    },
    search() {
      this.pageInfo.page = 1;
      this.$refs.searchForm.save(this.searchForm);
      this.$refs.searchForm.save(this.pageInfo, "page");
      this.getList();
    },
    reset() {
      this.searchForm = { ...initSearch };
      this.search();
    },
    openService(row) {
      this.orderVisible = true
    },
  },
  filters: {
    tenantOrderStatus(val) {
      const template = {
        0: "待支付",
        1: "已支付",
        2: "支付失败"
      }
      return template[val]
    }
  }
};
</script>
<style lang="scss" scoped>
.courseMask .el-dialog__body {
  padding-bottom: 0;
}
</style>
