<!--证书管理-->
<template>
  <div class="app-container certificate">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="form-box">

      <el-form-item label="证书名称" prop="certTitle">
        <el-input
          v-model="queryParams.certTitle"
          placeholder="请输入证书名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item style="float: right;">
        <el-col :span="1.5" style="margin-right:20px;">
          <p class="add-button-one-120"
             @click="handleAdd"
             v-hasPermi="['exam:certificate:add']"
          ><i class="el-icon-plus"></i>新增证书</p>
        </el-col>
      </el-form-item>
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" border :data="certificateList" @selection-change="handleSelectionChange">
      <el-table-column label="证书名称" align="left" prop="certTitle"/>
      <!--<el-table-column label="学科类别" align="left" prop="certType" >-->
        <!--<template slot-scope="scope">-->
<!--          <p v-for="(item,index) in subClassOptions" :key="index" v-if="scope.row.certType==item.dictValue">{{item.dictLabel}}</p >-->
<!--        </template>-->
      <!--</el-table-column>-->
      <el-table-column label="证书有效期(月)" align="left" prop="certValidity" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
               style="margin-right:10px;"
               @click="handleUpdate(scope.row)"
               v-hasPermiAnd="['laboratory:safe_book:edit','exam:certificate:edit']"
            >编辑</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['exam:certificate:remove']"
            >删除</p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination :page-sizes="[20, 30, 40, 50]"
      v-show="total>0"
      :total="total"
      layout="total, prev, pager, next, sizes, jumper"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改证书管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body class="cartifcate-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="证书名称" prop="certTitle">
          <el-input v-model="form.certTitle" placeholder="请输入证书名称" maxlength="20"/>
        </el-form-item>
        <!--<el-form-item label="封面" prop="certUrl">-->
          <!--<el-upload-->
            <!--class="avatar-uploader"-->
            <!--action="https://jsonplaceholder.typicode.com/posts/"-->
            <!--:show-file-list="false"-->
            <!--:on-success="handleAvatarSuccess"-->
            <!--:before-upload="beforeAvatarUpload">-->
            <!--<img v-if="imageUrl" :src="imageUrl" class="avatar">-->
            <!--<i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
          <!--</el-upload>-->
          <!--&lt;!&ndash;<el-input v-model="form.certUrl" placeholder="请输入证书上传的图片" />&ndash;&gt;-->
        <!--</el-form-item>-->
        <el-form-item label="封面" prop="certUrl">
          <el-upload
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/gif,image/png"
            :on-success="handleAvatarSuccess"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.certUrl" :src="form.certUrl" class="avatar">
            <i v-if="!form.certUrl" class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!--<el-form-item label="学科类别" prop="certType">-->
          <!--<el-select v-model="form.certType" placeholder="请选择学科类别">-->
            <!--<el-option-->
              <!--v-for="dict in subClassOptions"-->
              <!--:key="dict.dictValue"-->
              <!--:label="dict.dictLabel"-->
              <!--:value="dict.dictValue">-->
            <!--</el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="有效期" prop="certValidity">
          <!-- @change="handleChange"-->
          <el-input-number v-model="form.certValidity" :min="1" :max="24" label="输入有效期"></el-input-number><span style="margin-left:20px;font-size:16px;">(月)</span>
        </el-form-item>
        <!--
        有效期为 一个月/三个月/半年/一年
        -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCertificate, getCertificate, delCertificate, addCertificate, updateCertificate, listByCertType,updateTrain } from "@/api/exam/certificate";
import { getToken } from "@/utils/auth";
export default {
  name: "Certificate",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['laboratory:safe_book:edit','exam:certificate:edit','exam:certificate:remove']),
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证书管理表格数据
      certificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      subClassOptions: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        certTitle: null,
        certType: null,
        certUrl: null,
        certValidity: null,
        certValidityUnit: null,
        deptId: null,
        deptName: null,
        userId: null,
      },
      // 表单参数
      form: {
        certValidity: 1,
        certType: 1
      },
      // 表单校验
      rules: {
        certTitle: [
          { required: true, message: "请输入证书名称", trigger: "blur" },
          { required: true, message: "请输入播放文字", validator: this.spaceJudgment, trigger: "blur" }
        ],
        certType: [
          { required: true, message: "请选择学科类别", trigger: "blur" }
        ],
        certUrl: [
          { required: true, message: "请上传封面照片", trigger: "blur" }
        ],
        certValidity: [
          { required: true, message: "输入有效期", trigger: "blur" },
          { required: true, message: "输入有效期", validator: this.spaceJudgment, trigger: "blur" }
        ]
      },
      imageUrl:"",
    };
  },
  created() {
    this.getList(),
    this.getDicts("subject_class").then(response => {
      this.subClassOptions = response.data;
    })
  },
  methods: {
    //上传
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      console.log(res.data.url);
      this.form.certUrl = res.data.url;
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      let type = false;
      console.log('file',file);
      if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/gif') {
        type = true;
      }else{
        this.$message.error('只能上传png/jpeg/gif格式图片');
        type = false;
      }
      return type;
    },
    /** 查询证书管理列表 */
    getList() {
      this.loading = true;
      listCertificate(this.queryParams).then(response => {
        this.certificateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        certTitle: null,
        certType: null,
        certUrl: null,
        certValidity: null,
        certValidityUnit: null,
        deptId: null,
        deptName: null,
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.imageUrl = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加证书管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCertificate(id).then(response => {
        this.form = response.data;
        this.form.certType = ""+response.data.certType;
        this.open = true;
        this.title = "修改证书管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCertificate(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCertificate(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除"' + row.certTitle + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCertificate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exam/certificate/export', {
        ...this.queryParams
      }, `exam_certificate.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .certificate {
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px!important;
    .button-box{
      width:190px;
      display: flex;
    }
    .form-box{
    }
  }
</style>
<style lang="scss">
  .cartifcate-dialog{
    .el-input-number__increase, .el-input-number__decrease{
      height:38px;
    }
  }
</style>
