/* eslint-disable */
<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 中心编号 -->
        <el-form-item :label="$t('trials:pmMedicalReview:select:siteCode')">
          <el-select v-model="searchData.SiteId" clearable filterable style="width:120px;">
            <el-option
              v-for="(item,index) of siteOptions"
              :key="index"
              :label="item.TrialSiteCode"
              :value="item.SiteId"
            />
          </el-select>
        </el-form-item>
        <!-- 受试者编号 -->
        <el-form-item :label="$t('trials:pmMedicalReview:select:subjectCode')">
          <el-input v-model="searchData.SubjectCode" style="width:100px;" />
        </el-form-item>
        <!-- 任务名称 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:pmMedicalReview:select:taskName')">
          <el-input
            v-model="searchData.TaskName"
            style="width:100px;"
            clearable
          />
        </el-form-item>
        <!-- 阅片人 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:pmMedicalReview:select:reader')">
          <el-select v-model="searchData.DoctorUserId" clearable style="width:120px;">
            <el-option v-for="item of DoctorUserList" :key="'DoctorUserId' + item.DoctorUserId" :value="item.DoctorUserId" :label="item.UserName" />
          </el-select>
        </el-form-item>
        <!-- 阅片标准 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:pmMedicalReview:select:criterionName')">
          <el-select v-model="searchData.TrialReadingCriterionId" clearable style="width:120px;">
            <el-option v-for="item of trialCriterionList" :key="'TrialReadingCriterionId' + item.TrialReadingCriterionId" :value="item.TrialReadingCriterionId" :label="item.TrialReadingCriterionName" />
          </el-select>
        </el-form-item>
        <!-- 是否加急 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:pmMedicalReview:select:isUrgent')">
          <el-select v-model="searchData.IsUrgent" clearable style="width:120px;">
            <el-option v-for="item of $d.YesOrNo" :key="'IsUrgent' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 任务状态 -->
        <!-- <el-form-item style="margin-bottom:10px" label="任务状态">
          <el-select v-model="searchData.TaskState" clearable style="width:120px;">
            <el-option v-for="item of $d.TaskState" :key="'TaskState' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item> -->
        <!-- 任务类型 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:pmMedicalReview:select:taskType')">
          <el-select v-model="searchData.ReadingCategory" clearable style="width:120px;">
            <el-option v-for="item of $d.ReadingCategory" :key="'ReadingCategory' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 是否产生裁判 -->
        <el-form-item style="margin-bottom:10px" :label="$t('trials:pmMedicalReview:select:isGeneratedJudge')">
          <el-select v-model="searchData.IsGeneratedJudge" clearable style="width:120px;">
            <el-option v-for="item of $d.YesOrNo" :key="'IsGeneratedJudge' + item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
<!--        &lt;!&ndash; 全局阅片是否有更新 &ndash;&gt;-->
<!--        <el-form-item style="margin-bottom:10px" :label="$t('trials:pmMedicalReview:select:globalReadingChanged')">-->
<!--          <el-select v-model="searchData.IsGeneratedJudge" clearable style="width:120px;">-->
<!--            <el-option v-for="item of $d.YesOrNo" :key="'ReadingCategory' + item.label" :value="item.value" :label="item.label" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item style="margin-bottom:10px">
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <el-button type="primary" icon="el-icon-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
          <!-- 批量分配 -->
          <el-button type="primary" @click="openAssignMedicalReviewTask">
            {{$t('trials:pmMedicalReview:select:batchAllocation')}}
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <template slot="main-container">
      <el-table
        v-adaptive="{bottomOffset:60}"
        v-loading="loading"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortChange"
        @selection-change="handleSelectChange"
      >
        <el-table-column :selectable="selectable" type="selection" align="left" width="45" />
        <!-- 是否加急 -->
        <el-table-column
          prop="IsUrgent"
          :label="$t('trials:pmMedicalReview:select:isUrgent')"
          show-overflow-tooltip
          width="120"
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsUrgent" type="danger">{{ $fd('YesOrNo', scope.row.IsUrgent) }}</el-tag>
            <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsUrgent) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 任务编号 -->
        <el-table-column
          prop="TaskCode"
          :label="$t('trials:pmMedicalReview:select:taskCode')"
          width="120"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 中心编号 -->
        <el-table-column
          prop="TrialSiteCode"
          :label="$t('trials:pmMedicalReview:select:siteCode')"
          width="100"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 受试者编号 -->
        <el-table-column
          prop="SubjectCode"
          :label="$t('trials:pmMedicalReview:select:subjectCode')"
          width="120"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 第二编号 -->
        <el-table-column
          prop="SubjectCode"
          :label="$t('trials:pmMedicalReview:select:subjectSecondCode')"
          width="120"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 任务名称 -->
        <el-table-column
          prop="VisitTaskNum"
          :label="$t('trials:pmMedicalReview:select:taskName')"
          width="140"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{scope.row.TaskName}}
          </template>
        </el-table-column>
        <!-- 盲态任务标识 -->
        <el-table-column
          prop="TaskBlindName"
          :label="$t('trials:pmMedicalReview:select:taskBlindName')"
          width="160"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 任务类型 -->
        <el-table-column
          prop="ReadingCategory"
          :label="$t('trials:pmMedicalReview:select:taskType')"
          width="140"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.ReadingCategory === 1" type="primary">{{ $fd('ReadingCategory', scope.row.ReadingCategory) }}</el-tag>
            <el-tag v-if="scope.row.ReadingCategory === 2" type="info">{{ $fd('ReadingCategory', scope.row.ReadingCategory) }}</el-tag>
            <el-tag v-if="scope.row.ReadingCategory === 4" type="danger">{{ $fd('ReadingCategory', scope.row.ReadingCategory) }}</el-tag>
            <el-tag v-if="scope.row.ReadingCategory === 5" type="warning">{{ $fd('ReadingCategory', scope.row.ReadingCategory) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 角色 -->
        <el-table-column
          prop="ArmEnum"
          :label="$t('trials:pmMedicalReview:select:role')"
          width="80"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.ArmEnum === 0" type="primary">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 1" type="primary">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 2" type="success">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 3" type="info">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 4" type="danger">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
            <el-tag v-if="scope.row.ArmEnum === 5" type="warning">{{ $fd('ArmEnum', scope.row.ArmEnum) }}</el-tag>
          </template>
        </el-table-column>
        <!-- 阅片人 -->
        <el-table-column
          prop="UserName"
          :label="$t('trials:pmMedicalReview:select:reader')"
          width="120"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.DoctorUser.UserName }}({{ scope.row.DoctorUser.FullName }})
          </template>
        </el-table-column>
        <!-- 阅片标准 -->
        <el-table-column
          prop="TrialReadingCriterionName"
          :label="$t('trials:pmMedicalReview:select:criterionName')"
          width="180"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 阅片完成时间 -->
        <el-table-column
          prop="SignTime"
          :label="$t('trials:pmMedicalReview:select:signTime')"
          width="200"
          sortable="custom"
          show-overflow-tooltip
        />
        <!-- 阅片时长 -->
        <el-table-column
          prop="ReadingDurationTimeSpan"
          :label="$t('trials:pmMedicalReview:select:readingDurationTime')"
          width="140"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{scope.row.ReadingDuration}}
          </template>
        </el-table-column>
        <!-- 是否产生裁判 -->
        <el-table-column
          prop="IsGeneratedJudge"
          :label="$t('trials:pmMedicalReview:select:isGeneratedJudge')"
          width="140"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <div>
              <el-tag v-if="!scope.row.IsGeneratedJudge" type="danger">{{ $fd('YesOrNo', scope.row.IsGeneratedJudge) }}</el-tag>
              <el-tag v-else type="primary">{{ $fd('YesOrNo', scope.row.IsGeneratedJudge) }}</el-tag>
            </div>
          </template>
        </el-table-column>
<!--        &lt;!&ndash; 全局阅片是否有校正 &ndash;&gt;-->
<!--        <el-table-column-->
<!--          prop="IsGlobalHaveUpdate"-->
<!--          :label="$t('trials:pmMedicalReview:select:globalReadingChanged')"-->
<!--          width="180"-->
<!--          sortable="custom"-->
<!--          show-overflow-tooltip-->
<!--        >-->
<!--        <template slot-scope="scope">-->
<!--            <div>-->
<!--              <el-tag v-if="scope.row.IsGlobalHaveUpdate" type="primary">{{ $fd('YesOrNo', scope.row.IsGlobalHaveUpdate) }}</el-tag>-->
<!--            </div>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <!-- 审核次数 -->
        <el-table-column
          prop="GeneratedMedicalReviewCount"
          :label="$t('trials:pmMedicalReview:select:auditCount')"
          width="150"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          fixed="right"
          :label="$t('common:action:action')"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              icon="el-icon-thumb"
              circle
              :title="$t('trials:pmMedicalReview:select:allocation')"
              @click="assignMedicalReviewTask(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
      <!-- 分配医学经理 -->
      <el-dialog
        v-if="AssignMedicalReviewTaskVisible"
        :title="$t('trials:pmMedicalReview:allocation:aliocationMIM')"
        :visible.sync="AssignMedicalReviewTaskVisible"
        width="500px"
        append-to-body
        :close-on-click-modal="false"
      >
        <AssignMedicalReviewTask :data="rowData" :select-list="SelectList" :doctor-user-list="MIMOptions" @close="() => {AssignMedicalReviewTaskVisible = false}" @getList="getList" />
      </el-dialog>
    </template>
  </BaseContainer>
</template>
<script>
import { getTrialSiteSelect } from '@/api/trials'
import { getMIMUserList, getGenerateMedicalReviewTaskList, getDoctorUserSelectList } from '@/api/trials/reading'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import AssignMedicalReviewTask from './AssignMedicalReviewTask'

const searchDataDefault = () => {
  return {
    PageIndex: 1,
    PageSize: 20,
    Asc: true,
    SortField: '',
    TrialId: null,
    SiteId: null,
    SubjectId: null,
    SubjectCode: null,
    IsUrgent: null,
    TaskName: null,
    TaskState: null,
    DoctorUserId: null,
    ReadingCategory: null,
    TaskAllocationState: null,
    BeginAllocateDate: null,
    EndAllocateDate: null,
    RootReReadingTaskId: null,
    IsGeneratedJudge: null
  }
}
export default {
  name: 'TrialsNotice',
  props: {
    trialCriterionList: {
      type: Array,
      default() {
        return []
      }
    }
  },
  components: { BaseContainer, Pagination, AssignMedicalReviewTask },
  data() {
    return {
      MIMOptions: [],
      searchData: searchDataDefault(),
      maxLength: 0,
      list: [],
      siteOptions: [],
      total: 0,
      SubjectAssignListTotal: 0,
      loading: false,
      AssignMedicalReviewTaskVisible: false,
      AllocationConfigVisible: false,
      ConfirmReReadingForm: {
        RequestReReadingRejectReason: null
      },
      rules: {
        RequestReReadingRejectReason: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' }]
      },
      ConfirmReReadingVisible: false,
      taskAllocationRuleVisible: false,
      AutoAlloctionVisible: false,
      manualAllocationVisible: false,
      trialId: this.$route.query.trialId,
      currentData: {},
      param: {},
      DoctorUserList: [],
      TaskAllocationRuleList: [],
      btnLoading: false,
      value: true,
      value1: true,
      SelectList: [],
      TrialTaskInfo: {
        TrialId: null,
        TaskAllocateObjEnum: 0,
        FollowVisitAutoAssignDefaultState: 0,
        FollowGlobalVisitAutoAssignDefaultState: 0,
        TaskAllocateDefaultState: 1
      },
      title: '',
      rowData: {},
      timeList: [],
      SubjectAssignList: [],
      SubjectAssignSelectList: [],
      TaskAllocationRuleSelectList: [],
      readVisible: false,
      TaskOptType: null,
      OtherInfo: null,
      ReadingType: null
    }
  },
  mounted() {
    this.getList()
    this.getSite()
    this.getDoctorUserSelectList()
    this.getMIMUserList()
  },
  methods: {
    getMIMUserList() {
      getMIMUserList(this.trialId).then(res => {
        this.MIMOptions = res.Result
      })
    },
    openAssignMedicalReviewTask() {
      this.rowData = {}
      if (this.SelectList.length === 0) {
        // '请先选择需要分配的医学审核任务'
        this.$alert(this.$t('trials:pmMedicalReview:allocation:msg1'))
        return
      }
      this.AssignMedicalReviewTaskVisible = true
    },
    handleSelectChange(e) {
      this.SelectList = e
    },
    assignMedicalReviewTask(row, type) {
      this.rowData = {...row}
      this.AssignMedicalReviewTaskVisible = true
    },
    changeTimeList() {
      if (this.timeList) {
        this.searchData.BeginAllocateDate = this.timeList[0]
        this.searchData.EndAllocateDate = this.timeList[1]
      } else {
        this.searchData.BeginAllocateDate = null
        this.searchData.EndAllocateDate = null
      }
    },
    // 申请重阅
    selectable(row) {
      var num
      if (this.ReadingType === 2) {
        num = 2
      } else {
        num = 1
      }
      if (this.TaskAllocationRuleSelectList.length === num && !this.TaskAllocationRuleSelectList.find(v => { return v.UserName === row.UserName })) {
        return false
      } else {
        return true
      }
    },
    getDoctorUserSelectList() {
      getDoctorUserSelectList(this.$route.query.trialId).then(res => {
        this.DoctorUserList = res.Result
        if (this.DoctorUserList.length > 0) {
          this.ReadingType = this.DoctorUserList[0].ReadingType
        } else {
        }
      })
    },
    getList() {
      this.searchData.TrialId = this.$route.query.trialId
      this.loading = true
      getGenerateMedicalReviewTaskList(this.searchData).then(res => {
        this.loading = false
        this.OtherInfo = res.OtherInfo
        this.$emit('getList')
        this.AssignMedicalReviewTaskVisible = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    getSite() {
      getTrialSiteSelect(this.trialId).then(res => {
        this.siteOptions = res.Result
      })
    },
    // 排序
    handleSortChange(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/ .hidden-row{
  display: none;
}
/deep/ .el-dialog__body {
  padding: 15px 20px;
}

/deep/ .el-tag--danger.el-tag--dark {
  // background-color: #f56c6c!important;
  border-color: none!important;
  // color: #fff!important;
}
/deep/ #TaskAllocationRuleList thead .el-checkbox__inner{
  display: none;
}
/deep/ .el-descriptions-item__label.has-colon:after{
  display: none;
}
</style>
