<template>
  <div class="form-container">
    <el-card class="box-card">
      <div style="width: 80%">
        <div class="top">
          <span>{{ $t('system:reviewer:tab:Summarize') }}</span>
          <el-button
            size="small"
            type="primary"
            v-if="$route.query.ReviewStatus !== '1'"
            @click="handleAdd()"
          >
            {{ $t('common:button:add') }}
          </el-button>
        </div>
        <el-table
          ref="educationTbl"
          :data="list"
          border
          class="table"
          :loading="loading"
          size="small"
        >
          <el-table-column type="index" width="30" />
          <el-table-column
            prop="Indication"
            :label="$t('system:reviewer:label:Indication')"
            min-width="55"
            show-overflow-tooltip
          />
          <el-table-column
            prop="Summarize"
            :label="$t('system:reviewer:label:SummarizeZH')"
            min-width="55"
            show-overflow-tooltip
          />
          <el-table-column
            prop="SummarizeEn"
            :label="$t('system:reviewer:label:SummarizeEN')"
            min-width="70"
            show-overflow-tooltip
          />
          <el-table-column
            prop="IsMain"
            :label="$t('system:reviewer:label:IsMain')"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{ $fd('YesOrNo', scope.row.IsMain) }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('system:EducationTraining:table:Operation')"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                :disabled="$route.query.ReviewStatus === '1'"
                @click="handleAdd(scope.row)"
              >
                {{ $t('common:button:edit') }}
              </el-button>
              <el-button
                type="text"
                size="small"
                :disabled="$route.query.ReviewStatus === '1'"
                @click="handleDelete(scope.row)"
              >
                {{ $t('common:button:delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="summarizeFrom"
          v-loading="loading"
          :rules="rules"
          :model="form"
          class="demo-ruleForm"
          label-width="100px"
        >
          <el-form-item
            :label="$t('system:reviewer:label:Indication')"
            prop="Indication"
          >
            <el-input
              v-model="form.Indication"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:Indication')
              "
              size="small"
              :maxlength="400"
            />
          </el-form-item>
          <el-form-item prop="IndicationEn">
            <el-input
              v-model="form.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"
          >
            <el-input
              v-model="form.Summarize"
              type="textarea"
              :rows="10"
              :placeholder="
                $t('curriculumVitae:summarize:form:placeholder:summarize')
              "
              size="small"
              :maxlength="4000"
            />
          </el-form-item>
          <el-form-item prop="SummarizeEn">
            <el-input
              v-model="form.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="form.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="handleCancle">
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button
          size="small"
          type="primary"
          @click="handleSave"
          :loading="loading"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
  </div>
</template>
<script>
import {
  addOrUpdateGneralSituation,
  getSummarizeInfo,
  deleteSummarizeInfo,
} from '@/api/reviewers'
import BaseModel from '@/components/BaseModel'
const defaultForm = () => {
  return {
    Id: null,
    DoctorId: null,
    Summarize: null,
    SummarizeEn: null,
    IsMain: false,
    Indication: null,
    IndicationEn: null,
  }
}
export default {
  props: {
    reviewerId: {
      type: String,
    },
  },
  components: { BaseModel },
  data() {
    return {
      list: [],
      loading: false,
      ISMAIN: false,
      model_cfg: {
        visible: false,
        showClose: true,
        width: '800px',
        title: this.$t('system:reviewer:form:SummarizeTitle'),
        appendToBody: false,
        top: '8vh',
      },
      form: defaultForm(),
      rules: {
        Indication: [
          {
            required: true,
            message: 'Please enter Indication',
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        IndicationEn: [
          {
            required: true,
            message: 'Please enter Indication',
            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',
          },
        ],
      },
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    async handleSave() {
      try {
        let validate = await this.$refs.summarizeFrom.validate()
        if (!validate) return false
        this.form.DoctorId = this.$route.query.Id || this.reviewerId
        this.loading = true
        if (this.$route.query.trialId) {
          this.form.TrialId = this.$route.query.trialId
        }
        if (this.list.length <= 0) {
          this.form.IsMain = true
        }
        let res = await addOrUpdateGneralSituation(this.form)
        this.loading = false
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.model_cfg.visible = false
          this.getDetail()
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    // 获取详情
    async getDetail() {
      try {
        let id = this.$route.query.Id || this.reviewerId
        if (!id) return false
        this.loading = true
        let res = await getSummarizeInfo({
          DoctorId: id,
        })
        this.loading = false
        if (res.IsSuccess) {
          this.list = res.Result.SummarizeList
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    handleCancle() {
      this.model_cfg.visible = false
    },
    handleAdd(row) {
      this.form = defaultForm()
      this.ISMAIN = false
      if (row) {
        Object.keys(this.form).forEach((key) => {
          this.form[key] = row[key]
        })
        this.form.Id = row.Id
        if (this.form.IsMain) {
          this.ISMAIN = true
        }
      }
      if (this.list.length <= 0) {
        this.form.IsMain = true
      }
      this.model_cfg.visible = true
    },
    async handleDelete(row) {
      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()
        }
      } catch (err) {
        console.log(err)
        this.loading = false
      }
    },
  },
}
</script>
  <style lang="scss" scoped>
.basic-form {
  .el-input--small {
    width: 220px !important;
  }
  .el-select--small {
    width: 220px !important;
  }
}
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  margin-bottom: 10px;
}
</style>