<template>
  <div class="doctor-edit">
    <el-form ref="formRef" class="doctor-edit-form" label-width="140px" :model="auditForm" :rules="formRules">
      <el-form-item label="培训师姓名:">
        <span>{{ doctorInfo.doctorName }}</span>
      </el-form-item>
      <el-form-item label="从业资格证编号:">
        <span>{{ doctorInfo.certificateNo }}</span>
      </el-form-item>
      <el-form-item label="所在机构:" prop="clubName">
        <span>{{ doctorInfo.clubName }}</span>
      </el-form-item>
      <el-form-item label="形象照:" prop="doctorImage">
        <preview-image width="140px" height="140px" class="preview-image" :src="doctorInfo.doctorImage" />
      </el-form-item>
      <el-form-item label="简介图:" prop="banner">
        <template v-for="(image, index) in doctorInfo.bannerList">
          <preview-image
            :key="index"
            width="140px"
            height="140px"
            class="preview-image"
            :src="image"
            :preview-src-list="doctorInfo.bannerList"
          />
        </template>
      </el-form-item>

      <el-form-item label="操作培训师标签">
        <span v-text="doctorInfo.tagList.join()" />
      </el-form-item>

      <el-form-item label="自定义属性:" prop="paramList">
        <div class="device-section">
          <div v-for="(param, index) in doctorInfo.paramList" :key="index" class="section">
            <el-form label-width="90px">
              <el-form-item label="字段名称:">
                <span>{{ param.name }}</span>
              </el-form-item>
              <el-form-item label="字段内容:">
                <span>{{ param.content }}</span>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-form-item>

      <!-- <el-form-item label="审核状态:">
        <audit-status :status="doctorInfo.auditStatus" :reason="doctorInfo.invalidReason" />
      </el-form-item> -->

      <!-- 审核表单 -->
      <template v-if="type === 'audit'">
        <el-form-item label="审核状态：">
          <el-radio-group v-model="auditForm.auditStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="0">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="auditForm.auditStatus !== 1" prop="invalidReason" label="原因：">
          <el-input v-model="auditForm.invalidReason" type="textarea" placeholder="请说明原因" />
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="navigateBack">返回</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </el-form-item>
      </template>
    </el-form>
  </div>
</template>

<script>
import { doctorFormData, auditDoctor, auditDoctorCheck } from '@/api/doctor'
export default {
  data() {
    return {
      type: 'review',
      isRequest: true,
      doctorId: '',
      point: {},
      disabled: false,
      doctorInfo: {
        doctorName: '',
        certificateNo: '',
        clubName: '',
        bannerList: '',
        doctorImage: '',
        tagList: [],
        paramList: [],
        auditStatus: '',
        invalidReason: ''
      },
      auditForm: {
        doctorId: '', // 商品id
        auditStatus: 1, // 商品审核状态
        invalidReason: '' // 审核信息
      },
      formRules: {
        invalidReason: { required: true, message: '不通过原因不能为空', tigger: 'blur' }
      },
      doctorImgList: [],
      bannerImgList: [],
      equipmentImgList: []
    }
  },
  created() {
    this.auditForm.doctorId = parseInt(this.$route.query.id)
    this.type = this.$route.query.type
    this.fetchDoctorDetail()
    this.auditDoctorCheck()
  },
  methods: {
    async auditDoctorCheck() {
      try {
        await auditDoctorCheck(this.auditForm.doctorId)
      } catch (error) {
        console.log(error)
      }
    },

    // 表单数据回显
    async fetchDoctorDetail() {
      try {
        const res = await doctorFormData({ doctorId: this.auditForm.doctorId })
        this.setFormData(res.data)
      } catch (error) {
        console.log(error)
      }
    },

    // 设置表单数据
    setFormData(data) {
      this.doctorInfo.doctorName = data.doctorName
      this.doctorInfo.certificateNo = data.certificateNo
      this.doctorInfo.clubName = data.clubName
      this.doctorInfo.bannerList = data.bannerList
      this.doctorInfo.doctorImage = data.doctorImage
      this.doctorInfo.tagList = data.tagList
      this.doctorInfo.paramList = data.paramList
      this.doctorImgList = [data.doctorImage]
      this.bannerImgList = data.bannerList
    },

    // 提交审核信息
    async submit() {
      try {
        await this.$refs.formRef.validate()
        this.onAudit()
      } catch (error) {
        console.log(error)
      }
    },

    // 审核
    async onAudit() {
      try {
        this.isLoading = true
        const res = await auditDoctor(this.auditForm)
        this.$message.success(res.data)
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.back()
      } catch (error) {
        console.log(error)
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.doctor-edit {
  margin-bottom: 80px;
}

.doctor-edit-form {
  width: 650px;
  margin: 0 auto;
  margin-top: 80px;
}
.submit-btn {
  text-align: center;
  .el-button {
    width: 140px;
  }
}

.device-section {
  .section {
    position: relative;
    border: 1px solid #eee;
    padding: 25px 25px 0 16px;
    margin-bottom: 16px;
    .remove {
      position: absolute;
      right: 6px;
      top: 6px;
      font-size: 14px;
      cursor: pointer;
      background: #f56c6c;
      color: #fff;
      padding: 2px;
      border-radius: 50%;
      transition: all 0.2s;
      &:hover {
        background: red;
      }
    }
  }
  .el-form-item {
    padding-bottom: 25px;
  }
}
</style>
