<!--证书管理-->
<template>
  <div class="app-container certificate">
    <div class="page-container">
      <div class="page-form-title-box">
        <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="请输入证书名称"

              size="small"
            />
          </el-form-item>
          <p class="page-submit-common-style-button"
             style="float:right"
             @click="handleAdd"
             v-hasPermiRouter="['exam:certificate:add']"
          >新增证书</p>
          <el-form-item>
            <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
            <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table v-loading="loading" border class="table-box" :data="certificateList" @selection-change="handleSelectionChange">
          <el-table-column label="证书名称" align="left" prop="certTitle"/>
          <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="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="handleUpdate(scope.row)"
                   v-hasPermiRouter="['exam:certificate:edit']"
                >编辑</p>
                <p class="table-button-p"
                   @click="handleDelete(scope.row)"
                   v-hasPermiRouter="['exam:certificate:del']"
                >删除</p>
                <p class="table-button-null"></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.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <!-- 添加或修改证书管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body class="cartifcate-dialog" :close-on-click-modal="false">
      <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="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/jpg,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="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 dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="cancel">取消</p>
        <p class="dialog-footer-button-primary" @click="submitForm">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { examCertificateList, examCertificateInfo,
  examCertificateDel, examCertificateAdd,
  examCertificateEdit } from "@/api/safetyEducationExam/index";
import { getToken } from "@/utils/auth";
export default {
  name: "Certificate",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['laboratory:safe_book:edit','exam:certificate:edit','exam:certificate:del']),
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      headers: {
        Authorization:getToken(),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证书管理表格数据
      certificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      subClassOptions: "",
      // 查询参数
      queryParams: {
        page: 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.form.certUrl = res.data.url;
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      let type = false;
      if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/jpg' || file.type == 'image/gif') {
        type = true;
      }else{
        this.msgError('只能上传png/jpeg/jpg/gif格式图片');
        type = false;
      }
      return type;
    },
    /** 查询证书管理列表 */
    getList() {
      this.loading = true;
      examCertificateList(this.queryParams).then(response => {
        this.certificateList = response.data.records;
        this.total = response.data.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.page = 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
      examCertificateInfo(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) {
            examCertificateEdit(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            examCertificateAdd(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 examCertificateDel({id:ids});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exam/certificate/export', {
        ...this.queryParams
      }, `exam_certificate.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
  .certificate {
    .button-box{
      width:190px;
      display: flex;
    }
    .form-box{
    }


  }
</style>
<style lang="scss">
  .cartifcate-dialog{
    .el-input-number__increase, .el-input-number__decrease{
      height:38px;
    }
    .certificate-avatar-uploader{
      i{
        width:80px;
        height:80px;
        line-height:80px;
        text-align: center;
        border:1px solid #e0e0e0;
        border-radius:4px;
      }
      img{
        width:240px;
        height:240px;
      }
    }
  }
</style>
