<template>
  <el-form
    ref="taskAllocationRuleDataForm"
    v-loading="loading"
    :model="form"
    size="small"
  >
  <div>
<!--    <div style="line-height: 32px;margin-bottom: 10px;">-->
<!--      &lt;!&ndash; 当前项目阅片模式为： &ndash;&gt;-->
<!--      {{$t('trials:reviewAssign:assignReader:title:readingModel')}}-->
<!--      <el-tag type="primary">{{ $fd('ReadingMethod', readingType) }}</el-tag>-->
<!--    </div>-->
    <div v-if="readingType === 1" style="line-height: 32px;margin-bottom: 10px;">
      <!-- 阅片人（R1）： -->
      阅片人
      <el-select v-model="form.R0" v-if="SubjectAssignSelectList.length === 1" :disabled="!!q.find(v => v.ArmEnum === 0) && q.length !== 0" clearable @change="$forceUpdate()" size="mini">
        <el-option v-for="item of DoctorSelectList.R1" :key="`R1${item.DoctorUserId}`" v-if="form.O1 !== item.DoctorUserId && form.AD !== item.DoctorUserId && form.R2 !== item.DoctorUserId" :label="`${item.UserName}(${item.FullName})`" :value="item.DoctorUserId" />
      </el-select>
      <el-select v-model="form.R0" v-else clearable @change="$forceUpdate()" :disabled="!!q.find(v => v.ArmEnum === 0)" size="mini">
        <el-option v-for="item of DoctorSelectList.R1" :key="`R1${item.DoctorUserId}`" v-if="form.O1 !== item.DoctorUserId && form.AD !== item.DoctorUserId && form.R2 !== item.DoctorUserId && !q.find(v => v.DoctorUser.UserId === item.DoctorUserId) || !!q.find(v => v.ArmEnum === 0)" :label="`${item.UserName}(${item.FullName})`" :value="item.DoctorUserId" />
      </el-select>
    </div>
    <div v-if="readingType === 2" style="line-height: 32px;margin-bottom: 10px;">
      <!-- 阅片人（R1）： -->
      阅片人
      <el-select v-model="form.R1" clearable @change="$forceUpdate()" :disabled="!!q.find(v => v.ArmEnum === 1)" size="mini">
        <el-option v-for="item of DoctorSelectList.R1" :key="`R1${item.DoctorUserId}`" v-if="form.O1 !== item.DoctorUserId && form.AD !== item.DoctorUserId && form.R2 !== item.DoctorUserId && !q.find(v => v.DoctorUser.UserId === item.DoctorUserId) || !!q.find(v => v.ArmEnum === 1)" :label="`${item.UserName}(${item.FullName})`" :value="item.DoctorUserId" />
      </el-select>
    </div>
    <div v-if="readingType === 2" style="line-height: 32px;margin-bottom: 10px;">
      <!-- 阅片人（R2）： -->
      {{$t('trials:reviewAssign:assignReader:title:r2')}}
      <el-select v-model="form.R2" clearable @change="$forceUpdate()" :disabled="!!q.find(v => v.ArmEnum === 2)" size="mini">
        <el-option v-for="item of DoctorSelectList.R2" :key="`R2${item.DoctorUserId}`" v-if="form.O1 !== item.DoctorUserId && form.R1 !== item.DoctorUserId && form.AD !== item.DoctorUserId && !q.find(v => v.DoctorUser.UserId === item.DoctorUserId) || !!q.find(v => v.ArmEnum === 2)" :label="`${item.UserName}(${item.FullName})`" :value="item.DoctorUserId" />
      </el-select>
    </div>
    <div style="line-height: 32px;margin-bottom: 10px;"   v-if="OtherInfo && OtherInfo.IsArbitrationReading && readingType === 2">
      <!-- 裁判（AD）： -->
      {{$t('trials:reviewAssign:assignReader:title:ad')}}
      <el-select v-model="form.AD" clearable @change="$forceUpdate()" :disabled="!!q.find(v => v.ArmEnum === 3)" size="mini">
        <el-option v-for="item of DoctorSelectList.AD" :key="`AD${item.DoctorUserId}`" v-if="form.O1 !== item.DoctorUserId && form.R1 !== item.DoctorUserId && form.R2 !== item.DoctorUserId && !q.find(v => v.DoctorUser.UserId === item.DoctorUserId) || !!q.find(v => v.ArmEnum === 3)" :label="`${item.UserName}(${item.FullName})`" :value="item.DoctorUserId" />
      </el-select>
    </div>
    <div style="line-height: 32px;margin-bottom: 10px;"   v-if="OtherInfo && OtherInfo.IsOncologyReading">
      <!-- 肿瘤阅片人（O1）： -->
      {{$t('trials:reviewAssign:assignReader:title:o1')}}
      <el-select v-model="form.O1" clearable @change="$forceUpdate()" :disabled="!!q.find(v => v.ArmEnum === 4)" size="mini">
        <el-option v-for="item of DoctorSelectList.O1" :key="`O1${item.DoctorUserId}`" v-if="form.AD !== item.DoctorUserId && form.R1 !== item.DoctorUserId && form.R2 !== item.DoctorUserId && !q.find(v => v.DoctorUser.UserId === item.DoctorUserId) || !!q.find(v => v.ArmEnum === 4)" :label="`${item.UserName}(${item.FullName})`" :value="item.DoctorUserId" />
      </el-select>
    </div>
  </div>
<!--  <el-table-->
<!--    border-->
<!--    :data="list"-->
<!--    stripe-->
<!--  >-->
<!--  &lt;!&ndash; 阅片人 &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="UserName"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:reader')"-->
<!--      min-width="180"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--      <template slot-scope="scope">-->
<!--        {{ `${scope.row.DoctorUser.UserName}(${scope.row.DoctorUser.FullName})` }}-->
<!--      </template>-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 计划受试者数量 &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="PlanSubjectCount"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:plannedSubjectCount')"-->
<!--      min-width="130"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 已分配受试者数量 &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="AssignedSubjectCount"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:assignedSubjectCount')"-->
<!--      min-width="140"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 阅片类型 &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="UserName"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:readingType')"-->
<!--      min-width="260"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--      <template slot-scope="scope">-->
<!--    <span v-for="item of scope.row.ReadingCategoryList">-->
<!--      <el-tag v-if="item === 1" type="primary">{{ $fd('ReadingCategory', item) + '&' + $fd('ReadingCategory', 2) }}</el-tag>-->
<!--      <el-tag v-if="item === 4" type="danger">{{ $fd('ReadingCategory', item) }}</el-tag>-->
<!--      <el-tag v-if="item === 5" type="warning">{{ $fd('ReadingCategory', item) }}</el-tag>-->
<!--    </span>-->
<!--      </template>-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 待处理任务量（全平台） &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="WaitDealAllTaskCount"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:pengdingTaskCount')"-->
<!--      min-width="180"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 待处理任务量（项目） &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="WaitDealTrialTaskCount"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:pengdingTrialsTaskCount')"-->
<!--      min-width="180"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 访视平均阅片时间（去除离群值后的） &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop=""-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:averageReadingTime')"-->
<!--      min-width="260"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 状态 &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="IsEnable"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:status')"-->
<!--      min-width="100"-->
<!--      show-overflow-tooltip-->
<!--    >-->
<!--      <template slot-scope="scope">-->
<!--        <el-tag v-if="!scope.row.IsEnable" type="danger">{{ $fd('IsEnable', scope.row.IsEnable) }}</el-tag>-->
<!--        <el-tag v-else>{{ $fd('IsEnable', scope.row.IsEnable) }}</el-tag>-->
<!--      </template>-->
<!--    </el-table-column>-->
<!--    &lt;!&ndash; 备注 &ndash;&gt;-->
<!--    <el-table-column-->
<!--      prop="Note"-->
<!--      :label="$t('trials:reviewAssign:assignReader:table:remark')"-->
<!--      min-width="100"-->
<!--      show-overflow-tooltip-->
<!--    />-->
<!--  </el-table>-->
    <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:close')}}
        </el-button>
        <el-button size="small" type="primary" :loading="loading" :disabled="!!q.find(v => v.ArmEnum === 0)" style="margin-top: 10px;" @click="save">
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>

<script>
import { getSubjectAssignAndTaskStatList, batchAssignDoctorToSubject, getDoctorSubjectStat, getDoctorSelectList} from '@/api/trials/reading'

export default {
  name: 'ManualAllocationTable',
  props: {
    TrialReadingCriterionId: {
      type: String,
      default() {
        return []
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    SubjectAssignSelectList: {
      type: Array,
      default() {
        return []
      }
    },
    // readingType: {
    //   type: Number,
    //   default() {
    //     return 0
    //   }
    // }
  },
  data() {
    return {
      TrialId: this.$route.query.trialId,
      form: {
        R0: null,
        R1: null,
        R2: null,
        AD: null,
        O1: null
      },
      DoctorSelectList: {
        R1: [],
        R2: [],
        AD: [],
        O1: [],
      },
      list: [],
      loading: false,
      btnLoading: false,
      q: [],
      OtherInfo: null,
      readingType: null,
      isShowInfo: {}
    }
  },
  mounted() {
    this.TrialId = this.$route.query.trialId
    this.getDoctorSubjectStat()
    var arr = []
    arr.push(this.getDoctorSelectList(1))
    arr.push(this.getDoctorSelectList(2))
    arr.push(this.getDoctorSelectList(4))
    arr.push(this.getDoctorSelectList(5))
    var q = []
    if (Object.keys(this.data).length && this.data.SubjectId) {
    } else {
      this.SubjectAssignSelectList.forEach(v => {
        v.NowDoctorUserList.forEach(v1 => {
          q.push(v1)
        })
        this.q = q
      })
    }
    console.log(q)
    Promise.all(arr).then(() => {
      if (Object.keys(this.data).length && this.data.SubjectId) {
        this.form.R0 = this.findDoctorUser(this.data.NowDoctorUserList, 0).DoctorUser ? this.findDoctorUser(this.data.NowDoctorUserList, 0).DoctorUser.UserId : null
        this.form.R1 = this.findDoctorUser(this.data.NowDoctorUserList, 1).DoctorUser ? this.findDoctorUser(this.data.NowDoctorUserList, 1).DoctorUser.UserId : null
        this.form.R2 = this.findDoctorUser(this.data.NowDoctorUserList, 2).DoctorUser ? this.findDoctorUser(this.data.NowDoctorUserList, 2).DoctorUser.UserId : null
        this.form.AD = this.findDoctorUser(this.data.NowDoctorUserList, 3).DoctorUser ? this.findDoctorUser(this.data.NowDoctorUserList, 3).DoctorUser.UserId : null
        this.form.O1 = this.findDoctorUser(this.data.NowDoctorUserList, 4).DoctorUser ? this.findDoctorUser(this.data.NowDoctorUserList, 4).DoctorUser.UserId : null
        this.data.NowDoctorUserList.forEach(v1 => {
          q.push(v1)
        })
        this.q = q
      } else {
        var userList = {
          R0: [],
          R1: [],
          R2: [],
          AD: [],
          O1: []
        }
        this.SubjectAssignSelectList.forEach(v1 => {
          v1.NowDoctorUserList.forEach(v => {
            if (v.ArmEnum === 0) {
              userList.R0.push(v)
            }
            if (v.ArmEnum === 1) {
              userList.R1.push(v)
            }
            if (v.ArmEnum === 2) {
              userList.R2.push(v)
            }
            if (v.ArmEnum === 3) {
              userList.AD.push(v)
            }
            if (v.ArmEnum === 4) {
              userList.O1.push(v)
            }
          })
        })
        var isShowInfo = {}
        for (var key in userList) {
          var itemList = userList[key]
          var id
          isShowInfo[key] = this.checkArrayObjectValueRepeat(itemList, 'DoctorUserId')
        }
        this.isShowInfo = isShowInfo
        if (!isShowInfo.R0) {
          this.form.R0 = this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 0).DoctorUser ? this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 0).DoctorUser.UserId : null
        }
        if (!isShowInfo.R1) {
          this.form.R1 = this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 1).DoctorUser ? this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 1).DoctorUser.UserId : null
        }
        if (!isShowInfo.R2) {
          this.form.R2 = this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 2).DoctorUser ? this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 2).DoctorUser.UserId : null
        }
        if (!isShowInfo.AD) {
          this.form.AD = this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 3).DoctorUser ? this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 3).DoctorUser.UserId : null
        }
        if (!isShowInfo.O1) {
          this.form.O1 = this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 4).DoctorUser ? this.findDoctorUser(this.SubjectAssignSelectList[0].NowDoctorUserList, 4).DoctorUser.UserId : null
        }
      }
    })
  },
  methods: {
    checkArrayObjectValueRepeat(dataArr, key) {
      console.log(dataArr)
      var a = null
      var b = false
      console.log(this.SubjectAssignSelectList.length)
      if (this.SubjectAssignSelectList.length === 1) {
        return false
      }
      dataArr.forEach((v,i) => {
        if(i === 0){
          a = v[key]
        }
        if (v[key] !== a) {
          b = true
        }
        if (dataArr.length === 1) {
          b = true
        }
      })
      return b
    },
    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
    },
    getDoctorSelectList(ReadingCategory) {
      return new Promise(resolve => {
        getDoctorSelectList({
          TrialId: this.$route.query.trialId,
          ReadingCategory: ReadingCategory,
          TrialReadingCriterionId: this.TrialReadingCriterionId
        }).then(res => {
          switch (ReadingCategory) {
            case 1:
              this.$set(this.DoctorSelectList, 'R1', res.Result)
              break;
            case 2:
              this.$set(this.DoctorSelectList, 'R2', res.Result)
              break;
            case 4:
              this.$set(this.DoctorSelectList, 'AD', res.Result)
              break;
            case 5:
              this.$set(this.DoctorSelectList, 'O1', res.Result)
              break;
          }
          resolve()
        })
      })
    },
    getDoctorSubjectStat() {
      this.loading = true
      getDoctorSubjectStat(this.TrialId, this.TrialReadingCriterionId).then(res => {
        this.readingType = res.OtherInfo.ReadingType
        this.list = res.Result
        this.OtherInfo = res.OtherInfo
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    save() {
      var DoctorArmList = []
      console.log(this.form)
      if (this.form.R0) {
        DoctorArmList.push({
          ArmEnum: 0,
          DoctorUserId: this.form.R0
        })
      }
      if (this.form.R1) {
        DoctorArmList.push({
          ArmEnum: 1,
          DoctorUserId: this.form.R1
        })
      }
      if (this.form.R2) {
        DoctorArmList.push({
          ArmEnum: 2,
          DoctorUserId: this.form.R2
        })
      }
      if (this.form.AD) {
        DoctorArmList.push({
          ArmEnum: 3,
          DoctorUserId: this.form.AD
        })
      }
      if (this.form.O1) {
        DoctorArmList.push({
          ArmEnum: 4,
          DoctorUserId: this.form.O1
        })
      }
      var params = {
        TrialId: this.TrialId,
        TrialReadingCriterionId: this.TrialReadingCriterionId,
        SubjectIdList: this.SubjectAssignSelectList.map(v => v.SubjectId),
        DoctorArmList: DoctorArmList
      }
      this.loading = true
      batchAssignDoctorToSubject(params).then(res => {
        this.$message.success(this.$t('common:message:savedSuccessfully'))
        this.loading = false
        this.$emit('getList')
        this.close()
      }).catch(() => {
        this.loading = false
      })
    },
    close() { this.$emit('close') }
  }
}
</script>

<style scoped>

</style>
