<template>
  <BaseContainer>
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 受试者编号 -->
        <el-form-item :label="$t('trials:crcQuestion:table:subjectId')">
          <el-input v-model="searchData.SubjectCode" style="width: 140px" />
        </el-form-item>
        <!-- 患者编号 -->
        <!-- <el-form-item :label="$t('trials:uploadDicomList:table:pId')">
          <el-input v-model="searchData.SubjectInfo" style="width: 140px" />
        </el-form-item> -->
        <!-- 患者姓名 -->
        <!-- <el-form-item :label="$t('trials:uploadDicomList:table:patientName')">
          <el-input v-model="searchData.SubjectInfo" style="width: 140px" />
        </el-form-item> -->
        <!-- 访视名称 -->
        <!-- <el-form-item :label="$t('trials:globalReview:table:visitName')">
          <el-select
            v-model="searchData.SiteId"
            clearable
            filterable
            style="width: 150px"
          >
            <el-option
              v-for="item of visitOptions"
              :key="item.SiteId"
              :label="item.TrialSiteCode"
              :value="item.SiteId"
            />
          </el-select>
        </el-form-item> -->
        <!-- 任务状态 -->
        <el-form-item :label="$t('trials:readTask:table:taskState')">
          <el-select
            v-model="searchData.taskState"
            clearable
            filterable
            style="width: 150px"
          >
            <el-option
              v-for="item of $d.TaskState"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- 阅片状态 -->
        <el-form-item :label="$t('trials:readTask:table:readingTaskState')">
          <el-select
            v-model="searchData.ReadingTaskState"
            clearable
            filterable
            style="width: 150px"
          >
            <el-option
              v-for="item of $d.ReadModuleEnum"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- 阅片完成时间 -->
        <el-form-item :label="$t('trials:reviewTrack:table:signTime')">
          <el-date-picker
            v-model="dateValue"
            type="datetimerange"
            range-separator="-" 
            :start-placeholder="$t('trials:uploadClinicalData:table:beginDate')"
            :end-placeholder="$t('trials:uploadClinicalData:table:endDate')"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <!-- 查询 -->
          <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-form-item>
      </el-form>
    </template>

    <!-- 阅片管理列表 -->
    <template slot="main-container">
      <el-table
        v-loading="loading"
        v-adaptive="{ bottomOffset: 60 }"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
        ref="redManagenentTable"
        :default-sort="{ prop: 'CreateTime', order: 'descending' }"
      >
        <el-table-column type="index" width="40" />
        <!-- 受试者编号 -->
        <el-table-column
          prop="SubjectCode"
          :label="$t('trials:uploadMonitor:table:subjectId')"
          show-overflow-tooltip
        />
        <!-- 患者编号 -->
        <el-table-column
          prop="StudyCode"
          :label="$t('trials:uploadDicomList:table:pId')"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span
              v-for="(item, index) in scope.row.PatientList"
              :key="`${index}${item.PatientId}`"
            >
              {{
                index === scope.row.PatientList.length - 1
                  ? item.PatientIdStr
                  : `${item.PatientIdStr}, `
              }}
            </span>
          </template></el-table-column
        >
        <!-- 患者姓名 -->
        <el-table-column
          prop="StudyCode"
          :label="$t('trials:researchStaff:table:Name')"
          show-overflow-tooltip
        />
        <!-- 任务名称 -->
        <el-table-column
          prop="TaskBlindName"
          :label="$t('trials:auditRecord:table:taskName')"
          show-overflow-tooltip
        />
        <!-- 任务创建时间 -->
        <el-table-column
          prop="CreateTime"
          :label="$t('trials:reviewTrack:table:createTime')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 任务状态 -->
        <el-table-column
          prop="TaskState"
          :label="$t('trials:readTask:table:taskState')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 阅片人 -->
        <el-table-column
          prop="StudyCode"
          :label="$t('trials:reviewAssign:searchForm:reader')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 阅片标准 -->
        <el-table-column
          prop="TrialReadingCriterionName"
          :label="$t('trials:processCfg:form:criterion')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 建议完成时间 -->
        <!-- <el-table-column
          prop="StudyCode"
          :label="$t('trials:consistencyAnalysis:table:suggesteFinishedTime')"
          show-overflow-tooltip
          sortable="custom"
        /> -->
        <!--操作-->
        <el-table-column
          :label="$t('common:action:action')"
          min-width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <!--阅片结果-->
            <el-button type="text" @click="readResult(scope.row)" disabled>{{
              $t("trials:auditRecord:table:readingResult")
            }}</el-button>
            <!--下载报告-->
            <el-button
              type="text"
              @click="downloadReport(scope.row)"
              disabled
              >{{ $t("trials:reading:button:uploadReport") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        class="page"
        :total="total"
        :page.sync="searchData.PageIndex"
        :limit.sync="searchData.PageSize"
        @pagination="getList"
      />
    </template>
  </BaseContainer>
</template>
<script>
import BaseContainer from "@/components/BaseContainer";
import Pagination from "@/components/Pagination";
import { getPatientVisitTaskList } from "@/api/readManagenent.js";
const defaultSearchData = () => {
  return {
    SubjectCode: null,
    ReadingCategory: 1,
    ReadingTaskState: null,
    TaskState: null,
    BeginSignTime: null,
    EndSignTime: null,
    PageIndex: 1,
    PageSize: 20,
    Asc: false,
    SortField: "CreateTime",
  };
};
export default {
  name: "readManagenent",
  components: { BaseContainer, Pagination },
  data() {
    return {
      // 查询
      searchData: defaultSearchData(),
      dateValue: [],
      // 列表
      list: [],
      loading: false,
      total: 0,
      // 修改检查
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询
    handleSearch() {
      this.getList();
    },
    // 重置
    handleReset() {
      this.reset();
      this.getList();
    },
    // 初始化
    reset() {
      this.searchData = defaultSearchData();
      this.dateValue = [];
      this.$refs.redManagenentTable.clearSort();
    },
    // 排序
    handleSortByColumn(sort) {
      this.searchData.SortField = sort.prop;
      if (sort.order === "ascending") this.searchData.Asc = true;
      if (sort.order === "descending") this.searchData.Asc = false;
      if (!sort.order) this.searchData.SortField = null;
      this.getList();
    },
    // 获取列表
    async getList() {
      try {
        let data = {};
        Object.keys(this.searchData).forEach((key) => {
          data[key] = this.searchData[key];
        });
        data.TrialId = this.$route.query.trialId;
        if (this.dateValue && this.dateValue[0] && this.dateValue[1]) {
          data.earliestStudyTime = this.$moment(this.dateValue[0]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          data.latestStudyTime = this.$moment(this.dateValue[1]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        } else {
          data.EarliestStudyTime = null;
          data.LatestStudyTime = null;
        }
        this.loading = true;
        let res = await getPatientVisitTaskList(data);
        this.loading = false;
        if (res.IsSuccess) {
          this.list = res.Result.CurrentPageData;
          this.total = res.Result.TotalCount;
        }
      } catch (err) {
        this.loading = false;
        console.log(err);
      }
    },
    // 下载报告
    downloadReport() {},
    // 阅片结果
    readResult() {},
  },
};
</script>
