<template>
  <div class="school-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="启用/禁用：" label-width="90px">
          <el-select
            v-model="filter.schoolId"
            placeholder="选择学校"
            filterable
            clearable
          >
            <el-option
              v-for="item in schools"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-button type="primary" @click="filterSchool">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          :loading="loading"
          @click="toSyncSchool"
          >机构同步</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"></el-table-column>
        <el-table-column
          prop="name"
          label="学校名称"
          min-width="200"
        ></el-table-column>
        <el-table-column
          prop="code"
          label="学校代码"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="code"
          label="学校域名"
          min-width="100"
        ></el-table-column>
        <el-table-column class-name="action-column" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button class="btn-primary" type="text" @click="toSet(scope.row)"
              >设置</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- ModifySchool -->
    <modify-school
      ref="ModifySchool"
      :instance="curSchool"
      @modified="getSchools"
    ></modify-school>
    <!-- ModifySchoolSet -->
    <modify-school-set
      ref="ModifySchoolSet"
      :school="curSchool"
    ></modify-school-set>
  </div>
</template>

<script>
import { schoolList, schoolSync } from "../api";
import ModifySchool from "../components/ModifySchool.vue";
import ModifySchoolSet from "../components/ModifySchoolSet.vue";

export default {
  name: "school-manage",
  components: { ModifySchool, ModifySchoolSet },
  data() {
    return {
      schools: [],
      dataList: [],
      filter: {
        schoolId: null
      },
      curSchool: {},
      loading: false
    };
  },
  mounted() {
    this.getSchools();
  },
  methods: {
    async getSchools() {
      const data = await schoolList();
      this.schools = data || [];
      this.dataList = this.schools;
    },
    filterSchool() {
      if (!this.filter.schoolId) {
        this.dataList = this.schools;
        return;
      }

      this.dataList = this.schools.filter(
        item => item.id === this.filter.schoolId
      );
    },
    async toSyncSchool() {
      if (this.loading) return;
      this.loading = true;

      const res = await schoolSync().catch(() => {});
      this.loading = false;

      if (!res) return;

      this.$message.success("操作成功");
      this.filter.schoolId = null;
      this.getSchools();
    },
    toEdit(row) {
      this.curSchool = row;
      this.$refs.ModifySchool.open();
    },
    toSet(row) {
      this.curSchool = row;
      this.$refs.ModifySchoolSet.open();
    }
  }
};
</script>
