<template>
  <div class="doctor-edit">
    <el-form ref="formRef" class="doctor-edit-form" label-width="140px" :model="formData" :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">
        <el-image
          style="width: 140px; height: 140px"
          :src="doctorInfo.doctorImage"
          :preview-src-list="doctorImgList"
        />
      </el-form-item>
      <el-form-item label="轮播图:" prop="banner">
        <el-image
          v-for="(image,index) in doctorInfo.bannerList"
          :key="index"
          style="width: 140px; height: 140px"
          :src="image"
          :preview-src-list="bannerImgList"
        />
      </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="审核状态：">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="0">不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.auditStatus!==1" prop="invalidReason" label="原因：">
        <el-input v-model="formData.invalidReason" type="textarea" placeholder="请说明原因" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="$_back">返回</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { doctorFormData, doctorAudit } from '@/api/doctor'

export default {
  data() {
    return {
      isRequest: true,
      doctorId: '',
      point: {},
      disabled: false,
      doctorInfo: {
        doctorName: '',
        certificateNo: '',
        clubName: '',
        bannerList: '',
        doctorImage: '',
        tagList: [],
        paramsList: []
      },
      formData: {
        auditBy: '', // 审核人id
        doctorId: '', // 商品id
        auditStatus: 1, // 商品审核状态
        invalidReason: '' // 审核信息
      },
      formRules: {
        invalidReason: { required: true, message: '不通过原因不能为空', tigger: 'blur' }
      },
      doctorImgList: [],
      bannerImgList: [],
      equipmentImgList: []
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.doctorId = this.formData.doctorId = parseInt(this.$route.query.id)
    this.initFormData()
  },
  methods: {
    // 表单数据回显
    initFormData() {
      doctorFormData({ doctorId: this.doctorId }).then(res => {
        this.setFormData(res.data)
      })
    },
    // 设置表单数据
    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
    },

    // 提交审核信息
    submit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.isLoading = true
          // 指定审核人
          this.formData.auditBy = this.authUserId
          // 提交审核信息
          doctorAudit(this.formData).then(res => {
            if (res.code !== 0) return
            this.$message.success(res.data)
            this.$store.dispatch('tagsView/delView', this.$route)
            this.$router.back()
          }).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>
