<template>
  <div class="form-container">
    <div class="title-wrapper">
      <p>Clinical Trial Experience</p>
      <el-button :disabled="$route.query.ReviewStatus === '1'" class="add" size="small" @click="handleAddClinicalTrial">Add</el-button>
    </div>
    <div style="padding:0 40px;">
      <el-table
        ref="clinicalTrialTbl"
        v-loading="loading"
        :data="clinicalTrialList"
        class="table"
        size="small"
      >
        <el-table-column type="index" width="40" />
        <el-table-column prop="Phase" label="Phase" min-width="50" />
        <el-table-column prop="EvaluationCriteriaList" label="Review Criteria" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.EvaluationCriteriaList.length>0? scope.row.EvaluationCriteriaList.join(', '):'' }}
          </template>
        </el-table-column>
        <el-table-column prop="EvaluationCriteriaList" label="Starting And Ending Date" min-width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.StartTime">{{ scope.row.StartTime.split('-')[0] }}-{{scope.row.EndTime ? scope.row.EndTime.split('-')[0] : '至今'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="VisitReadingCount" label="Visit Reading Count" min-width="70" />
        <el-table-column prop="EvaluationContent" label="Indication" min-width="70" />
        <el-table-column label="Operation" min-width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" :disabled="$route.query.ReviewStatus === '1'" @click="handleEdit(scope.row)">Edit</el-button>
            <el-button type="text" size="small" :disabled="$route.query.ReviewStatus === '1'" @click="handleDel(scope.row)">Delete</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="title-wrapper" style="margin-top:10px;">
      <p>GCP Certificate</p>
    </div>
    <div style="margin-left:40px;width:40%">
      <!-- <el-radio-group v-model="GCP" style="margin-bottom:20px">
        <el-radio :label="1">Yes</el-radio>
        <el-radio :label="0">No</el-radio>
      </el-radio-group>
      <upload-file v-if="GCP" :doctor-id="doctorId" type="GCP" @getFileList="getFileList" />
      <div><el-button type="primary" size="small" style="margin-top:20px" @click="handleSaveGCP">Save</el-button></div> -->
      <gcp-certificate :doctor-id="doctorId" :gcp="GCP" :gcp-id="GCPID" />
    </div>
    <div class="title-wrapper" style="margin-top:10px;">
      <p>Other Relevant Experience</p>
    </div>
    <div style="padding:0 40px;">

      <el-row type="flex" justify="space-between">
        <el-col :span="11">

          <el-input
            v-model="OtherClinicalExperience"
            type="textarea"
            rows="5"
            placeholder="Please specify in English"
            size="small"
          />
        </el-col>
        <el-col :span="11">
          <el-input v-model="OtherClinicalExperienceCN" :disabled="$route.query.ReviewStatus === '1'" type="textarea" rows="5" placeholder="请用中文注明" size="small" />
        </el-col>
      </el-row>
      <el-button type="primary" size="small" style="margin-top:20px" :disabled="isBtnDisabled || $route.query.ReviewStatus === '1'" @click="handleSaveOtherClinical">Save</el-button>
    </div>
    <el-dialog v-if="clinicalTrialDialogVisible" :title="clinicalTrialDialogTitle" :visible.sync="clinicalTrialDialogVisible" width="480px" :close-on-click-modal="false">
      <el-form
        ref="clinicalTrialForm"
        label-width="150px"
        :rules="clinicalTrialRules"
        class="demo-ruleForm"
        :model="clinicalTrialForm"
        size="small"
      >
        <el-form-item label="Phase" prop="PhaseId">
          <el-select v-model="clinicalTrialForm.PhaseId" placeholder="Please select">
            <!-- <el-option
              v-for="(key,value) of dictionaryList.Phase"
              :key="value"
              :label="key"
              :value="value"
            /> -->
            <el-option
              v-for="item of dictionaryList.Trial_Phase"
              :key="item.Id"
              :label="item.Value"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Review Criteria" prop="EvaluationCriteriaIdList">
          <el-select
            v-model="clinicalTrialForm.EvaluationCriteriaIdList"
            placeholder="Please select"
            multiple
          >
            <!-- <el-option v-for="(key,value) of dictionaryList.ReadingStandard" :key="value" :label="key" :value="value" /> -->
            <el-option
              v-for="item of dictionaryList.ReadingStandard"
              :key="item.Id"
              :label="item.Value"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="Start Time" prop="StartTime">
          <el-date-picker
            v-model="clinicalTrialForm.StartTime"
            type="year"
            placeholder="Select Start Time">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="End Time" prop="EndTime">
          <el-date-picker
            v-model="clinicalTrialForm.EndTime"
            type="year"
            placeholder="Select End Time">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="Visit Reading Count" prop="VisitReadingCount">
          <el-input style="width: 100%" oninput="value=value.replace(/[^\d]/g,'')" v-model="clinicalTrialForm.VisitReadingCount" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="Indication" prop="EvaluationContent">
          <el-input v-model="clinicalTrialForm.EvaluationContent" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" :loading="isDisabled" @click="handleSave">Save</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// import UploadFile from '@/components/UploadFile'
import { getTrialExperience, addOrUpdateTrialExperience, deleteTrialExperience, updateOtherExperience } from '@/api/reviewers'
import { getBasicDataSelects } from '@/api/dictionary/dictionary'
import GcpCertificate from './GcpCertificate.vue'
const getClinicalTrialDefault = () => {
  return {
    PhaseId: '',
    EvaluationCriteriaIdList: [],
    EvaluationContent: '',
    VisitReadingCount: 0,
    StartTime: null,
    EndTime: null,
  }
}
export default {
  name: 'TrialExperience',
  components: { GcpCertificate },
  data() {
    return {
      loading: false,
      clinicalTrialList: [],
      clinicalTrialDialogTitle: '',
      clinicalTrialDialogVisible: false,
      clinicalTrialForm: getClinicalTrialDefault(),
      clinicalTrialRules: {
        PhaseId: [
          { required: true, message: 'Please select', trigger: ['blur', 'change'] }
        ],
        EvaluationCriteriaIdList: [
          { required: true, message: 'Please select', trigger: ['blur', 'change'] }
        ],
        VisitReadingCount: [
          { required: true, message: 'Please specify', trigger: 'blur' },
        ],
        EvaluationContent: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 300, message: 'The maximum length is 300' }
        ],
        StartTime: [
          { required: true, message: 'Please specify', trigger: 'blur' },
        ],
      },
      GCP: 0,
      GCPID: '',
      OtherClinicalExperience: '',
      OtherClinicalExperienceCN: '',
      isDisabled: false,
      doctorId: this.$route.query.Id,
      isBtnDisabled: false,
      selectId: '00000000-0000-0000-0000-000000000000',
      dictionaryList: {}
    }
  },
  created() {
    this.initForm()
  },
  methods: {
    async initForm() {
      await this.getDicData()
      const id = this.$route.query.Id
      if (id) {
        this.loading = true
        getTrialExperience(id).then(res => {
          this.clinicalTrialList = res.Result.ClinicalTrialExperienceList
          this.GCP = res.Result.GCP
          this.GCPID = res.Result.GCPId
          this.OtherClinicalExperience = res.Result.OtherClinicalExperience
          this.OtherClinicalExperienceCN = res.Result.OtherClinicalExperienceCN
          this.loading = false
        }).catch(() => { this.loading = false })
      }
    },
    getDicData() {
      getBasicDataSelects(['Trial_Phase', 'ReadingStandard']).then(res => {
        this.dictionaryList = { ...res.Result }
      })
    },
    handleAddClinicalTrial() {
      this.clinicalTrialDialogTitle = 'New'
      this.clinicalTrialDialogVisible = true
      this.clinicalTrialForm.Id = null
      this.clinicalTrialForm = Object.assign({}, getClinicalTrialDefault())
    },
    handleEdit(row) {
      const { Id, PhaseId, EvaluationCriteriaIdList, EvaluationContent, StartTime, EndTime, VisitReadingCount } = row
      this.clinicalTrialForm = Object.assign({}, getClinicalTrialDefault())
      this.clinicalTrialDialogTitle = 'Edit'
      this.clinicalTrialDialogVisible = true
      this.clinicalTrialForm.Id = Id
      this.clinicalTrialForm.StartTime = StartTime
      this.clinicalTrialForm.EndTime = EndTime
      this.clinicalTrialForm.PhaseId = PhaseId
      this.clinicalTrialForm.VisitReadingCount = VisitReadingCount
      this.clinicalTrialForm.EvaluationCriteriaIdList = EvaluationCriteriaIdList
      this.clinicalTrialForm.EvaluationContent = EvaluationContent
    },
    handleSave() {
      this.$refs.clinicalTrialForm.validate(valid => {
        if (valid) {
          this.isDisabled = true
          this.clinicalTrialForm.DoctorId = this.$route.query.Id
          var dateTime
          if (this.clinicalTrialForm.StartTime) {
            dateTime = new Date(this.clinicalTrialForm.StartTime)
            this.clinicalTrialForm.StartTime = new Date(dateTime.setDate(dateTime.getDate()+1))
          }
          if (this.clinicalTrialForm.EndTime) {
            dateTime = new Date(this.clinicalTrialForm.EndTime)
            this.clinicalTrialForm.EndTime = new Date(dateTime.setDate(dateTime.getDate()+1))
          }
          addOrUpdateTrialExperience(this.clinicalTrialForm).then(res => {
            this.isDisabled = false
            this.initForm()
            if (this.clinicalTrialForm.Id) {
              this.$message({
                message: 'Updated successfully',
                type: 'success'
              })
            } else {
              this.$message({
                message: 'Addedd successfully',
                type: 'success'
              })
            }
            this.clinicalTrialDialogVisible = false
          })
            .catch(() => {
              this.isDisabled = false
            })
        }
      })
    },
    handleDel(row) {
      this.$confirm('Confirm to delete?', {
        type: 'warning',
        distinguishCancelAndClose: true,
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel'
      })
        .then(() => {
          deleteTrialExperience(row.Id)
            .then(res => {
              this.clinicalTrialList.splice(this.clinicalTrialList.findIndex(item => item.Id === row.Id), 1)
              this.$message.success('Deleted successfully !')
            })
        })
        .catch(action => {})
    },
    handleSaveOtherClinical() {
      const param = { DoctorId: this.doctorId, OtherClinicalExperience: this.OtherClinicalExperience, OtherClinicalExperienceCN: this.OtherClinicalExperienceCN }
      updateOtherExperience(param).then(res => {
        if (res.IsSuccess) {
          if (this.id) {
            this.$message.success('Updated successfully')
          } else {
            this.$message.success('Addedd successfully')
          }
        }
      })
    }
  }
}
</script>
