<template>
  <div class="scientificResearchProject">
    <div class="title">
      <span>{{ $t('curriculumVitae:scientificResearchProject: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.Research || DATA.ResearchCN || DATA.Grants || DATA.GrantsCN"
    >
      <el-form class="demo-form-inline">
        <!-- <el-form-item
          :label="$t('curriculumVitae:scientificResearchProject:direction')"
        >
          <div>
            <span v-if="isEN">{{ DATA.Research }}</span>
            <span v-else>{{ DATA.ResearchCN }}</span>
          </div>
        </el-form-item> -->
        <div style="display: flex; font-size: 14px">
          <span style="display: inline-block; width: 80px; color: #606266">{{
            $t('curriculumVitae:scientificResearchProject:direction')
          }}</span>
          <span
            class="break-word"
            style="white-space: pre-wrap; width: calc(100% - 80px)"
            v-html="isEN ? DATA.Research : DATA.ResearchCN"
          ></span>
        </div>
        <!-- <el-form-item
          :label="$t('curriculumVitae:scientificResearchProject:subject')"
        >
          <div>
            <span
              style="white-space: pre-wrap"
              v-html="isEN ? DATA.Grants : DATA.GrantsCN"
            ></span>
          </div>
        </el-form-item> -->
        <div style="display: flex; font-size: 14px">
          <span style="display: inline-block; width: 80px; color: #606266">{{
            $t('curriculumVitae:scientificResearchProject:subject')
          }}</span>
          <span
            class="break-word"
            style="white-space: pre-wrap; width: calc(100% - 80px)"
            v-html="isEN ? DATA.Grants : DATA.GrantsCN"
          ></span>
        </div>
      </el-form>
    </div>
    <div class="noData" v-else>{{ $t('curriculumVitae:noData') }}</div>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="scientificResearchProjectFrom"
          v-loading="loading"
          :model="form"
          :rules="rules"
          label-width="80px"
          size="small"
        >
          <el-form-item
            :label="
              $t('curriculumVitae:scientificResearchProject:form:direction')
            "
            prop="Research"
            v-if="isEN"
          >
            <el-input
              clearable
              v-model="form.Research"
              style="margin-bottom: 10px"
              :placeholder="
                $t(
                  'curriculumVitae:scientificResearchProject:placeholder:directionEN'
                )
              "
              type="textarea"
              :rows="2"
              :maxlength="4000"
            ></el-input>
          </el-form-item>
          <el-form-item
            prop="ResearchCN"
            :label="
              $t('curriculumVitae:scientificResearchProject:form:direction')
            "
            v-else
          >
            <el-input
              v-model="form.ResearchCN"
              clearable
              :placeholder="
                $t(
                  'curriculumVitae:scientificResearchProject:placeholder:direction'
                )
              "
              type="textarea"
              :rows="2"
              :maxlength="4000"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="
              $t('curriculumVitae:scientificResearchProject:form:subject')
            "
            prop="Grants"
            v-if="isEN"
          >
            <el-input
              v-model="form.Grants"
              clearable
              style="margin-bottom: 10px"
              type="textarea"
              :rows="5"
              :maxlength="4000"
              :placeholder="
                $t(
                  'curriculumVitae:scientificResearchProject:placeholder:subjectEN'
                )
              "
            ></el-input>
          </el-form-item>
          <el-form-item
            prop="GrantsCN"
            :label="
              $t('curriculumVitae:scientificResearchProject:form:subject')
            "
            v-else
          >
            <el-input
              clearable
              v-model="form.GrantsCN"
              type="textarea"
              :rows="5"
              :maxlength="4000"
              :placeholder="
                $t(
                  'curriculumVitae:scientificResearchProject:placeholder:subject'
                )
              "
            ></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,
    Research: '',
    ResearchCN: '',
    Grants: '',
    GrantsCN: '',
  }
}
export default {
  name: 'scientificResearchProject',
  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: '600px',
        title: this.$t('curriculumVitae:scientificResearchProject:form:title'),
        appendToBody: true,
      },
      form: defaultForm(),
      rules: {
        Research: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
        ResearchCN: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
        Grants: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
        GrantsCN: [
          {
            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.scientificResearchProjectFrom.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>
.scientificResearchProject {
  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>