<template>
  <div class="training-plan-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="专业：">
            <professional-select
              v-model="filter.professionalId"
              placeholder="专业"
            ></professional-select>
          </el-form-item>
          <el-form-item label="培养方案名称：">
            <el-input
              v-model.trim="filter.name"
              placeholder="培养方案名称"
              clearable
            ></el-input>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'add')"
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="toAdd"
          >新增</el-button
        >
      </div>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="55"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="name" label="培养方案名称" min-width="300">
        </el-table-column>
        <el-table-column prop="professionalName" label="专业" min-width="200">
        </el-table-column>
        <el-table-column prop="targetCount" label="培养目标" width="80">
          <span slot-scope="scope"> {{ scope.row.targetCount }}个 </span>
        </el-table-column>
        <el-table-column prop="requirementCount" label="毕业要求" width="80">
          <span slot-scope="scope"> {{ scope.row.requirementCount }}项 </span>
        </el-table-column>
        <el-table-column prop="courseCount" label="课程体系" width="80">
          <span slot-scope="scope"> {{ scope.row.courseCount }}门 </span>
        </el-table-column>
        <el-table-column prop="createName" label="创建人" min-width="200">
          <span slot-scope="scope">
            {{ scope.row.createRealName }}（{{ scope.row.createLoginName }}）
          </span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="170"
          fixed="right"
        >
          <template slot-scope="scope">
            <!-- <el-button
              v-if="checkPrivilege('link', 'edit')"
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            > -->
            <el-button
              v-if="checkPrivilege('link', 'copy')"
              class="btn-primary"
              type="text"
              @click="toCopy(scope.row)"
              >复制</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'delete')"
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'CultureProgramDetail')"
              class="btn-primary"
              type="text"
              @click="toDetail(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ModifyTrainingPlan -->
    <modify-training-plan
      v-if="checkPrivilege('button', 'add') || checkPrivilege('link', 'edit')"
      ref="ModifyTrainingPlan"
      :instance="curRow"
      @modified="getList"
    ></modify-training-plan>
    <!-- DetailTrainingPlan -->
    <detail-training-plan
      v-if="checkPrivilege('link', 'CultureProgramDetail')"
      ref="DetailTrainingPlan"
      :row-data="curRow"
      @modified="getList"
    ></detail-training-plan>
  </div>
</template>

<script>
import {
  trainingPlanListPage,
  deleteTrainingPlan,
  copyTrainingPlan,
} from "../api";
import ModifyTrainingPlan from "../components/training-plan/ModifyTrainingPlan.vue";
import DetailTrainingPlan from "../components/training-plan/DetailTrainingPlan.vue";

export default {
  name: "training-plan-manage",
  components: { ModifyTrainingPlan, DetailTrainingPlan },
  data() {
    return {
      filter: {
        professionalId: "",
        name: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await trainingPlanListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifyTrainingPlan.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyTrainingPlan.open();
    },
    async toDelete(row) {
      const confirm = await this.$confirm(
        `确定要删除培养方案【${row.name}】吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await deleteTrainingPlan(row.id);
      this.$message.success("删除成功！");
      this.deletePageLastItem();
    },
    async toCopy(row) {
      const confirm = await this.$confirm(
        `确定要复制培养方案【${row.name}】吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      await copyTrainingPlan(row.id);
      this.$message.success("复制成功！");
      this.getList();
    },
    toDetail(row) {
      this.curRow = row;
      this.$refs.DetailTrainingPlan.open();
    },
  },
};
</script>
