<template>
  <div id="commoditySetTeacher">
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      @addClick="addClick"
      :navText="navText"
      :loading="loading"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="edit(props.scope.row)">编辑</el-button>
        <el-button type="text" @click="del(props.scope.row)">删除</el-button>
      </template>
    </table-list>
    <!-- <pagination
      :total="total"
      :pageSize="formData.pageSize"
      :currentPage="formData.pageNum"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    /> -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="1200px"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">关联课程</div>
        <div class="rightBoxs">
          <img src="@/assets/images/Close@2x.png" alt="" @click="closeBZ" />
        </div>
      </div>
      <div>
        <el-form
          label-position="right"
          label-width="150px"
          :model="resultData"
          ref="resultData"
        >
          <el-form-item label="模板名称" prop="name">
            <el-input
              v-model="resultData.name"
              placeholder="请输入别名"
            ></el-input>
          </el-form-item>
          <el-form-item label="关联课程" prop="majors">
            <el-button type="primary" size="small" @click="link"
              >关联课程</el-button
            >
            <el-table
              v-if="resultData.courseList && resultData.courseList.length > 0"
              border
              :data="resultData.courseList"
              style="width: 100%"
              max-height="250"
              :header-cell-style="{ 'text-align': 'center' }"
            >
              <el-table-column prop="code" label="编码" width="200">
              </el-table-column>
              <el-table-column prop="courseName" label="名称" width="300">
              </el-table-column>
              <el-table-column prop="subjectName" label="科目" width="200">
              </el-table-column>
              <el-table-column prop="aliasName" label="别名(必填)" width="200">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row['aliasName']"
                    :maxlength="10"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="108">
                <template slot-scope="scope">
                  <el-button
                    @click="delCourse(scope.row.courseId)"
                    type="text"
                    size="small"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeBZ">取 消</el-button>
        <el-button @click="submits" v-throttle>确 定</el-button>
      </span>
    </el-dialog>
    <select-course ref="selectCourse" @backData="backData" />
  </div>
</template>

<script>
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
import selectCourse from "./selectCourse.vue";
export default {
  name: "CommoditySetTeacher",
  components: { tableList, pagination, selectCourse },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      goodsId: "", //商品ID
      dialogVisible: false,
      bieName: "",
      navText: {
        title: "双师资模板设置",
        num: false,
        border: true,
        choice: false,
        addHide: false,
        changeWidth: "180px",
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      resultData: {
        name: "",
        courseList: [],
      },
      topType: true,
      formData: {
        pageSize: 10,
        pageNum: 1,
      },
      // 表单
      tableSet: [
        {
          label: "模板名称",
          prop: "name",
          hidden: true,
        },
        {
          label: "关联课程",
          prop: "courseList",
          scope: "courseAbout",
          hidden: true,
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
    };
  },
  created() {
    this.goodsId = this.$route.query.id;
  },
  mounted() {
    this.search();
  },
  methods: {
    edit(v) {
      this.resultData.id = v.id;
      this.resultData.name = v.name;
      this.resultData.courseList = v.courseList;
      console.log(this.resultData);
      this.dialogVisible = true;
    },
    addClick() {
      this.resultData.id = "";
      this.resultData.name = "";
      this.resultData.courseList = [];
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["listData"];
      });
    },
    closeBZ() {
      this.dialogVisible = false;
      this.search();
    },
    link() {
      //打开关联课程组件
      this.$refs.selectCourse.openBox(this.goodsId, this.resultData.courseList);
    },
    //弹窗回调
    backData(array) {
      let size = this.resultData.courseList.length + array.length;
      if (size > 3) {
        this.$message.warning("最多关联3条数据,请重新选择！");
      } else {
        if (array && array.length > 0) {
          array.forEach((item) => {
            let data = {
              courseId: item.courseId,
              code: item.code,
              courseName: item.courseName,
              subjectId: item.subjectId,
              subjectName: item.subjectName,
              aliasName: "",
            };
            this.resultData.courseList.push(data);
          });
        }
      }
    },
    search() {
      let param = {
        goodsId: this.goodsId,
      };
      this.$api.inquireCourseTeacherList(param).then((res) => {
        console.log(res, "res");
        if (res.code == 200) {
          this.tableData = res.data;
        }
      });
    },
    init() {
      this.search();
    },
    delCourse(courseId) {
      this.resultData.courseList = this.resultData.courseList.filter(
        (x) => x.courseId !== courseId
      );
    },
    del(v) {
      console.log(v, "v");
      //删除
      let param = {
        id: v.id,
        status: -1,
      };
      this.$api.editCourseTeacherList(param).then((res) => {
        this.$message.success("删除成功");
        this.dialogVisible = false;
        this.search();
      });
    },
    handleSizeChange(v) {
      this.formData.pageSize = v;
      this.formData.pageNum = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.formData.pageNum = v;
      this.search();
    },
    submits() {
      if (
        this.resultData.courseList &&
        this.resultData.courseList.length > 1 &&
        this.resultData.courseList.length < 4
      ) {
        let data = this.resultData.courseList.filter((x) => x.aliasName == "");
        if (data && data.length > 0) {
          this.$message.warning("请先填写别名");
        } else {
          let subjectId = this.resultData.courseList[0].subjectId;
          let flag = this.resultData.courseList.every((items) => {
            return items.subjectId === subjectId;
          });
          if (flag) {
            if (this.resultData.id && this.resultData.id != "") {
              //修改
              let param = {
                id: this.resultData.id,
                goodsId: this.goodsId,
                name: this.resultData.name,
                courseList: this.resultData.courseList,
              };
              this.$api.editCourseTeacherList(param).then((res) => {
                this.$message.success("修改成功");
                this.dialogVisible = false;
                this.search();
              });
            } else {
              //新增
              let param = {
                goodsId: this.goodsId,
                name: this.resultData.name,
                courseList: this.resultData.courseList,
              };
              this.$api.addCourseTeacherList(param).then((res) => {
                this.$message.success("添加成功");
                this.dialogVisible = false;
                this.search();
              });
            }
          } else {
            this.$message.warning(
              "只能选择相同科目的课程进行关联,请重新选择！"
            );
          }
        }
      } else {
        this.$message.warning("最多关联3条/最少关联2条数据,请重新选择！");
      }
      console.log(this.resultData, "resultData");
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
</style>

