<template>
  <div class="container">
    <div>
      <el-button
        type="primary"
        @click="
          noticeVisible = true;
          title = '添加非自主授课院校';
        "
        style="float: right;margin-bottom:5px"
        >添加非自主授课院校</el-button
      >
    </div>
    <el-dialog
      title="添加非自主授课院校"
      :visible.sync="noticeVisible"
      @close="cancel('schoolTypeInfo')"
    >
      <el-form :model="schoolTypeInfo" ref="schoolTypeInfo">
        <div class="line">
          <el-form-item label="院校名称" prop="school">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input type="textarea" autosize v-model="schoolTypeInfo.school">
            </el-input>
          </el-form-item>
          <el-form-item label="院校简称" prop="schoolAbbr">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="schoolTypeInfo.schoolAbbr"
            >
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('schoolTypeInfo')">取 消</el-button>
        <el-button type="primary" @click="submitForm('schoolTypeInfo')"
          >确 定</el-button
        >
        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>

    <el-dialog
      title="编辑院校信息"
      :visible.sync="editNoticeVisible"
      @close="cancel('editSchoolInfo')"
    >
      <el-form :model="editSchoolInfo" ref="editSchoolInfo">
        <div class="line">
          <el-form-item label="院校名称" prop="school">
            <el-input type="textarea" autosize v-model="editSchoolInfo.school">
            </el-input>
          </el-form-item>
          <el-form-item label="院校简称" prop="schoolAbbr">
            <el-input
              type="textarea"
              autosize
              v-model="editSchoolInfo.schoolAbbr"
            >
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('editSchoolInfo')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editSchoolInfo')"
          >确 定</el-button
        >

        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>

    <div class="table-container">
      <el-table
        ref="productTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="院校名称" align="center">
          <template slot-scope="scope">{{ scope.row.school }}</template>
        </el-table-column>
        <el-table-column label="院校简称" align="center">
          <template slot-scope="scope">{{ scope.row.schoolAbbr }}</template>
        </el-table-column>

        <!-- @click="
                handleupdateNoticeAll(scope.row.id, scope.row.schoolName,scope.row.schoolBrief)
              " -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click="
                handleUpdateSchoolAll(
                  scope.row.id,
                  scope.row.school,
                  scope.row.schoolAbbr,
                  scope.row.type
                )
              "
              size="mini"
              >编辑</el-button
            >
            <!-- <el-button @click="handleDel(scope.row.id)" size="mini"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
         @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next,jumper"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getSchoolTypeAll,
  getSchoolTypeAdd,
  getSchoolTypeUpdate,
  // recruitSchoolAll,
  // recruitSchoolAdd,
  // recruitSchoolUpdate,
  recruitSchoolDel
} from "@/api/cso2";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  type: 2
};
export default {
  name: "unIndependentTeach",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      listSelected: null,
      pageSize: null,
      pageNum: null,
      total: null,
      checked: true,
      listLoading: true,
      multipleSelection: [],
      formLabelWidth: "120px",
      options: [],
      teacherId: "",
      noticeVisible: false,
      editNoticeVisible: false,
      title: "",

      // recruitCourseInfo:{},
      schoolTypeInfo: {
        type: 2
      },
      editSchoolInfo: {
        school: " ",
        schoolAbbr: " ",
        type: 2
      },
      //  editRecruitCourse:{
      //   school: " ",
      //   courseName: " ",
      //   courseCode: " ",
      //   csoName: " ",
      //   csoCode: " ",

      // },

      updateId: ""
    };
  },
  methods: {
    getData() {
      getSchoolTypeAll(this.listQuery).then(res => {
        console.log(res, "getTypeData");
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
        // console.log(this.list, "this.list,codeAll");
        this.$forceUpdate();
      });
    },
    getSelectedForm() {
      const data = this.list;
      data.forEach((item, index) => {
        if (item.csoName == " " || item.csoCode == " ") {
          this.list = data.splice(index, 1);
        }
      });
    },

    handleCurrentChange(val) {
      // 翻页
      this.listQuery.pageNum = val;
      this.getData();
    },
     handleSizeChange(val){
      //切换展示条数
       this.listQuery.pageSize = val;
       this.getData();
    },

    handleStuDetail(id) {
      // 测试用的
      this.$router.push({
        path: "/pms/stuDetail",
        query: {
          id: id
        }
      });
      // console.log(123);
    },

    submitForm(formName) {
      if (this.updateId) {
        this.schoolTypeInfo.id = this.updateId;
      }
      this.$confirm("请认真核对院校名称和院校简称！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        getSchoolTypeAdd(this.schoolTypeInfo).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.noticeVisible = false;

          this.getData();
          this.$forceUpdate();
        });
      });
    },
    submitEditForm(formName) {
      this.editSchoolInfo.id = this.updateId;

      this.$confirm("请认真核对院校名称和院校简称！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        //拿到整个公告
        getSchoolTypeUpdate(this.editSchoolInfo).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.editNoticeVisible = false;

          this.getData();
          this.$forceUpdate();
        });
      });
    },
    // handleDel(id) {
    //   //   console.log(id, "handleDel");
    //   this.$confirm("是否删除", "提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(() => {
    //     recruitSchoolDel(id).then(response => {
    //       this.$message({
    //         type: "success",
    //         message: "删除成功",
    //         duration: 1000
    //       });

    //       this.getData();
    //       this.$forceUpdate();
    //     });
    //   });
    // },
    //handleUpdateNoticeAll(id, schoolName,schoolBrief) {
    handleUpdateSchoolAll(id, school, schoolAbbr, type) {
      this.editNoticeVisible = true;
      // this.title = "编辑公告";
      this.updateId = id;
      this.editSchoolInfo.school = school;
      this.editSchoolInfo.schoolAbbr = schoolAbbr;
      this.type = type;
      // console.log(this.updateId, noticeContent);
    },
    cancel(formName) {
      this.noticeVisible = false;
      this.editNoticeVisible = false;
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.getData();
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}
.handleStuDetail {
  cursor: pointer;
}
</style>
