<template>
  <div class="summarize">
    <div class="title">
      <span>{{ $t('curriculumVitae:summarize: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.SummarizeEn || DATA.Summarize">
      {{ isEN ? DATA.SummarizeEn : DATA.Summarize }}
    </div>
    <div class="noData" v-else>{{ $t('curriculumVitae:noData') }}</div>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="summarizeFrom"
          v-loading="loading"
          :model="form"
          :rules="rules"
          label-width="80px"
          size="small"
        >
          <el-form-item
            :label="$t('curriculumVitae:summarize:form:summarize')"
            prop="Summarize"
          >
            <el-input
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:summarize')
              "
              v-model="form.Summarize"
              type="textarea"
              :rows="2"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item prop="SummarizeEn">
            <el-input
              v-model="form.SummarizeEn"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:summarizeEN')
              "
              type="textarea"
              :rows="2"
              clearable
            ></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 { updateGneralSituation } from '@/api/reviewers'
const defaultForm = () => {
  return {
    SummarizeEn: '',
    Summarize: '',
  }
}
export default {
  name: 'summarize',
  components: { BaseModel },
  props: {
    DATA: {
      type: Object,
      required: true,
      default: () => {
        return {}
      },
    },
    reviewerId: {
      type: String,
      default: '',
    },
    trialId: {
      type: String,
      default: '',
    },
    isEN: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      model_cfg: {
        visible: false,
        showClose: true,
        width: '600px',
        title: this.$t('curriculumVitae:summarize:form:title'),
        appendToBody: true,
      },
      form: defaultForm(),
      rules: {},
      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.summarizeFrom.validate()
        if (!validate) return false
        if (this.reviewerId) {
          this.form.Id = this.reviewerId
        }
        if (this.trialId) {
          this.form.trialId = this.trialId
        }
        this.loading = true
        let res = await updateGneralSituation(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>
.summarize {
  min-height: 100px;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
  }
  .message {
    width: 100%;
    margin: auto;
    min-height: 100px;
    background-color: #eee;
    padding: 10px;
    line-height: 30px;
    border-radius: 5px;
    word-wrap: break-word;
  }
}
</style>