<template>
  <div class="specialty-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="所属学院：">
            <org-select
              v-model="filter.orgId"
              placeholder="所属学院"
              :filter-param="{ orgId: userOrgId, withoutPrintingRoom: true }"
            ></org-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="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="name" label="专业"> </el-table-column>
        <el-table-column prop="orgName" label="所属学院"> </el-table-column>
        <el-table-column prop="createName" label="创建人">
          <span slot-scope="scope">
            {{ scope.row.userName }}（{{ scope.row.loginName }}）
          </span>
        </el-table-column>
        <!-- <el-table-column prop="createTime" label="创建时间">
          <span slot-scope="scope">
            {{ scope.row.createTime | timestampFilter }}
          </span>
        </el-table-column> -->
        <el-table-column
          class-name="action-column"
          label="操作"
          width="100"
          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', 'delete')"
              class="btn-danger"
              type="text"
              @click="toDelete(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>
    <!-- ModifyProfessional -->
    <modify-professional
      v-if="checkPrivilege('button', 'add')"
      ref="ModifyProfessional"
      :instance="curRow"
      @modified="getList"
    ></modify-professional>
  </div>
</template>

<script>
import { professionalListPage, deleteProfessional } from "../api";
import ModifyProfessional from "../components/ModifyProfessional.vue";

export default {
  name: "specialty-manage",
  components: { ModifyProfessional },
  data() {
    return {
      filter: {
        orgId: "",
        name: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      userOrgId: this.$ls.get("orgId", ""),
    };
  },
  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 professionalListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    toAdd() {
      this.curRow = {
        orgId: this.userOrgId,
      };
      this.$refs.ModifyProfessional.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyProfessional.open();
    },
    toDelete(row) {
      this.$confirm(`确定要删除专业【${row.name}】吗?`, "提示", {
        type: "warning",
      })
        .then(async () => {
          await deleteProfessional(row.id);
          this.$message.success("删除成功！");
          this.deletePageLastItem();
        })
        .catch(() => {});
    },
  },
};
</script>
