<template>
  <el-container>
    <el-header v-if="isSearch">
      <el-form style="margin: 10px 0" :inline="true">
        <el-form-item label="证书名称：">
          <el-input type="text" v-model="search.certificateName" />
        </el-form-item>
        <el-form-item>
          <el-button
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-search"
            @click="initList"
          >
            搜索
          </el-button>
          <el-button class="commonBtn" icon="el-icon-refresh" @click="reset"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </el-header>

    <el-main>
      <div class="headerTop">
        <div>
          <el-button type="primary" class="el-icon-plus" @click="doAdd(1,'')">
            新增</el-button
          >
        </div>
        <div>
          <right-toolbar  @queryTable="initList" :search="true" @update:showSearch="isSearch = !isSearch"></right-toolbar>
        </div>
      </div>

      <el-table
        :data="list"
        height="calc(100% - 200px)"
        :row-style="{ height: '0px' }"
        :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
        :cell-style="{ 'text-align': 'center', padding: '2px' }"
        style="width: 100%"
      >
        <el-table-column width="80" type="index" label="序号" />
        <el-table-column
          prop="certificateName"
          show-overflow-tooltip
          label="证书名称"
          align="center"
        />
        <el-table-column
          prop="createDate"
          show-overflow-tooltip
          label="创建时间"
          align="center"
        />
        <el-table-column
          prop="userName"
          show-overflow-tooltip
          label="创建人"
          align="center"
        />

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="doAdd(2, scope.row)">
              编辑
            </el-button>
            <el-button type="text" @click="doAdd(3,scope.row)">
              查看
            </el-button>
            <el-button
              type="text"
               
              @click="doDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :page.sync="search.pageNo"
        :limit.sync="search.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        @pagination="initList"
      />
    </el-main>

    <!-- 新增/编辑 的dialog -->
    <el-dialog
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      width="600px"
      :title="isEdit"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleFormRef"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="证书名称：" prop="certificateName">
          <el-input
            v-model="ruleForm.certificateName"
            maxlength="100"
            :disabled="disabledVal"
          ></el-input>
        </el-form-item>
        <el-form-item label="培训计划分类：" prop="trainingPlanClassificationId">
          <el-select
            :disabled="disabledVal"
            v-model="ruleForm.trainingPlanClassificationId"
            style="width: 100%"
          >
            <el-option
              v-for="item in optionList"
              :key="item.id"
              :label="item.categoriesName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer" v-if="!disabledVal">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="submit">提 交</el-button>
      </span>
    </el-dialog>

    <!-- 查看 的dialog -->
    <el-dialog :visible.sync="LookdialogVisible" title="查看" width="700px">
      <el-row>
        <el-col :span="12">类目名称： {{ watchObj.categoriesName }} </el-col>
        <el-col :span="12">排序： {{ watchObj.classificationSort }} </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">创建时间： {{ watchObj.createDate }} </el-col>
        <el-col :span="12">创建人： {{ watchObj.userName }} </el-col>
      </el-row>
    </el-dialog>
  </el-container>
</template>

<script>
import {
  queryLastLevelClassification,
  getCertificateList,
  delCertificateById,
  saveCertificate,
} from '@/api/planClass/planClass.js'
export default {
  name: 'RuoyiPlanClass',

  data() {
    return {
      isSearch: false, //控制搜索框是否显示
      search: {
        certificateName: '',
        pageNo: 1,
        pageSize: 10,
      },
      total:0,

      list: [],
      optionList:[],
      watchObj: {}, // 查看的接收
      dialogVisible: false, //新增/修改的dialog
      LookdialogVisible: false, //查看的dialog
      isEdit: '',
      disabledVal: false, //查看禁用

      ruleForm: {
        certificateName: '',
        trainingPlanClassificationId: '',
      },
      rules: {
        certificateName: [
          { required: true, message: '请输入证书名称', trigger: 'blur' }
        ],
        trainingPlanClassificationId: [
          { required: true, message: '请选择培训计划分类', trigger: 'change' }
        ]
      },
      loading: false // loading 效果
    }
  },

  mounted() {
    this.initList() // 列表初始化
    this.initQueryLastLevelClassification() // 列表初始化
  },

  methods: {
    // 列表初始化
    initList() {
      try {
        this.loading = true
        getCertificateList(this.search).then((res) => {
          if (res.code === 200) {
            this.list = res.data.list
            this.total = res.data.total
          } else {
            this.$message.error(res.msg)
          }
          this.loading = false
        })
      } catch (error) {
        this.loading = false
      }
    },
    async initQueryLastLevelClassification(){
      const {data:res} = await queryLastLevelClassification()
      this.optionList = res
    },
    reset(){
      this.search.certificateName = ''
      this.initList()
    },
    doAdd(type, val) {
      if (type === 1) {
        this.isEdit = '新增'
        this.disabledVal=false //查看禁用
        if(val) {
          this.ruleForm.parantId = val.id || "";
        }
      } else if(type === 2) {
        this.isEdit = '编辑'
        this.ruleForm = {...val}
        this.disabledVal=false //查看禁用
      }else {
        this.isEdit = '查看'
        this.ruleForm = {...val}
        this.disabledVal=true //查看禁用
      }
      this.dialogVisible = true
    },
    // 取消按钮
    cancle() {
      this.$nextTick(() => {
        this.ruleForm = {
          certificateName: '',
        trainingPlanClassificationId: '',
        }
        this.$refs.ruleFormRef.resetFields()
      })
      this.dialogVisible = false
    },
    handleClose() {
      this.$nextTick(() => {
        this.ruleForm = {
          certificateName: '',
        trainingPlanClassificationId: '',
        }
        this.$refs.ruleFormRef.resetFields()
      })
      this.dialogVisible = false
    },
    // 确定按钮
    submit() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (valid) {
          const res = await saveCertificate(this.ruleForm)
          if (res.code !== 200) return this.$message.error(res.msg)
          this.initList()
          this.$message.success(`${this.isEdit}成功!`)
          this.ruleForm = {
            categoriesName: '',
            classificationSort: '',
            parantId: ''
          }
          this.dialogVisible = false
        } else {
          this.$message.warning("必传项不能为空!")
          return false
        }
      })
    },
    // 查看
    doWatch(val) {
      this.watchObj = val
      this.LookdialogVisible = true
    },
    // 删除
    doDelete(val) {
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await delCertificateById({id:val.id})
          if (res.code !== 200) return this.$message.error(res.msg)
          this.initList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message.warning('已取消删除')
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}

::v-deep .el-loading-mask {
  background-color: rgba(0, 222, 240, 0.2) !important;
}
::v-deep .el-loading-spinner {
  top: 65% !important;
}
::v-deep .el-loading-spinner i {
  font-size: 0.1513rem !important;
}
</style>
