<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      考级管理
    </h2>
    <div class="m-core">
      <!-- 搜索标题 -->
      <save-form
        :inline="true"
        class="searchForm"
        ref="searchForm"
        @submit="onSearch"
        @reset="onReSet"
        :model="searchForm"
      >
        <el-form-item prop="keyword">
          <el-input
            v-model.trim="searchForm.keyword"
            clearable
            placeholder="请输入名称/编号"
            @keyup.enter.native="
              e => {
                e.target.blur();
                $refs.searchForm.save();
                search();
              }
            "
          ></el-input>
        </el-form-item>
        <el-form-item prop="orderDate">
          <el-date-picker
            v-model.trim="searchForm.orderDate"
            style="width: 410px"
            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 prop="status">
          <el-select
            class="multiple"
            v-model.trim="searchForm.status"
            filterable
            clearable
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in levelStatusList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </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>

      <el-button
        v-permission="'degree/save'"
        type="primary"
        @click="onGradeOperation('create')"
        style="margin-bottom:20px"
      >
        新增考级
      </el-button>

      <div class="tableWrap">
        <el-table
          :data="tableList"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
        >
          <el-table-column width="90px" align="center" prop="id" label="编号">
            <template slot-scope="scope">
              <copy-text>{{ scope.row.id }}</copy-text>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="考级名称" align="center">
            <template slot-scope="scope">
              <overflow-text :text="scope.row.name"></overflow-text>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="orderNo"
            label="考级时间"
            :min-width="100"
          >
            <template slot-scope="scope">
              {{ scope.row.startTime | dayjsFormat }}-{{
                scope.row.endTime | dayjsFormat
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="transNo" label="人数">
            <template slot-scope="scope">
              <p>报名人数：{{ scope.row.registerNum }}人</p>
              <p>报名人次：{{ scope.row.registerTotal }}人</p>
              <!-- <p>购课学员：{{ scope.row.purchaseCourse }}人</p> -->
            </template>
          </el-table-column>
          <el-table-column align="center" prop="city" label="适用分部">
            <template slot-scope="scope">
              <overflow-text
                :text="formatOrangNames(scope.row.organNames)"
              ></overflow-text>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="school" label="状态">
            <template slot-scope="scope">
              <copy-text>{{ scope.row.status | levelStatus }}</copy-text>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button
                v-if="$helpers.permission('/levelManager')"
                type="text"
                @click="
                  () => {
                    $router.push({
                      path: '/levelManager',
                      query: {
                        id: scope.row.id,
                        title: scope.row.name
                      }
                    });
                  }
                "
                >详情</el-button
              >
              <el-button
                v-if="$helpers.permission('degree/update')"
                type="text"
                @click="onGradeOperation('update', scope.row)"
                >修改</el-button
              >
              <el-button type="text" @click="onCreateQrCode(scope.row)"
                >链接</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="pageInfo.total"
          :page.sync="pageInfo.page"
          :limit.sync="pageInfo.limit"
          :page-sizes="pageInfo.page_size"
          @pagination="getList"
        />
      </div>
    </div>

    <gradeOperation ref="gradeOperation" @getList="getList" />

    <!-- 预报名链接 -->
    <qr-code v-model="codeStatus" title="考级报名链接" :codeUrl="qrCodeUrl" />
  </div>
</template>
<script>
import { vaildStudentUrl } from "@/utils/validate";
import qrCode from "@/components/QrCode/index";
import pagination from "@/components/Pagination/index";
import gradeOperation from "./model/grade-operation";
import { degreePage } from "./levelManageApi";
import { getTimes } from "@/utils";
import { levelStatusList } from "@/utils/searchArray";
import dayjs from "dayjs";
export default {
  components: { pagination, gradeOperation, qrCode },
  name: "helpCategory",
  data() {
    return {
      codeStatus: false,
      qrCodeUrl: null,
      levelStatusList,
      searchForm: {
        keyword: null,
        status: null,
        startTime: null,
        endTime: null,
        orderDate: []
      },
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      tempTreeList: []
    };
  },
  mounted() {
    this.$store.dispatch("setBranchs");
    this.getList();
  },
  methods: {
    formatOrangNames(val) {
      return val ? val.join(",") : "";
    },
    onSearch() {
      this.pageInfo.page = 1;
      this.getList();
    },
    onReSet() {
      // 重置搜索
      this.orderDate = null;
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    async getList() {
      try {
        let { orderDate, ...rest } = this.searchForm;
        let params = {
          ...rest,
          page: this.pageInfo.page,
          rows: this.pageInfo.limit,
          ...getTimes(orderDate, ["startTime", "endTime"])
        };
        await degreePage(params).then(res => {
          let result = res.data;
          if (res.code == 200) {
            this.tableList = result.rows;
            this.pageInfo.total = result.total;
          }
        });
      } catch {}
    },
    onGradeOperation(type, row) {
      const dialogType = type || "create";
      this.$refs.gradeOperation.openDialog(dialogType, row);
    },
    onCreateQrCode(row) {
      this.codeStatus = true;
      this.qrCodeUrl = vaildStudentUrl() + "/#/level-apply-new?id=" + row.id;
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-image-viewer__close {
  color: #fff;
  opacity: 1;
}
::v-deep .el-date-editor.el-input {
  width: 100% !important;
}
::v-deep .el-select {
  width: 100% !important;
}
::v-deep .el-table .cell {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>
