<template>
  <el-form
    ref="selectSubjectForm"
    size="small"
    label-width="130px"
    :inline="true"
  >
    <div class="base-dialog-body">
      <el-divider content-position="left">{{$t('trials:readingPeriod:title:screeningRule')}}</el-divider>
      <el-form-item
        :label="$t('trials:readingPeriod:label:IsJoinEvaluation')"
        prop="IsJoinEvaluation"
      >
        {{$t('trials:readingPeriod:value:IsJoinEvaluation')}}
      </el-form-item>
      <el-divider content-position="left">{{ $t('trials:readingPeriod:title:visitsList') }}</el-divider>
      <el-table
        ref="subjectList"
        v-loading="btnLoading"
        :data="list"
        stripe
      >
        <el-table-column
          prop="VisitName"
          :label="$t('trials:trials-panel:table:VisitName')"
          sortable="custom"
          show-overflow-tooltip
          width="120"
        />
        <el-table-column
          prop="EarliestScanDate"
          :label="$t('trials:trials-panel:table:EarliestScanDate')"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.EarliestScanDate ? moment(scope.row.EarliestScanDate).format('YYYY-MM-DD') : '' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="LatestScanDate"
          :label="$t('trials:trials-panel:table:LatestScanDate')"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.LatestScanDate ? moment(scope.row.LatestScanDate).format('YYYY-MM-DD') : '' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="ImageFilterState"
          :label="$t('trials:trials-panel:table:ImageFilterState')"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('ImageFilterState', scope.row.ImageFilterState) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('common:action:action')"
          width="120"
          fixed="right"
        >
          <template slot-scope="scope">
            <!-- 影像筛选 -->
            <el-button
              circle
              :title="$t('trials:subject:table:ImageSelect')"
              icon="el-icon-picture-outline"
              @click="handleImageScreening(scope.row)"
            />
            <!-- 生成任务 -->
            <el-button
              circle
              :disabled="scope.row.ImageFilterState !== 2"
              :title="$t('trials:subject:action:handleCreateTask')"
              icon="el-icon-success"
              @click="handleCreateTask(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-divider content-position="left">{{ $t('trials:readingPeriod:title:taskList') }}</el-divider>
      <el-table
        ref="subjectList"
        v-loading="btnLoading"
        :data="list2"
        stripe
      >
        <el-table-column
          prop="VisitName"
          :label="$t('trials:trials-panel:table:VisitName')"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          prop="UpdateTime"
          :label="$t('trials:trials-panel:table:UpdateTime')"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('common:action:action')"
          width="180"
        >
          <template slot-scope="scope">
            <!-- 影像筛选 -->
            <el-button
              circle
              :title="$t('trials:subject:action:ImageSelect')"
              icon="el-icon-picture-outline"
              @click="handleImageScreening(scope.row)"
            />
            <el-button
              circle
              :title="$t('trials:subject:action:openReReadingOrBackList')"
              icon="el-icon-back"
              @click="openReReadingOrBackList(scope.row)"
            />
            <el-button
              circle
              :title="$t('trials:subject:action:handleOpenTaskAllocation')"
              icon="el-icon-document"
              @click="handleOpenTaskAllocation(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <!-- 取消 -->
        <el-button
          :disabled="btnLoading"
          size="small"
          type="primary"
          @click="close"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
      </el-form-item>
    </div>
    <el-dialog
      v-if="selectSubjectImage.visible"
      :title="selectSubjectImage.title"
      :visible.sync="selectSubjectImage.visible"
      :fullscreen="true"
      :append-to-body="true"
    >
      <subject-study :data="rowData" @close="selectSubjectImage.visible = false" @getList="getList"></subject-study>
    </el-dialog>
    <el-dialog
      v-if="taskAllocationVisible.visible"
      :title="taskAllocationVisible.title"
      :visible.sync="taskAllocationVisible.visible"
      :fullscreen="true"
      :append-to-body="true"
    >
      <task-allacation :data="rowData" @close="taskAllocationVisible.visible = false"></task-allacation>
    </el-dialog>
    <el-dialog
      v-if="ReReadingOrBackVisible"
      :title="$t('trials:reviewTrack:dialog:backImpactList')"
      :visible.sync="ReReadingOrBackVisible"
      width="1460px"
      append-to-body
      :close-on-click-modal="false"
      custom-class="base-dialog-wrapper"
    >
      <div class="base-dialog-body">
        <el-table
          v-loading="btnLoading"
          :data="InfluenceTaskList"
          stripe
          height="100"
          style="min-height: 400px;"
        >
          <!-- 任务编号 -->
          <el-table-column
            prop="TaskCode"
            :label="$t('trials:reviewTrack:table:taskCode')"
            min-width="100"
            show-overflow-tooltip
          />
          <!-- 中心编号 -->
          <el-table-column
            prop="TrialSiteCode"
            :label="$t('trials:reviewTrack:table:siteCode')"
            min-width="100"
            show-overflow-tooltip
          />
          <!-- 受试者编号 -->
          <el-table-column
            prop="SubjectCode"
            :label="$t('trials:reviewTrack:table:subjectCode')"
            min-width="120"
            show-overflow-tooltip
          />
          <!-- 任务名称 -->
          <el-table-column
            prop="VisitTaskNum"
            :label="$t('trials:reviewTrack:table:taskName')"
            min-width="120"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{scope.row.TaskName}}
            </template>
          </el-table-column>
          <!-- 盲态任务标识 -->
          <el-table-column
            prop="TaskBlindName"
            :label="$t('trials:reviewTrack:table:blindName')"
            min-width="120"
            show-overflow-tooltip
          />
          <!-- 阅片标准 -->
          <el-table-column
            prop="TrialReadingCriterionName"
            :label="$t('trials:reviewTrack:table:criterionName')"
            min-width="100"
            show-overflow-tooltip
          >
          </el-table-column>
          <!-- 任务状态 -->
          <el-table-column
            prop="TaskState"
            :label="$t('trials:reviewTrack:table:taskStatus')"
            min-width="100"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.TaskState === 0" type="primary">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
              <el-tag v-if="scope.row.TaskState === 1" type="info">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
              <el-tag v-if="scope.row.TaskState === 3" type="danger">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
              <el-tag v-if="scope.row.TaskState === 4" type="warning">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
              <el-tag v-if="scope.row.TaskState === 5" type="danger">{{ $fd('TaskState', scope.row.TaskState) }}</el-tag>
            </template>
          </el-table-column>
          <!-- 任务类型 -->
          <el-table-column
            prop="ReadingCategory"
            min-width="100"
            :label="$t('trials:reviewTrack:table:taskType')"
            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="ReadingTaskState"
            :label="$t('trials:reviewTrack:table:readingStatus')"
            min-width="100"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.ReadingTaskState === 2" type="primary">{{ $fd('ReadingTaskState', scope.row.ReadingTaskState) }}</el-tag>
              <el-tag v-else type="danger">{{ $fd('ReadingTaskState', scope.row.ReadingTaskState) }}</el-tag>
            </template>
          </el-table-column>
          <!-- 生效后影响 -->
          <el-table-column
            prop="OptType"
            :label="$t('trials:reviewTrack:applyReread:title:postEffectiveImpacts')"
            min-width="100"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.OptType === 0" type="primary">{{ $fd('ReReadingOrBackOptType', scope.row.OptType) }}</el-tag>
              <el-tag v-if="scope.row.OptType === 1" type="danger">{{ $fd('ReReadingOrBackOptType', scope.row.OptType) }}</el-tag>
              <el-tag v-if="scope.row.OptType === 2" type="warning">{{ $fd('ReReadingOrBackOptType', scope.row.OptType) }}</el-tag>
            </template>
          </el-table-column>
          <!-- 阅片人 -->
          <el-table-column
            prop="UserName"
            :label="$t('trials:reviewTrack:table:reader')"
            min-width="160"
            show-overflow-tooltip
          >
            <template v-if="scope.row.DoctorUser" slot-scope="scope">
              {{ scope.row.DoctorUser.UserName }}({{ scope.row.DoctorUser.FullName }})
            </template>
          </el-table-column>
        </el-table>
        <div style="font-size: 12px;color: #f66;margin-top: 10px;" v-if="ReReadingOtherInfo && ReReadingOtherInfo.IsIRAppyTaskInfluenced">
          <!-- 注意！列表中存在IR已申请重阅的任务！ -->
          {{$t('trials:reviewTrack:applyReread:title:msg1')}}
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <!-- 取消 -->
        <el-button :disabled="btnLoading" size="small" type="primary" @click="ReReadingOrBackVisible = false">
          {{ $t('common:button:cancel') }}
        </el-button>
        <!-- 确定 -->
        <el-button :loading="btnLoading" size="small" type="primary" @click="PMSetTaskBack">
          {{$t('trials:reviewTrack:impactList:save')}}
        </el-button>
      </div>
    </el-dialog>
  </el-form>
</template>

<script>
import { getCriteriaVisitBackInfluenceTaskList, confirmBackCriteriaVisitTask, getSubjectCriteriaEvaluationVisitFilterList, getHaveGeneratedTaskList, batchGenerateTask, getVisitStudyAndSeriesList, batchAddSubjectCriteriaEvaluationVisitStudyFilter } from '@/api/trials/subject'
import subjectStudy from "./subjectStudy";
import moment from "moment";
import taskAllacation from './taskAllocation'
export default {
  name: 'selectSubjectImage',
  components: { subjectStudy, taskAllacation },
  props: {
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      trialId: this.$route.query.trialId,
      btnLoading: false,
      list: [],
      list2: [],
      selectSubjectImage: { visible: false, title: this.$t('trials:subject:table:ImageSelect') },
      rowData: null,
      moment,
      taskAllocationVisible:  { visible: false, title: this.$t('trials:readingPeriod:title:taskAllocationVisible') },
      InfluenceTaskList: [],
      ReReadingOrBackVisible: false,
      ReReadingOtherInfo: {
        IsIRAppyTaskInfluenced: false
      },
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    PMSetTaskBack() {
      this.loading = true
      confirmBackCriteriaVisitTask({
        TrialReadingCriterionId: this.rowData.TrialReadingCriterionId,
        TrialId: this.trialId,
        SubjectId: this.rowData.SubjectId,
        SubjectVisitId: this.rowData.SubjectVisitId
      }).then(res => {
        this.loading = false
        this.ReReadingOrBackVisible = false
        this.getList()
        // '退回成功'
        this.$message.success(this.$t('trials:reviewTrack:message:returnedSuccessfully'))
      }).catch(() => {this.loading = false})
    },
    openReReadingOrBackList(row) {
      this.btnLoading = true
      this.rowData = {...row}
      getCriteriaVisitBackInfluenceTaskList({
        TrialReadingCriterionId: row.TrialReadingCriterionId,
        TrialId: this.trialId,
        SubjectId: row.SubjectId,
        SubjectVisitId: row.SubjectVisitId
      }).then(res => {
        this.InfluenceTaskList = res.Result
        this.ReReadingOtherInfo = res.OtherInfo
        this.ReReadingOrBackVisible = true
        this.btnLoading = false
      }).catch(() => {this.btnLoading = false})
    },
    handleOpenTaskAllocation(row) {
      this.rowData = {...row}
      this.taskAllocationVisible.visible = true
    },
    close() {
      this.$emit('close')
    },
    getHaveGeneratedTaskList() {
      var params = {
        IsGeneratedTask: true,
        TrialReadingCriterionId: this.data.TrialReadingCriterionId,
        SubjectId: this.data.SubjectId
      }
      this.btnLoading = true
      getSubjectCriteriaEvaluationVisitFilterList(params).then(res => {
        this.btnLoading = false
        this.list2 = res.Result
      }).catch(() => {
        this.btnLoading = false
      })
      // getHaveGeneratedTaskList(params).then(res => {
      //   this.list2 = res.Result
      //   this.btnLoading = false
      // }).catch(() => {
      //   this.btnLoading = false
      // })
    },
    handleImageScreening(row) {
      this.rowData = row
      this.selectSubjectImage.visible = true
      this.selectSubjectImage.title = `${this.$t('trials:subject:table:ImageSelect')}(${row.SubjectCode})`
    },
    handleCreateTask(row) {
      var params = {
        TrialReadingCriterionId: row.TrialReadingCriterionId,
        TrialId: this.trialId,
        SubjectId: row.SubjectId,
        SubjectVisitIdList: [row.SubjectVisitId],
      }
      this.btnLoading = true
      batchGenerateTask(params).then(res => {
        this.$message.success(this.$t('trials:readingPeriod:message:batchGenerateTask'))
        this.getList()
        this.btnLoading = false
      }).catch(() => {
        this.btnLoading = false
      })
    },
    getList() {
      var params = {
        IsGeneratedTask: false,
        TrialReadingCriterionId: this.data.TrialReadingCriterionId,
        SubjectId: this.data.SubjectId
      }
      this.btnLoading = true
      getSubjectCriteriaEvaluationVisitFilterList(params).then(res => {
        this.btnLoading = false
        this.list = res.Result
        this.getHaveGeneratedTaskList()
      }).catch(() => {
        this.btnLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
