<template>
  <div class="container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch('listQuery')"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          :data="list"
          ref="listQuery"
          size="small"
          label-width="140px"
          @keyup.enter.native="handleSearchList()"
        >
          <el-checkbox v-model="isMatch">未匹配课程代码</el-checkbox>

          <el-form-item label="课程代码：" prop="courseCode">
            <el-input
              style="width: 203px"
              v-model="listQuery.courseCode"
              placeholder=""
            ></el-input>
          </el-form-item>

          <el-form-item label="课程名称：" prop="courseName">
            <el-input
              style="width: 203px"
              v-model="listQuery.courseName"
              placeholder=""
            ></el-input>
          </el-form-item>

          <el-form-item label="学生所在学校：" prop="school	">
            <el-input
              style="width: 203px"
              v-model="listQuery.school"
              placeholder=""
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <div>
      <!-- <el-checkbox v-model="isMatch">未匹配课程代码</el-checkbox>
      <el-button type="primary" @click="getSelectedForm()">确认</el-button>
      <el-button type="primary" @click="resetForm('listQuery')">重置</el-button> -->
      <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('recruitCourseInfo')"
    >
      <el-form :model="recruitCourseInfo" ref="recruitCourseInfo">
        <div class="line">
          <el-form-item label="cso课程代码（必填）" prop="csoCode">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="recruitCourseInfo.csoCode"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="cso课程名称" prop="csoName">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="recruitCourseInfo.csoName"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="招生院校（必填）" prop="school">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="recruitCourseInfo.school"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="招生院校课程代码（必填）" prop="courseCode">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="recruitCourseInfo.courseCode"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="招生院校课程名称" prop="courseName">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="recruitCourseInfo.courseName"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="是否强制覆盖" prop="isCover">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-switch
              v-model="recruitCourseInfo.isCover"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="0"
              :inactive-value="1"
            >
            </el-switch>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('recruitCourseInfo')">取 消</el-button>
        <el-button type="primary" @click="submitForm('recruitCourseInfo')"
          >确 定</el-button
        >
        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>

    <el-dialog
      title="编辑招生院校课程"
      :visible.sync="editNoticeVisible"
      @close="cancel('editRecruitCourse')"
    >
      <el-form :model="editRecruitCourse" ref="editRecruitCourse">
        <div class="line">
          <el-form-item label="编辑cso课程代码" prop="csoCode">
            <el-input
              type="textarea"
              autosize
              v-model="editRecruitCourse.csoCode"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="编辑cso课程名称:" prop="csoName">
            <el-input
              type="textarea"
              autosize
              v-model="editRecruitCourse.csoName"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="招生院校名称：" prop="school">
            <!-- <el-input
              type="textarea"
              autosize
              v-model="editRecruitCourse.school" 
            >
            </el-input>        -->
            {{ editRecruitCourse.school }}
          </el-form-item>

          <el-form-item label="招生院校课程代码:" prop="courseCode">
            <!-- <el-input
              type="textarea"
              autosize
              v-model="editRecruitCourse.courseCode" 
            >
            </el-input>        -->
            {{ editRecruitCourse.courseCode }}
          </el-form-item>

          <el-form-item label="编辑招生院校课程名称" prop="courseName">
            <el-input
              type="textarea"
              autosize
              v-model="editRecruitCourse.courseName"
            >
            </el-input>
            <!-- {{editRecruitCourse.courseName}} -->
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('editRecruitCourse')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editRecruitCourse')"
          >确 定</el-button
        >

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

    <!-- 编辑里的添加 -->
    <el-dialog
      title="补充招生院校课程"
      :visible.sync="addUpdateVisible"
      @close="cancel('addUpdateCourse')"
    >
      <el-form :model="addUpdateCourse" ref="addUpdateCourse">
        <div class="line">
          <el-form-item label="cso课程代码" prop="csoCode">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="addUpdateCourse.csoCode"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="cso课程名称" prop="csoName">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="addUpdateCourse.csoName"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="招生院校" prop="school">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input type="textarea" autosize v-model="addUpdateCourse.school">
            </el-input>
          </el-form-item>

          <el-form-item label="招生院校课程代码" prop="courseCode">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="addUpdateCourse.courseCode"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="招生院校课程名称" prop="courseName">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="addUpdateCourse.courseName"
            >
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('addUpdateCourse')">取 消</el-button>
        <el-button type="primary" @click="submitAddForm('addUpdateCourse')"
          >确 定</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="cso课程代码" align="center" prop="csoCode">
          <template slot-scope="scope">{{ scope.row.csoCode }}</template>
        </el-table-column>
        <el-table-column label="cso课程名称" align="center">
          <template slot-scope="scope">{{ scope.row.csoName }}</template>
        </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.courseCode }}</template>
        </el-table-column>
        <el-table-column label="招生院校课程名称" align="center">
          <template slot-scope="scope">{{ scope.row.courseName }}</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">
            <div v-if="scope.row.csoCode">
              <el-button
                @click="
                  handleUpdateSchoolAll(
                    scope.row.id,
                    scope.row.school,
                    scope.row.courseName,
                    scope.row.courseCode,
                    scope.row.csoName,
                    scope.row.csoCode
                  )
                "
                size="mini"
                >编辑</el-button
              >
              <el-button
                v-if="scope.row.id"
                @click="handleDel(scope.row.id)"
                size="mini"
                >删除</el-button
              >
            </div>
            <div v-else>
              <el-button
                title="添加招生院校课程"
                @click="
                  addUpdateSchoolAll(
                    scope.row.id,
                    scope.row.school,
                    scope.row.courseName,
                    scope.row.courseCode,
                    scope.row.csoName,
                    scope.row.csoCode
                  )
                "
                size="mini"
                style="width:120px"
                >添加</el-button
              >
            </div>
          </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 {
  // cooperateSchoolDel,
  // cooperateSchoolUpdate,
  // cooperateSchoolAdd,
  // cooperateSchoolAll,
  recruitSchoolAll,
  recruitSchoolAdd,
  recruitSchoolUpdate,
  recruitSchoolDel
} from "@/api/cso2";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "recruitCourse",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      pageSize: null,
      pageNum: null,
      listSelected: null,
      total: null,
      isMatch: false,
      listLoading: true,
      multipleSelection: [],
      formLabelWidth: "120px",
      options: [],
      teacherId: "",
      noticeVisible: false,
      addUpdateVisible: false,
      editNoticeVisible: false,
      title: "",
      // coopCourseInfo:{},
      recruitCourseInfo: {
        school: "",
        courseName: "",
        courseCode: "",
        csoName: "",
        csoCode: ""
      },
      //   editCoopCourse:{
      //   school: " ",
      //   courseName: " ",
      //   courseCode: " ",
      //   csoName: " ",
      //   csoCode: " ",

      // },
      editRecruitCourse: {
        id: 0,
        // school: " ",
        courseName: " ",
        // courseCode: " ",
        csoName: " ",
        csoCode: " "
      },
      addUpdateCourse: {
        id: 0,
        school: " ",
        courseName: " ",
        courseCode: " ",
        csoName: " ",
        csoCode: " "
      },

      updateId: 0
    };
  },
  methods: {
    getData() {
      if (this.isMatch) {
        this.listQuery.isMatch = 1;
      }
      recruitSchoolAll(this.listQuery).then(res => {
        // console.log(res, "getRecruitData");
        this.listLoading = false;

        this.list = res.data.list;
        this.pageNum = res.data.pageNum;
        this.pageSize = res.data.pageSize;
        this.total = res.data.total;
        // this.$store.commit("set_recruitSchoolAll", res.data.total);
        // this.list.auditSchool = res.data.list.school
        // this.total.auditSchool = total.data.list.school
        // console.log(this.list, "this.list,codeAll");

        this.$forceUpdate();
      });
      recruitSchoolAll({
        pageNum: 1,
        pageSize: 10,
        isMatch: 1
      }).then(res => {
        this.$store.commit("set_recruitSchoolAll", res.data.total);
        // console.log(res.data.total, "vuex");
      });
    },
    handleSearchList() {
      //搜索
      this.listQuery.pageNum = 1;
      this.getData();
    },
    handleResetSearch(formName) {
      this.$refs[formName].resetFields();
      // 重置
      // this.selectProductCateValue = [];
      this.isMatch = false;

      this.listQuery = Object.assign({}, defaultListQuery);
      this.getData();
    },
    // getSelectedForm() {
    //   this.listQuery.pageNum = 1;
    //   this.getData();
    // },

    // resetForm() {
    //   this.isMatch = false;
    //   this.listQuery = Object.assign({}, defaultListQuery);
    //   this.getData();
    // },

    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.recruitCourseInfo.id = this.updateId;
      // }
      this.$confirm("请认真核对cso课程名称和cso课程代码！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        recruitSchoolAdd(this.recruitCourseInfo).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.noticeVisible = false;

          this.getData();
          this.$forceUpdate();
        });
      });
    },
    //编辑添加
    submitAddForm(formName) {
      // if (this.updateId) {
      //   this.addUpdateCourse.id = this.updateId;
      // }
      this.$confirm("请认真核对cso课程名称和cso课程代码！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        recruitSchoolAdd(this.addUpdateCourse).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.addUpdateVisible = false;

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

      this.$confirm("请认真核对cso课程名称和cso课程代码！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        //拿到整个公告
        recruitSchoolUpdate(this.editRecruitCourse).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();
        });
      });
    },
    //添加编辑
    addUpdateSchoolAll(id, school, courseName, courseCode, csoName, csoCode) {
      this.addUpdateVisible = true;
      // this.title = "编辑公告";
      this.addUpdateCourse.id = id;
      this.addUpdateCourse.school = school;
      this.addUpdateCourse.courseName = courseName;
      this.addUpdateCourse.courseCode = courseCode;
      this.addUpdateCourse.csoName = csoName;
      this.addUpdateCourse.csoCode = csoCode;
    },
    //编辑
    handleUpdateSchoolAll(
      id,
      school,
      courseName,
      courseCode,
      csoName,
      csoCode
    ) {
      this.editNoticeVisible = true;
      // this.title = "编辑公告";
      this.editRecruitCourse.id = id;
      this.editRecruitCourse.school = school;
      this.editRecruitCourse.courseName = courseName;
      this.editRecruitCourse.courseCode = courseCode;
      this.editRecruitCourse.csoName = csoName;
      this.editRecruitCourse.csoCode = csoCode;
      // console.log(this.updateId, noticeContent);
    },
    cancel(formName) {
      this.noticeVisible = false;
      this.editNoticeVisible = false;
      this.addUpdateVisible = false;
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.getData();
  }
};
</script>

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