<template>
  <ai-detail class="AppCertificateManage">
    <template slot="title">
      <ai-title :title="params.id ? '编辑' + '证书' : '添加' + '证书'" isShowBack isShowBottomBorder @onBackClick="cancel(false)"></ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <el-form class="ai-form" :model="form" label-width="120px" ref="form">
            <el-form-item label="证书名称" style="width: 100%" prop="certificateName" :rules="[{required: true, message: '请输入证书名称', trigger: 'blur'}]">
              <el-input size="small" v-model="form.certificateName" clearable placeholder="请输入证书名称" :maxlength="200"></el-input>
            </el-form-item>
            <el-form-item prop="certificateType" label="类型" :rules="[{required: true, message: '请选择证书类型', trigger: 'change'}]">
              <ai-select
                v-model="form.certificateType"
                clearable
                placeholder="请选择证书类型"
                :selectList="dict.getDict('qjCertificateType')">
              </ai-select>
            </el-form-item>
            <el-form-item label="累计学习时长" v-if="form.certificateType === '0'" style="width: 100%" prop="studyDuration" :rules="[{required: true, message: '请输入累计学习时长', trigger: 'blur'}]">
              <el-input-number size="small" v-model="form.studyDuration" :min="1" clearable placeholder="请输入" :maxlength="200"></el-input-number>
              <span style="color: #999; padding-left: 10px;">分钟</span>
            </el-form-item>
            <el-form-item label="通过考试" v-else style="width: 100%" prop="passExam" :rules="[{required: true, message: '请输入通过考试场数', trigger: 'blur'}]">
              <el-input-number size="small" v-model="form.passExam" :min="1" clearable placeholder="请输入" :maxlength="200"></el-input-number>
              <span style="color: #999; padding-left: 10px;">场</span>
            </el-form-item>
            <el-form-item label="上传证书" prop="imageUrl" style="width: 100%;" :rules="[{required: true, message: '请上传证书', trigger: 'change'}]">
              <ai-uploader
                :instance="instance"
                v-model="form.imageUrl"
                :limit="1">
              </ai-uploader>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },
    data () {
      return {
        info: {},
        form: {
          certificateName: '',
          content: '',
          imageUrl: [],
          certificateType: ''
        },
        id: ''
      }
    },

    created () {
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appcertificateinfo/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = {
              ...res.data,
              imageUrl: [{
                url: res.data.imageUrl
              }]
            }
          }
        })
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appcertificateinfo/addOrUpdate`, {
              ...this.form,
              imageUrl: this.form.imageUrl[0].url
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功')
                setTimeout(() => {
                  this.cancel(true)
                }, 600)
              }
            })
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'List',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
  .AppCertificateManage {
  }
</style>
