<template>
  <div class="other">
    <div class="title">
      <span>{{ $t('curriculumVitae:other:AH') }}</span>
      <el-button
        type="text"
        class="editBtn"
        :disabled="!reviewerId"
        @click.stop="openEdit"
      >
        {{ $t('common:button:edit') }}
      </el-button>
    </div>
    <div class="message" v-if="DATA.AwardsHonors || DATA.AwardsHonorsCN">
      <span
        style="white-space: pre-wrap"
        v-html="isEN ? DATA.AwardsHonors : DATA.AwardsHonorsCN"
      ></span>
    </div>
    <div class="noData" v-else>{{ $t('curriculumVitae:noData') }}</div>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="otherFrom"
          v-loading="loading"
          :model="form"
          :rules="rules"
          label-width="100px"
          size="small"
        >
          <el-form-item
            :label="$t('curriculumVitae:other:form:AH')"
            prop="AwardsHonors"
            v-if="isEN"
          >
            <el-input
              clearable
              v-model="form.AwardsHonors"
              style="margin-bottom: 10px"
              type="textarea"
              :rows="10"
              :maxlength="4000"
              :placeholder="$t('curriculumVitae:other:placeholder:AHEN')"
            ></el-input>
          </el-form-item>
          <el-form-item
            prop="AwardsHonorsCN"
            :label="$t('curriculumVitae:other:form:AH')"
            v-else
          >
            <el-input
              clearable
              v-model="form.AwardsHonorsCN"
              style="margin-bottom: 10px"
              type="textarea"
              :rows="10"
              :maxlength="4000"
              :placeholder="$t('curriculumVitae:other:placeholder:AH')"
            ></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template slot="dialog-footer">
        <el-button size="small" type="primary" @click="handleCancle">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          @click="handleSave"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
  </div>
</template>
<script>
import BaseModel from '@/components/BaseModel'
import { addOrUpdateResearchPublicationInfo } from '@/api/reviewers'
const defaultForm = () => {
  return {
    Id: null,
    AwardsHonors: '',
    AwardsHonorsCN: '',
  }
}
export default {
  name: 'other',
  components: { BaseModel },
  props: {
    DATA: {
      type: Object,
      required: true,
      default: () => {
        return {}
      },
    },
    reviewerId: {
      type: String,
      default: '',
    },
    isEN: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      model_cfg: {
        visible: false,
        showClose: true,
        width: '800px',
        title: this.$t('curriculumVitae:scientificResearchProject:form:title'),
        appendToBody: true,
      },
      form: defaultForm(),
      rules: {
        AwardsHonors: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
        AwardsHonorsCN: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
      },
      loading: false,
    }
  },
  methods: {
    openEdit() {
      this.form = defaultForm()
      Object.keys(this.form).forEach((key) => {
        if (this.DATA[key]) {
          this.form[key] = this.DATA[key]
        }
      })
      this.model_cfg.visible = true
    },
    handleCancle() {
      this.form = defaultForm()
      this.model_cfg.visible = false
    },
    async handleSave() {
      try {
        let validate = await this.$refs.otherFrom.validate()
        if (!validate) return false
        if (this.reviewerId) {
          this.form.DoctorId = this.reviewerId
        }
        this.loading = true
        let res = await addOrUpdateResearchPublicationInfo(this.form)
        this.loading = false
        if (res.IsSuccess) {
          this.$emit('getInfo')
          this.model_cfg.visible = false
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.other {
  min-height: 100px;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    font-weight: bold;
  }
  .message {
    margin: auto;
    min-height: 100px;
    background-color: #eee;
    padding: 10px;
    line-height: 30px;
    border-radius: 5px;
    word-wrap: break-word;
  }
}
.el-select,
.el-date-editor {
  width: 97%;
}
</style>