<template>
  <div class="app-container">
    <el-card>
      <div style="margin-top: 6px">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-if="checkPermission(['course'])"
          >新增课程</el-button>
      </div>
    </el-card>
    <el-card style="margin-top: 10px">
    <el-table
      v-loading="listLoading"
      :data="tableList.results"
      style="width: 100%;"
      border
      fit
      stripe
      highlight-current-row
      max-height="700"
      ref="filterTable"
    >
     <el-table-column
      type="selection"
      width="55">
    </el-table-column>
      <el-table-column type="index" width="50" />
      <el-table-column label="课程名称" prop="name">
      </el-table-column>
      <el-table-column label="创建日期">
        <template slot-scope="scope">{{scope.row.create_time.substring(0, 10)}}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
          <template slot-scope="scope">
              <el-button type="primary" size="small" @click="handleEdit(scope.row)" icon="el-icon-edit"
                  :disabled="!checkPermission(['course'])"></el-button>
              <el-button type="danger" size="small" @click="handleDelete(scope)" icon="el-icon-delete"
                  :disabled="!checkPermission(['course'])"></el-button>
          </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableList.count > 0"
      :total="tableList.count"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.page_size"
      @pagination="getList"
    />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑课程' : '新增课程'"
    >
      <el-form
        ref="Form"
        :model="form"
        label-width="100px"
        label-position="right"
        :rules="rule"
      >
      <el-form-item label="课程名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
   
  </div>
</template>
<style >
.el-table-filter {
  width: 400px;
  max-height: 300px;
  overflow-y: auto;
}
</style>
<script>
import {getCourseList,createCourse,updateCourse,deleteCourse} from "@/api/edu";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

export default {
  components: { Pagination },
  data() {
    return {
      tableList: { count: 0 ,list:[] },
      listLoading: true,
      listQuery: {
        page: 1,
        page_size: 20,
      },
      form: {name:''},
      dialogShow:false,
      dialogVisible: false,
      dialogType: "new",
      popovervisible: false,
      rule: {
        name: [{ required: true, message: "请填写课程名称", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getCourseList(this.listQuery).then((response) => {
        if (response.data) {
          this.tableList = response.data;
        }
        this.listLoading = false;
      });
    },
   
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleAdd() {
      this.form = { name: "" };
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleEdit(row) {
      this.form = Object.assign({}, row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteCourse(scope.row.id);
          this.getList();
          this.$message({
            type: "success",
            message: "成功删除!",
          });
        })
        .catch((err) => {
        });
    },
    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updateCourse(this.form.id, this.form).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$notify({
                  title: "成功",
                  message: "编辑成功",
                  type: "success",
                  duration: 2000,
                });
              }
            });
          } else {
            createCourse(this.form).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$notify({
                  title: "成功",
                  message: "新增成功",
                  type: "success",
                  duration: 2000,
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    handleShow(row){
      let timer = new Date().getTime();
      if(row.证书地址!==null){
        this.imgurl = 'https://testsearch.ctc.ac.cn'+row.证书地址+'?'+timer;
        this.srcList.push(this.imgurl);
      }else{
        getCertificate(row.id).then(res=>{
          this.imgurl='https://testsearch.ctc.ac.cn'+res.data.证书地址+'?'+timer;
          this.srcList.push(this.imgurl);
        })
      }
      this.dialogShow = true;
    },
  },
};
</script>
