<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('coopCourseInfo')"
    >
      <el-form :model="coopCourseInfo" ref="coopCourseInfo">
        <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="coopCourseInfo.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="coopCourseInfo.csoName">
            </el-input>
          </el-form-item>

          <el-form-item label="合作院校（必填）" prop="auditSchool">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="coopCourseInfo.auditSchool"
            >
            </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="coopCourseInfo.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="coopCourseInfo.courseName"
            >
            </el-input>
          </el-form-item>

          <el-form-item label="是否强制覆盖" prop="isCover">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-switch
              v-model="coopCourseInfo.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('coopCourseInfo')">取 消</el-button>
        <el-button type="primary" @click="submitForm('coopCourseInfo')"
          >确 定</el-button
        >
        <!-- <el-button type="primary" @click="test">test</el-button> -->
      </div>
    </el-dialog>

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

          <el-form-item label="编辑cso课程名称" prop="csoName">
            <el-input type="textarea" autosize v-model="editCoopCourse.csoName">
            </el-input>
          </el-form-item>

          <el-form-item label="合作院校课程代码：" prop="courseCode">
            <!-- <el-input
              type="textarea"
              autosize
              v-model="editCoopCourse.courseCode"
            >
            </el-input> -->
            {{ editCoopCourse.courseCode }}
          </el-form-item>

          <el-form-item label="合作院校名称:" prop="auditSchool">
            <!-- <el-input type="textarea" autosize v-model="editCoopCourse.auditSchool">
            </el-input> -->
            {{ editCoopCourse.auditSchool }}
          </el-form-item>
          <el-form-item label="编辑合作院校课程名称" prop="courseName">
            <el-input
              type="textarea"
              autosize
              v-model="editCoopCourse.courseName"
            >
            </el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('editCoopCourse')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editCoopCourse')"
          >确 定</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="auditSchool">
            <!-- <el-input v-model="noticeInfo.note"> </el-input> -->
            <el-input
              type="textarea"
              autosize
              v-model="addUpdateCourse.auditSchool"
            >
            </el-input>
            <!-- {{addUpdateCourse.auditSchool}} -->
          </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
} from "@/api/cso2";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "cooperateCourse",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      listSelected: null,
      pageSize: null,
      pageNum: null,
      total: null,
      isMatch: false,
      addUpdateVisible: false,
      listLoading: true,
      multipleSelection: [],
      formLabelWidth: "120px",
      options: [],
      teacherId: "",
      noticeVisible: false,
      editNoticeVisible: false,
      title: "",
      coopCourseInfo: {
        auditSchool: "",
        courseName: "",
        courseCode: "",
        csoName: "",
        csoCode: ""
      },
      editCoopCourse: {
        id: 0,
        // auditSchool: " ",
        courseName: " ",
        // courseCode: " ",
        csoName: " ",
        csoCode: " "
      },
      addUpdateCourse: {
        id: 0,
        auditSchool: " ",
        courseName: " ",
        courseCode: " ",
        csoName: " ",
        csoCode: " "
      },

      updateId: ""
    };
  },
  methods: {
    getData() {
      if (this.isMatch) {
        this.listQuery.isMatch = 1;
      }
      this.listLoading = true;
      cooperateSchoolAll(this.listQuery).then(res => {
        // console.log(res, "getCoopData");
        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_cooperateSchoolAll", 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();
      });
      cooperateSchoolAll({
        pageNum: 1,
        pageSize: 10,
        isMatch: 1
      }).then(res => {
        this.$store.commit("set_cooperateSchoolAll", res.data.total);
        // console.log(res.data.total, "vuex");
      });
    },
    // getSelectedForm() {
    //   this.listQuery.pageNum = 1;
    //   this.getData();
    // },
    // resetForm() {
    //   this.isMatch = false;
    //   this.listQuery = Object.assign({}, defaultListQuery);
    //   this.getData();
    // },

    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();
    },

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

    //添加
    submitForm(formName) {
      // if (this.updateId) {
      //  this.coopCourseInfo.auditSchool = this.school;
      // }
      this.$confirm("请认真核对cso课程名称和cso课程代码！", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        cooperateSchoolAdd(this.coopCourseInfo).then(response => {
          this.$message({
            type: "success",
            message: "提交成功",
            duration: 1000
          });
          this.$refs[formName].resetFields();
          this.noticeVisible = false;

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

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

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

          this.getData();
          this.$forceUpdate();
        });
      });
    },

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

          this.getData();
          this.$forceUpdate();
        });
      });
    },
    //handleUpdateNoticeAll(id, schoolName,schoolBrief) {
    handleUpdateSchoolAll(
      id,
      school,
      courseName,
      courseCode,
      csoName,
      csoCode
    ) {
      this.editNoticeVisible = true;
      // this.title = "编辑公告";
      this.editCoopCourse.id = id;
      this.editCoopCourse.auditSchool = school;
      this.editCoopCourse.courseName = courseName;
      this.editCoopCourse.courseCode = courseCode;
      this.editCoopCourse.csoName = csoName;
      this.editCoopCourse.csoCode = csoCode;
      // console.log(this.updateId, noticeContent);
    },
    //编辑里的添加
    addUpdateSchoolAll(id, school, courseName, courseCode, csoName, csoCode) {
      this.addUpdateVisible = true;
      this.addUpdateCourse.id = id;
      this.addUpdateCourse.auditSchool = school;
      this.addUpdateCourse.courseName = courseName;
      this.addUpdateCourse.courseCode = courseCode;
      this.addUpdateCourse.csoName = csoName;
      this.addUpdateCourse.csoCode = csoCode;
    },

    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>
