<template>
  <BaseContainer>
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 受试者编号 -->
        <el-form-item :label="$t('trials:subject: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.PatientIdStr" style="width: 140px" />
        </el-form-item>
        <!-- 性别 -->
        <el-form-item :label="$t('trials:subject:table:gender')">
          <el-select v-model="searchData.SubjectSex" clearable>
            <el-option
              v-for="item of $d.sex"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 申请号 -->
        <!-- <el-form-item :label="$t('trials:study:form:accessionNumber')">
          <el-input v-model="searchData.SubjectInfo" style="width: 140px" />
        </el-form-item> -->
        <!-- 检查设备 -->
        <!-- <el-form-item :label="$t('trials:studyList:table:modality')">
          <el-select v-model="searchData.VisitPlanArray" clearable>
            <el-option
              v-for="item of $d.sex"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- 检查类型 -->
        <!-- <el-form-item :label="$t('trials:seletctedReviews:form:modality')">
          <el-select v-model="searchData.Modalities" clearable>
            <el-option
              v-for="item of $d.modalType"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- 检查日期 -->
        <el-form-item :label="$t('trials:audit:table:studyDate')">
          <el-date-picker
            v-model="dateValue"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 状态 -->
        <!-- <el-form-item :label="$t('trials:seletctedReviews:table:status')">
          <el-select v-model="searchData.VisitPlanArray" clearable>
            <el-option
              v-for="item of $d.sex"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </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: 80 }"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
        :default-sort="{ prop: 'StudyTime', order: 'descending' }"
      >
        <el-table-column type="index" width="40" />
        <!-- 受试者编号 -->
        <el-table-column
          prop="SubjectCode"
          :label="$t('trials:subject:table:subjectId')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        />
        <!-- 姓名 -->
        <el-table-column
          prop="SubjectShortName"
          :label="$t('trials:researchStaff:table:Name')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        />
        <!-- 性别 -->
        <el-table-column
          prop="SubjectSex"
          :label="$t('trials:subject:table:gender')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        >
          <template slot-scope="scope">
            <span>{{ $fd("sex", Number(scope.row.SubjectSex)) }}</span>
          </template>
        </el-table-column>
        <!-- 患者编号 -->
        <el-table-column
          prop="PatientIdStr"
          :label="$t('trials:uploadDicomList:table:pId')"
          show-overflow-tooltip
          min-width="100"
        >
        </el-table-column>
        <!-- 检查类型 -->
        <el-table-column
          prop="Modalities"
          :label="$t('trials:audit:table:modality')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        />
        <!--序列数量-->
        <el-table-column
          align="center"
          prop="SeriesCount"
          :label="$t('trials:audit:table:seriesCount')"
          show-overflow-tooltip
        ></el-table-column>
        <!--图像数量-->
        <el-table-column
          align="center"
          prop="InstanceCount"
          :label="$t('trials:audit:table:instanceCount')"
          show-overflow-tooltip
        ></el-table-column>
        <!-- 检查日期 -->
        <el-table-column
          prop="StudyTime"
          :label="$t('trials:audit:table:studyDate')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        />
        <!-- 访视 -->
        <!-- <el-table-column
          prop="VisitName"
          :label="$t('trials:auditRecord:table:visit')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        /> -->
        <!-- 状态 -->
        <!-- <el-table-column
          prop="SubmitState"
          :label="$t('trials:trials-list:table:status')"
          show-overflow-tooltip
          sortable="custom"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.SubmitState * 1 === 0" type="warning">{{
              $fd("SubmitState", scope.row.SubmitState * 1)
            }}</el-tag>
            <el-tag v-else-if="scope.row.SubmitState * 1 === 1" type="danger">{{
              $fd("SubmitState", scope.row.SubmitState * 1)
            }}</el-tag>
            <el-tag v-else-if="scope.row.SubmitState * 1 === 2">{{
              $fd("SubmitState", scope.row.SubmitState * 1)
            }}</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column
          :label="$t('common:action:action')"
          min-width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-button
              type="text"
              @click="handleViewStudy(scope.row)"
              v-hasPermi="['trials:trials-panel:study:view']"
              disabled
              >{{ $t("trials:studyList:button:view") }}</el-button
            >
            <!-- 修改访视 -->
            <el-button
              type="text"
              @click="editVisitFn(scope.row)"
              v-hasPermi="['trials:trials-panel:study:editVisit']"
              >{{ $t("trials:study:button:bindVisit") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        class="page"
        :total="total"
        :page.sync="searchData.PageIndex"
        :limit.sync="searchData.PageSize"
        @pagination="getList"
      />
    </template>
    <editVisit
      v-if="editVisitVisible"
      :visible.sync="editVisitVisible"
      :data="editVisitMessage"
      @getList="getList"
    />
  </BaseContainer>
</template>
<script>
import BaseContainer from "@/components/BaseContainer";
import Pagination from "@/components/Pagination";
import editVisit from "./edit-visit";
import { getTrialUnbindSubjectVisitStudyList } from "@/api/trials/study.js";
const defaultSearchData = () => {
  return {
    SubjectCode: null,
    PatientIdStr: null,
    PatientSex: null,
    BeginStudyTime: null,
    EndStudyTime: null,
    Asc: false,
    SortField: "StudyTime",
    PageIndex: 1,
    PageSize: 20,
  };
};
export default {
  name: "notBindStudyList",
  components: {
    BaseContainer,
    Pagination,
    editVisit,
  },
  props: {
    activeName: {
      required: true,
      default: "notSubmit",
    },
  },
  data() {
    return {
      // 搜索
      searchData: defaultSearchData(),
      dateValue: [],
      // 列表
      list: [],
      loading: false,
      total: 0,

      // 修改访视相关
      editVisitVisible: false,
      editVisitMessage: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 修改访视
    editVisitFn(item) {
      this.editVisitVisible = true;
      this.editVisitMessage = item;
      this.editVisitMessage.TrialId = this.$route.query.trialId;
    },
    // 获取检查列表数据
    async getList() {
      let data = {};
      Object.keys(this.searchData).forEach((key) => {
        data[key] = this.searchData[key];
      });
      data.TrialId = this.$route.query.trialId;
      if (this.dateValue[0] && this.dateValue[1]) {
        data.BeginStudyTime = this.dateValue[0];
        data.EndStudyTime = this.dateValue[1];
      } else {
        data.BeginStudyTime = null;
        data.EndStudyTime = null;
      }
      try {
        let res = await getTrialUnbindSubjectVisitStudyList(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);
      }
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1;
      this.getList();
    },
    // 重置
    handleReset() {
      this.reset();
      this.getList();
    },
    // 初始化
    reset() {
      this.searchData = defaultSearchData();
      this.dateValue = [];
    },
    // 排序
    handleSortByColumn(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>