<template>
  <el-form
    ref="taskAllocationRuleDataForm"
    v-loading="loading"
    :model="form"
    :rules="rules"
    size="small"
  >
    <div v-if="list.length > 0">
      <div style="text-align: right">
         <!-- 清空全部未读阅片人 -->
        <!-- <el-button size="mini" type="primary" style="position:relative;top: -10px" @click="clearAll">

          {{$t('trials:reviewAssign:clearReader:title:clearAll')}}
        </el-button> -->
      </div>
      <div v-if="readingType === 1" style="line-height: 32px;margin-bottom: 10px;">
        <!-- 阅片人（R1）： -->
        {{$t('trials:reviewAssign:clearReader:title:r')}}
        <el-tag v-if="findDoctorUser(list, 0).DoctorUser && !findDoctorUser(list, 0).IsCancelAssign" :closable="!findDoctorUser(list, 0).IsHaveReading" @close="myClose(findDoctorUser(list, 0))">
          {{findDoctorUser(list, 0).DoctorUser.UserName}}({{findDoctorUser(list, 0).DoctorUser.FullName}})
        </el-tag>
      </div>
      <div v-if="readingType === 2" style="line-height: 32px;margin-bottom: 10px;">
        <!-- 阅片人（R1）： -->
        {{$t('trials:reviewAssign:clearReader:title:r1')}}
        <el-tag v-if="findDoctorUser(list, 1).DoctorUser && !findDoctorUser(list, 1).IsCancelAssign" :closable="!findDoctorUser(list, 1).IsHaveReading" @close="myClose(findDoctorUser(list, 1))">
          {{findDoctorUser(list, 1).DoctorUser.UserName}}({{findDoctorUser(list, 1).DoctorUser.FullName}})
        </el-tag>
      </div>
      <div v-if="readingType === 2" style="line-height: 32px;margin-bottom: 10px;">
        <!-- 阅片人（R2）： -->
        {{$t('trials:reviewAssign:clearReader:title:r2')}}
        <el-tag v-if="findDoctorUser(list, 2).DoctorUser && !findDoctorUser(list, 2).IsCancelAssign" :closable="!findDoctorUser(list, 2).IsHaveReading" @close="myClose(findDoctorUser(list, 2))">
          {{findDoctorUser(list, 2).DoctorUser.UserName}}({{findDoctorUser(list, 2).DoctorUser.FullName}})
        </el-tag>
      </div>
      <div v-if="OtherInfo && OtherInfo.IsArbitrationReading && readingType === 2" style="line-height: 32px;margin-bottom: 10px;">
        <!-- 裁判（AD）： -->
        {{$t('trials:reviewAssign:clearReader:title:ad')}}
        <el-tag v-if="findDoctorUser(list, 3).DoctorUser && !findDoctorUser(list, 3).IsCancelAssign" :closable="!findDoctorUser(list, 3).IsHaveReading" @close="myClose(findDoctorUser(list, 3))">
          {{findDoctorUser(list, 3).DoctorUser.UserName}}({{findDoctorUser(list, 3).DoctorUser.FullName}})
        </el-tag>
      </div>
      <div style="line-height: 32px;margin-bottom: 10px;"  v-if="OtherInfo && OtherInfo.IsOncologyReading">
        <!-- 肿瘤阅片人（O1）： -->
        {{$t('trials:reviewAssign:clearReader:title:O1')}}
        <el-tag v-if="findDoctorUser(list, 4).DoctorUser && !findDoctorUser(list, 4).IsCancelAssign" :closable="!findDoctorUser(list, 4).IsHaveReading" @close="myClose(findDoctorUser(list, 4))">
          {{findDoctorUser(list, 4).DoctorUser.UserName}}({{findDoctorUser(list, 4).DoctorUser.FullName}})
        </el-tag>
      </div>
      <div style="line-height: 32px;margin-bottom: 10px;font-size: 12px;color:#f66;">
        <i class="el-icon-info"></i>
        <!-- 如果分配的阅片人已开始读片则不能清空 -->
        {{$t('trials:reviewAssign:clearReader:title:msg1')}}
      </div>
      <div>
        <!-- 原因 -->
        <el-form-item label-width="80px" :label="$t('trials:reviewAssign:clearReader:title:reason')" prop="Note">
          <el-input :maxLength="$inputMax" v-model="form.Note" type="textarea"  maxlength="500" show-word-limit></el-input>
        </el-form-item>
      </div>
    </div>
    <!-- 保存 -->
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <!-- 取消 -->
        <el-button
          :disabled="loading"
          size="small"
          type="primary"
          @click="close"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <el-button size="small" type="primary" :disabled="!findDoctorUser(list, 0).IsCancelAssign" :loading="loading" style="margin-top: 10px;" @click="save">
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>

<script>
import { getSubjectAssignedDoctorList, cancelSubjectAssignedDoctor, addSubjectCancelDoctorNote } from '@/api/trials/reading'

export default {
  name: 'ManualAllocationTable',
  props: {
    TrialReadingCriterionId: {
      type: String,
      default() {
        return ''
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    readingType: {
      type: Number,
      default() {
        return 0
      }
    }
  },
  data() {
    return {
      TrialId: this.$route.query.trialId,
      list: [],
      loading: false,
      btnLoading: false,
      form: {
        Note: ''
      },
      readingType: null,
      rules: {
        Note: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }],
      },
      OtherInfo: null
    }
  },
  mounted() {
    this.TrialId = this.$route.query.trialId
    this.loading = true
    getSubjectAssignedDoctorList(this.data.SubjectId, this.TrialReadingCriterionId).then(res => {
      this.list = res.Result
      this.readingType = res.OtherInfo.ReadingType
      this.OtherInfo = Object.assign({}, res.OtherInfo)
      this.loading = false
    }).catch(() => {
      this.loading = false
    })
  },
  methods: {
    clearAll () {
      var is = false
      this.form.Note = ''
      this.list.forEach((v,i) => {
        if (!v.IsHaveReading) {
          this.$set(v, 'IsCancelAssign', true)
          if (i + 1 === this.list.length) {
            // this.form.Note += `取消${this.$fd('ArmEnum', v.ArmEnum)}阅片人${v.DoctorUser.UserName}(${v.DoctorUser.FullName});`
            this.form.Note += `${this.$t('trials:reviewAssign:clearReader:title:cancelReader')}(${this.$fd('ArmEnum', v.ArmEnum)} ${v.DoctorUser.UserName}(${v.DoctorUser.FullName});`
          } else {
            // this.form.Note += `取消${this.$fd('ArmEnum', v.ArmEnum)}阅片人${v.DoctorUser.UserName}(${v.DoctorUser.FullName});\n`
            this.form.Note += `${this.$t('trials:reviewAssign:clearReader:title:cancelReader')}(${this.$fd('ArmEnum', v.ArmEnum)} ${v.DoctorUser.UserName}(${v.DoctorUser.FullName});\n`
          }
          is = true
        }
      })
      if (is) {
        // '已全部清空，请点击保存应用'
        this.$message.success(this.$t('trials:reviewAssign:clearReader:title:msg2'))
      } else {
        // '没有可清空的阅片人'
        this.$message.error(this.$t('trials:reviewAssign:clearReader:title:msg3'))
      }
    },
    myClose(o) {
      this.$set(o, 'IsCancelAssign', true)
      if (this.form.Note) {
        // this.form.Note += `\n取消${this.$fd('ArmEnum', o.ArmEnum)}阅片人${o.DoctorUser.UserName}(${o.DoctorUser.FullName});`
        this.form.Note += `\n${this.$t('trials:reviewAssign:clearReader:title:cancelReader')}(${o.DoctorUser.UserName}(${o.DoctorUser.FullName}));`
      } else {
        // this.form.Note += `取消${this.$fd('ArmEnum', o.ArmEnum)}阅片人${o.DoctorUser.UserName}(${o.DoctorUser.FullName});`
        this.form.Note += `${this.$t('trials:reviewAssign:clearReader:title:cancelReader')}(${o.DoctorUser.UserName}(${o.DoctorUser.FullName}));`
      }
    },
    findDoctorUser(list, type) {
      var a
      if (list.length > 0) {
        a = list.find(v => {
          return v.ArmEnum === type
        }) ? list.find(v => {
          return v.ArmEnum === type
        }) : {
          DoctorUser: null
        }
      }
      return a
    },
    save() {
      this.$refs.taskAllocationRuleDataForm.validate(valid => {
        if (!valid) return
        this.loading = true
        var params = this.list.map(v => {
          return {
            Id: v.Id,
            SubjectId: this.data.SubjectId,
            DoctorUserId: v.DoctorUserId,
            ArmEnum: v.ArmEnum,
            IsCancelAssign: v.IsCancelAssign
          }
        })
        cancelSubjectAssignedDoctor({
          CancelList: params,
          Note: this.form.Note,
          TrialReadingCriterionId: this.TrialReadingCriterionId,
          TrialId: this.TrialId
        }).then(() => {
          this.loading = false
          // '保存成功'
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.$emit('getList')
          this.close()
        }).catch(() => {
          this.loading = false
        })
      })
    },
    close() { this.$emit('close') }
  }
}
</script>

<style scoped>

</style>
