<template>
  <div class="EducationalExperience">
    <div class="title">
      <span>{{ $t('curriculumVitae:EducationalExperience:title') }}</span>
      <el-button
        type="text"
        class="editBtn"
        @click.stop="openEdit"
        :disabled="!reviewerId"
      >
        {{ $t('common:button:add') }}
      </el-button>
    </div>
    <el-table
      :data="DATA"
      v-loading="loading"
      style="width: 100%"
      :header-cell-style="{ background: '#eee', color: '#606266' }"
    >
      <el-table-column
        prop="date"
        :label="$t('curriculumVitae:EducationalExperience:table:time')"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.BeginDateStr }} ~ {{ scope.row.EndDateStr }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="date"
        :label="$t('curriculumVitae:EducationalExperience:table:specialy')"
      >
        <template slot-scope="scope">
          <span>
            {{ isEN ? scope.row.Major : scope.row.MajorCN }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="date"
        :label="$t('curriculumVitae:EducationalExperience:table:degree')"
      >
        <template slot-scope="scope">
          <span>
            {{ $fd('Degree', Number(scope.row.Degree)) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="date"
        :label="$t('curriculumVitae:EducationalExperience:table:school')"
      >
        <template slot-scope="scope">
          <span>
            {{ isEN ? scope.row.Organization : scope.row.OrganizationCN }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="date"
        :label="$t('curriculumVitae:EducationalExperience:table:city')"
      >
        <template slot-scope="scope">
          <span>
            {{ isEN ? scope.row.City : scope.row.CityCN }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="date" :label="$t('common:action:action')">
        <template slot-scope="scope">
          <el-button
            type="text"
            class="editBtn"
            @click.stop="openEdit(scope.row)"
          >
            {{ $t('common:button:edit') }}
          </el-button>
          <el-button
            type="text"
            class="editBtn"
            @click.stop="handleDel(scope.row)"
          >
            {{ $t('common:button:delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <base-model :config="model_cfg">
      <template slot="dialog-body">
        <el-form
          ref="EducationalExperienceFrom"
          v-loading="loading"
          :model="form"
          :rules="rules"
          label-width="80px"
          size="small"
        >
          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:time')"
            prop="BeginDate"
          >
            <el-date-picker
              clearable
              v-model="daterange"
              type="monthrange"
              value-format="yyyy-MM-dd"
              format="yyyy-MM"
              :range-separator="$t('curriculumVitae:daterange:rangeSeparator')"
              :start-placeholder="$t('curriculumVitae:daterange:startTime')"
              :end-placeholder="$t('curriculumVitae:daterange:endTime')"
              @change="changeTimeList"
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:specialy')"
            prop="Major"
            v-if="isEN"
          >
            <el-input
              clearable
              :placeholder="
                $t(
                  'curriculumVitae:EducationalExperience:placeholder:specialyEN'
                )
              "
              v-model="form.Major"
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:specialy')"
            prop="MajorCN"
            v-else
          >
            <el-input
              clearable
              :placeholder="
                $t('curriculumVitae:EducationalExperience:placeholder:specialy')
              "
              v-model="form.MajorCN"
              :maxlength="400"
            ></el-input>
          </el-form-item>

          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:degree')"
            prop="Degree"
          >
            <el-select v-model="form.Degree" placeholder="">
              <el-option
                v-for="item in $d.Degree"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:school')"
            prop="Organization"
            v-if="isEN"
          >
            <el-input
              v-model="form.Organization"
              clearable
              :placeholder="
                $t('curriculumVitae:EducationalExperience:placeholder:schoolEN')
              "
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:school')"
            prop="OrganizationCN"
            v-else
          >
            <el-input
              v-model="form.OrganizationCN"
              clearable
              :placeholder="
                $t('curriculumVitae:EducationalExperience:placeholder:school')
              "
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:city')"
            prop="City"
            v-if="isEN"
          >
            <el-input
              v-model="form.City"
              clearable
              :placeholder="
                $t('curriculumVitae:EducationalExperience:placeholder:City')
              "
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:city')"
            prop="CityCN"
            v-else
          >
            <el-input
              v-model="form.CityCN"
              clearable
              :placeholder="
                $t('curriculumVitae:EducationalExperience:placeholder:CityCN')
              "
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            :label="$t('curriculumVitae:EducationalExperience:form:Country')"
            prop="Country"
            v-if="isEN"
          >
            <el-input
              v-model="form.Country"
              clearable
              :placeholder="
                $t('curriculumVitae:EducationalExperience:placeholder:Country')
              "
              :maxlength="400"
            ></el-input>
          </el-form-item>
          <el-form-item
            prop="CountryCN"
            :label="$t('curriculumVitae:EducationalExperience:form:Country')"
            v-else
          >
            <el-input
              v-model="form.CountryCN"
              clearable
              :placeholder="
                $t(
                  'curriculumVitae:EducationalExperience:placeholder:CountryCN'
                )
              "
              :maxlength="400"
            ></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"
          @click="handleSave"
          :loading="loading"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </template>
    </base-model>
  </div>
</template>
<script>
import BaseModel from '@/components/BaseModel'
import { addOrUpdateEducationInfo, deleteEducationInfo } from '@/api/reviewers'
const defaultForm = () => {
  return {
    BeginDate: '',
    EndDate: '',
    Degree: '',
    Major: '',
    MajorCN: '',
    Organization: '',
    OrganizationCN: '',
    City: '',
    CityCN: '',
    Country: '',
    CountryCN: '',
  }
}
export default {
  name: 'EducationalExperience',
  components: { BaseModel },
  props: {
    DATA: {
      type: Array,
      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:EducationalExperience:form:title'),
        appendToBody: true,
      },
      form: defaultForm(),
      rules: {
        BeginDate: [
          {
            required: true,
            message: this.$t('common:ruleMessage:select'),
            trigger: 'blur',
          },
        ],
        EndDate: [
          {
            required: true,
            message: this.$t('common:ruleMessage:select'),
            trigger: 'blur',
          },
        ],
        Degree: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
        ],
        Major: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        MajorCN: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        Organization: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        OrganizationCN: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        City: [
          { required: true, message:this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        CityCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        Country: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
        CountryCN: [
          {
            required: true,
            message: this.$t('common:ruleMessage:specify'),
            trigger: 'blur',
          },
          {
            max: 400,
            message: this.$t('form:rules:maxLength:400'),
            trigger: 'blur',
          },
        ],
      },
      loading: false,
      daterange: [],
    }
  },
  methods: {
    openEdit(row) {
      this.form = defaultForm()
      this.daterange = []
      if (row) {
        Object.keys(this.form).forEach((key) => {
          if (row[key]) {
            this.form[key] = row[key]
          }
        })
        if (this.form.BeginDate && this.form.EndDate) {
          this.daterange = [this.form.BeginDate, this.form.EndDate]
        }
        if (this.form.Degree) {
          this.form.Degree = Number(this.form.Degree)
        }
        this.form.Id = row.Id
      }
      this.model_cfg.visible = true
    },
    handleCancle() {
      this.form = defaultForm()
      this.daterange = []
      this.model_cfg.visible = false
    },
    async handleSave() {
      try {
        let validate = await this.$refs.EducationalExperienceFrom.validate()
        if (!validate) return false
        if (this.reviewerId) {
          this.form.DoctorId = this.reviewerId
        }
        this.loading = true
        let res = await addOrUpdateEducationInfo(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 handleDel(row) {
      try {
        let confirm = await this.$confirm(
          this.$t('trials:trials-list:table:isDeleted')
        )
        if (!confirm) return false
        this.loading = true
        let res = await deleteEducationInfo(row.Id)
        this.loading = false
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:deletedSuccessfully'))
          this.$emit('getInfo')
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    changeTimeList() {
      if (this.daterange && this.daterange.length === 2) {
        this.form.BeginDate = this.daterange[0]
        this.form.EndDate = this.daterange[1]
      } else {
        this.form.BeginDate = null
        this.form.EndDate = null
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.EducationalExperience {
  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: 100%;
}
</style>