<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="mainSummarize.SummarizeEn || mainSummarize.Summarize"
      style="white-space: pre-wrap"
      v-html="isEN ? mainSummarize.SummarizeEn : mainSummarize.Summarize"
    ></div>
    <div class="noData" v-else>{{ $t('curriculumVitae:noData') }}</div>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <div class="form_title">
          {{ $t('curriculumVitae:summarize:form:tableTitle') }}
        </div>
        <el-button
          size="small"
          type="primary"
          @click="handleAdd()"
          style="float: right; margin-bottom: 5px"
        >
          {{ $t('common:button:add') }}
        </el-button>
        <el-table
          ref="summarizeTable"
          :data="list"
          border
          class="table"
          :loading="loading"
          size="small"
          :row-style="tableRowClassName"
        >
          <el-table-column type="index" width="30" />
          <el-table-column
            prop="Indication"
            :label="$t('system:reviewer:label:Indication')"
            show-overflow-tooltip
            v-if="!isEN"
          />
          <el-table-column
            prop="IndicationEn"
            :label="$t('system:reviewer:label:Indication')"
            show-overflow-tooltip
            v-else
          />
          <el-table-column
            prop="Summarize"
            :label="$t('system:reviewer:label:SummarizeZH')"
            show-overflow-tooltip
            v-if="!isEN"
          />
          <el-table-column
            prop="SummarizeEn"
            :label="$t('system:reviewer:label:SummarizeEN')"
            show-overflow-tooltip
            v-else
          />
          <el-table-column
            prop="IsMain"
            :label="$t('system:reviewer:label:IsMain')"
            show-overflow-tooltip
            width="100px"
          >
            <template slot-scope="scope">
              {{ $fd('YesOrNo', scope.row.IsMain) }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('system:EducationTraining:table:Operation')"
            width="120px"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleAdd(scope.row)">
                {{ $t('common:button:edit') }}
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="handleDelete(scope.row, scope.$index)"
              >
                {{ $t('common:button:delete') }}
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="setCurrent(scope.row)"
                v-if="trialId"
              >
                {{ $t('common:button:check') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-form
          ref="summarizeFrom"
          v-loading="loading"
          :model="form"
          :rules="rules"
          label-width="80px"
          size="small"
          v-if="trialId"
        >
          <div class="form_title">
            {{ $t('curriculumVitae:summarize:form:formTitle') }}
          </div>
          <template>
            <el-form-item>
              <div style="float: right">
                <!-- <el-button
                  type="text"
                  size="small"
                  @click="cover"
                  :disabled="!currentRow.Id"
                >
                  {{ $t('curriculumVitae:summarize:button:cover') }}
                </el-button> -->
                <el-button type="text" size="small" @click="addNew">
                  {{ $t('curriculumVitae:summarize:button:addNew') }}
                </el-button>
              </div>
            </el-form-item>
          </template>
          <el-form-item
            :label="$t('curriculumVitae:summarize:form:summarize')"
            prop="Summarize"
            v-if="!isEN"
          >
            <el-input
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:summarize')
              "
              v-model="form.Summarize"
              type="textarea"
              :rows="10"
              clearable
              :maxlength="4000"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="$t('curriculumVitae:summarize:form:summarize')"
            prop="SummarizeEn"
            v-else
          >
            <el-input
              v-model="form.SummarizeEn"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:summarizeEN')
              "
              type="textarea"
              :rows="10"
              clearable
              :maxlength="4000"
            ></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template slot="dialog-footer" v-if="trialId">
        <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>
    <base-model :config="model_cfg_table">
      <template slot="dialog-body">
        <el-form
          ref="summarizeFrom2"
          v-loading="loading"
          :rules="rules"
          :model="tableForm"
          class="demo-ruleForm"
          label-width="100px"
        >
          <el-form-item
            :label="$t('system:reviewer:label:Indication')"
            prop="Indication"
            v-if="!isEN"
          >
            <el-input
              v-model="tableForm.Indication"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:Indication')
              "
              size="small"
              :maxlength="400"
            />
          </el-form-item>
          <el-form-item
            prop="IndicationEn"
            :label="$t('system:reviewer:label:Indication')"
            v-else
          >
            <el-input
              v-model="tableForm.IndicationEn"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:IndicationEn')
              "
              size="small"
              :maxlength="400"
            />
          </el-form-item>
          <el-form-item
            :label="$t('system:reviewer:label:Summarize')"
            prop="Summarize"
            v-if="!isEN"
          >
            <el-input
              v-model="tableForm.Summarize"
              type="textarea"
              :rows="10"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:summarize')
              "
              size="small"
              :maxlength="4000"
            />
          </el-form-item>
          <el-form-item
            prop="SummarizeEn"
            :label="$t('system:reviewer:label:Summarize')"
            v-else
          >
            <el-input
              v-model="tableForm.SummarizeEn"
              type="textarea"
              :rows="10"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:summarizeEN')
              "
              size="small"
              :maxlength="4000"
            />
          </el-form-item>
          <el-form-item
            prop="IsMain"
            :label="$t('system:reviewer:label:IsMain')"
          >
            <el-switch
              v-model="tableForm.IsMain"
              :disabled="list.length <= 0 || ISMAIN"
            >
            </el-switch>
          </el-form-item>
        </el-form>
      </template>
      <template slot="dialog-footer">
        <el-button size="small" type="primary" @click="handleCancle2">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          size="small"
          type="primary"
          @click="handleSave2"
          :loading="loading"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
  </div>
</template>
<script>
import BaseModel from '@/components/BaseModel'
import {
  addOrUpdateGneralSituation,
  getSummarizeInfo,
  deleteSummarizeInfo,
} from '@/api/reviewers'
const defaultForm = () => {
  return {
    Id: null,
    DoctorId: null,
    Summarize: null,
    SummarizeEn: null,
    IsMain: false,
    Indication: null,
    IndicationEn: null,
    TrialId: null,
  }
}
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: '800px',
        title: this.$t('curriculumVitae:summarize:form:title'),
        appendToBody: true,
      },
      model_cfg_table: {
        visible: false,
        showClose: true,
        width: '800px',
        title: this.$t('system:reviewer:form:SummarizeTitle'),
        appendToBody: true,
        top: '8vh',
      },
      tableForm: defaultForm(),
      form: defaultForm(),
      rules: {
        Indication: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        IndicationEn: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        Summarize: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
        SummarizeEn: [
          {
            max: 4000,
            message: this.$t('form:rules:maxLength:4000'),
            trigger: 'blur',
          },
        ],
      },
      loading: false,
      list: [],
      currentRow: {},
      ISMAIN: false,
      SaveSummarizeInfoType: 0,
    }
  },
  computed: {
    mainSummarize() {
      if (Array.isArray(this.DATA.SummarizeList) && this.trialId) {
        return this.DATA.SummarizeList[0] || {}
      }
      if (Array.isArray(this.DATA.SummarizeList)) {
        return this.DATA.SummarizeList.find((item) => item.IsMain) || {}
      }
      return {}
    },
  },
  methods: {
    cover() {},
    addNew() {
      this.SaveSummarizeInfoType = 2
      this.tableForm = defaultForm()
      this.tableForm.Summarize = this.form.Summarize
      this.tableForm.SummarizeEn = this.form.SummarizeEn
      // this.tableForm.Indication = this.form.Indication
      // this.tableForm.IndicationEn = this.form.IndicationEn
      this.model_cfg_table.visible = true
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.Id === this.currentRow.Id) {
        return {
          background: '#d9ecff',
        }
      }
      return {}
    },
    handleAdd(row) {
      this.tableForm = defaultForm()
      this.ISMAIN = false
      this.SaveSummarizeInfoType = 0
      if (row) {
        Object.keys(this.tableForm).forEach((key) => {
          this.tableForm[key] = row[key]
        })
        this.tableForm.Id = row.Id
        if (this.tableForm.IsMain) {
          this.ISMAIN = true
        }
      }
      if (this.list.length <= 0) {
        this.tableForm.IsMain = true
      }
      this.model_cfg_table.visible = true
    },
    async handleDelete(row, index) {
      try {
        if (row.IsMain) {
          return this.$message.warning(
            this.$t('system:reviewer:message:deletedSuccessfully')
          )
        }
        let confirm = await this.$confirm(
          this.$t('system:reviewer:confirm:delete')
        )
        if (!confirm) return false
        let data = {
          Id: row.Id,
        }
        this.loading = true
        let res = await deleteSummarizeInfo(data)
        this.loading = false
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:deletedSuccessfully'))
          // this.getDetail()
          this.list.splice(index, 1)
        }
      } catch (err) {
        console.log(err)
        this.loading = false
      }
    },
    // 获取系统模板
    async getIndicationList() {
      try {
        let id = this.reviewerId
        if (!id) return false
        let res = await getSummarizeInfo({
          DoctorId: id,
        })
        if (res.IsSuccess) {
          this.list = res.Result.SummarizeList
        }
      } catch (err) {
        console.log(err)
      }
    },
    async openEdit() {
      this.form = defaultForm()
      Object.keys(this.form).forEach((key) => {
        // if (!this.isPM) {
        this.form[key] = this.mainSummarize[key]
        // }
      })
      this.getIndicationList()
      this.currentRow = {}
      this.model_cfg.visible = true
    },
    handleCancle() {
      this.form = defaultForm()
      this.currentRow = {}
      this.model_cfg.visible = false
    },
    handleCancle2() {
      this.tableForm = defaultForm()
      this.model_cfg_table.visible = false
    },
    async handleSave() {
      try {
        let validate = await this.$refs.summarizeFrom.validate()
        if (!validate) return false
        if (this.reviewerId) {
          this.form.DoctorId = this.reviewerId
        }
        if (this.trialId) {
          this.form.TrialId = this.trialId
        }
        this.loading = true
        let res = await addOrUpdateGneralSituation(this.form)
        this.loading = false
        if (res.IsSuccess) {
          this.$emit('getInfo')
          this.model_cfg.visible = false
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    async handleSave2() {
      try {
        let validate = await this.$refs.summarizeFrom2.validate()
        if (!validate) return false
        this.tableForm.DoctorId = this.reviewerId
        this.loading = true
        if (this.trialId && this.SaveSummarizeInfoType > 0) {
          this.tableForm.TrialId = this.trialId
          this.tableForm.SaveSummarizeInfoType = this.SaveSummarizeInfoType
        }
        if (this.list.length <= 0) {
          this.tableForm.IsMain = true
        }
        let res = await addOrUpdateGneralSituation(this.tableForm)
        this.loading = false
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.model_cfg_table.visible = false
          if (this.SaveSummarizeInfoType > 0) {
            this.$emit('getInfo')
            this.model_cfg.visible = false
            return false
          }
          this.getIndicationList()
          // this.getDetail()
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    handleCurrentChange(val) {
      this.currentRow = val
      this.form.Summarize = this.currentRow.Summarize
      this.form.SummarizeEn = this.currentRow.SummarizeEn
      this.form.Indication = this.currentRow.Indication
      this.form.IndicationEn = this.currentRow.IndicationEn
    },
    setCurrent(row) {
      // this.$refs.summarizeTable.setCurrentRow(row)
      this.handleCurrentChange(row)
    },
  },
}
</script>
<style lang="scss" scoped>
.summarize {
  min-height: 100px;
  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    font-weight: bold;
  }
  .message {
    width: 100%;
    margin: auto;
    min-height: 100px;
    background-color: #eee;
    padding: 10px;
    line-height: 30px;
    border-radius: 5px;
    word-wrap: break-word;
  }
}
.form_title {
  margin: 10px 0;
  display: flex;
  align-items: center;
  &::after {
    margin-left: 20px;
    display: block;
    content: '';
    border-top: 1px solid #eee;
    flex: 1;
  }
}
</style>