<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      学练宝缴费记录
    </h2>
    <div class="m-core">
      <el-form :inline="true" :model="searchForm">
        <el-form-item prop="period">
          <el-select
            placeholder="周期"
            clearable
            v-model.trim="searchForm.period"
            style="width: 100% !important"
          >
            <el-option label="月度" value="MONTH"></el-option>
            <!-- <el-option label="季度" value="QUARTERLY"></el-option> -->
            <el-option label="半年" value="YEAR_HALF"></el-option>
            <el-option label="年度" value="YEAR"></el-option>
            <!-- <el-option label="固定天数" value="DAY"></el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item prop="organId">
          <el-select
            class="multiple"
            v-model.trim="searchForm.organId"
            filterable
            clearable
            placeholder="请选择分部"
          >
            <el-option
              v-for="(item, index) in selects.branchs"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="memberRankId">
          <el-cascader
            v-model.trim="searchForm.memberRankId"
            :options="memberRankList"
            :props="{ value: 'id', label: 'name',  }"
            :show-all-levels="false"
            collapse-tags
            clearable
            placeholder="请选择学练宝版本"
          ></el-cascader>
        </el-form-item>
        <el-form-item prop="visitTime">
          <el-date-picker
            v-model.trim="searchForm.visitTime"
            style="width: 410px"
            type="daterange"
            :picker-options="{
              firstDayOfWeek: 1
            }"
            :default-time="['00:00:00', '23:59:59']"
            range-separator="至"
            start-placeholder="创建开始日期"
            end-placeholder="创建结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="search" type="danger">搜索</el-button>
          <el-button @click="onReSet" type="primary">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="id"
            label="续费编号"
          ></el-table-column>
          <el-table-column align="center" prop="organName" label="分部名称">
          </el-table-column>
          <el-table-column align="center" prop="name" label="学练宝版本">
          </el-table-column>
          <el-table-column align="center" prop="period" label="周期">
            <template slot-scope="scope">
              {{ scope.row.period | memberEnumType }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="memberNum" label="周期数">
          </el-table-column>
          <el-table-column
            align="center"
            prop="subjectName"
            label="缴费金额(元)"
          >
            <template slot-scope="scope">
              {{ scope.row.actualAmount | moneyFormat(true) }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="type" label="已缴费人数/总人数">
            <template slot-scope="scope">
              {{ scope.row.paymentUserNum }}/{{ scope.row.userNum }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="status"
            label="审核状态"
            width="100px"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.status | auditType }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建人" prop="operatorName">
          </el-table-column>
          <el-table-column
            align="center"
            prop="createTime"
            label="创建时间"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="remark"
            label="备注"
            width="220px"
          >
            <template slot-scope="scope">
              <overflow-text width="200px" :text="scope.row.remark" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                v-if="
                  scope.row.status == 'REJECT' &&
                    $helpers.permission('cloudCoachPaymentProgram/update')
                "
                @click="onUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                type="text"
                v-if="
                  scope.row.status != 'REJECT' &&
                    $helpers.permission(
                      'studentCloudCoachPaymentDetails/queryPage'
                    )
                "
                @click="onDetail(scope.row)"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>

    <el-dialog
      title="激活学练宝"
      :visible.sync="recordVisible"
      width="1000px"
      v-if="recordVisible"
      append-to-body
    >
      <recordDetail :detail="selectDetail" @close="recordVisible = false" />
    </el-dialog>

    <el-dialog
      title="修改学练宝缴费"
      width="500px"
      :visible.sync="memberVisible"
    >
      <createMember
        ref="addMember"
        v-if="memberVisible"
        type="update"
        :multipleSelection="selectDetail"
        @close="memberVisible = false"
        @submited="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="memberVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddMember">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import recordDetail from "@/views/studentManager/modals/recordDetail";
import { cloudCoachPaymentProgramQueryPage } from "./api";
import { getmemberRankList } from '@/views/categroyManager/productSystem/api'
import createMember from "./modals/createMember";
import { getTimes } from "@/utils";
export default {
  components: { pagination, recordDetail, createMember },
  data() {
    return {
      recordVisible: false,
      memberVisible: false,
      searchForm: {
        memberRankId: [],
        visitTime: [],
        period: null,
        organId: null
      },
      memberRankList: [],
      tableList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      selectDetail: null
    };
  },
  async mounted() {
    await this.$store.dispatch("setBranchs");
    this.getMemberList()
    this.getList();
  },
  methods: {
    async getMemberList() {
      try {
        const { data } = await getmemberRankList()
        this.memberRankList = data || [];
      } catch (e) {
        console.log(e);
      }
    },
    async getList() {
      try {
        const { visitTime, memberRankId, ...search } = this.searchForm;
        let rankId = null
        if(memberRankId && memberRankId.length > 0) {
          rankId = memberRankId[memberRankId.length - 1]
        }
        const res = await cloudCoachPaymentProgramQueryPage({
          ...search,
          memberRankId: rankId,
          ...getTimes(visitTime, ["startTime", "endTime"]),
          page: this.rules.page,
          rows: this.rules.limit
        });
        this.rules.total = res.data.total;
        this.tableList = res.data.rows;
      } catch (e) {}
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.searchForm = {
        visitTime: [],
        period: null
      };
      this.search();
    },
    onDetail(row) {
      this.selectDetail = row;
      this.recordVisible = true;
    },
    onUpdate(row) {
      this.memberVisible = true;
      this.selectDetail = row;
    },
    submitAddMember() {
      this.$refs.addMember.submit();
    }
  }
};
</script>
