<template>
  <div class="treatise">
    <div class="title">
      <span>{{ $t('curriculumVitae:treatise:title') }}</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.Publications"
      v-html="DATA.Publications"
    ></div>
    <div class="noData" v-else>{{ $t('curriculumVitae:noData') }}</div>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="treatiseFrom"
          v-loading="loading"
          :model="form"
          :rules="rules"
          label-width="80px"
          size="small"
        >
          <el-form-item
            :label="$t('curriculumVitae:treatise:form:name')"
            prop="Publications"
          >
            <el-input
              v-model="form.Publications"
              clearable
              type="textarea"
              :rows="10"
              :maxlength="4000"
            ></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,
    Publications: '',
  }
}
export default {
  name: 'treatise',
  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:treatise:form:title'),
        appendToBody: true,
      },
      form: defaultForm(),
      rules: {
        Publications: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
      },
      loading: false,
      daterange: [],
    }
  },
  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.treatiseFrom.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>
.treatise {
  min-height: 100px;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    font-weight: bold;
  }
}
.el-select,
.el-date-editor {
  width: 97%;
}
.message {
  margin: auto;
  min-height: 100px;
  background-color: #eee;
  padding: 10px;
  line-height: 30px;
  border-radius: 5px;
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style>