<template>
  <BaseContainer class="qc-check-wrapper">
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 中心编号 -->
        <el-form-item :label="$t('trials:dicomForward:table:siteId')">
          <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:dicomForward:table:subjectId')">
          <el-input v-model="searchData.SubjectInfo" style="width: 100px" />
        </el-form-item>
        <!-- 访视名称 -->
        <el-form-item
          class="my_multiple"
          :label="$t('trials:dicomForward:tableon:visitName')"
        >
          <el-select
            v-model="searchData.VisitPlanArray"
            style="width: 140px"
            clearable
            multiple
          >
            <el-option
              v-for="(item, index) of visitPlanOptions"
              :key="index"
              :label="item.VisitName"
              :value="item.VisitNum"
            >
              <span style="float: left">{{ item.VisitName }}</span>
            </el-option>
            <el-option key="Other" label="Out of Plan" value="1.11" />
          </el-select>
        </el-form-item>
        <!-- 转发状态 -->
        <el-form-item :label="$t('trials:dicomForward:table:forwardStatus')">
          <el-select
            v-model="searchData.ForwardState"
            clearable
            style="width: 120px"
          >
            <template v-for="item of $d.ForwardStateEnum">
              <el-option
                v-if="item.value !== 0"
                :value="item.value"
                :label="item.label"
                :key="item.id"
              />
            </template>
          </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-button
            :disabled="loading || selectArr.length === 0"
            type="primary"
            icon="el-icon-document-copy"
            @click="handleBulkForward"
          >
            {{ $t("trials:dicomForward:action:forward") }}
          </el-button>
        </el-form-item>
      </el-form>
    </template>

    <!-- qc审核列表 -->
    <template slot="main-container">
      <el-table
        ref="forwardList"
        v-loading="loading"
        v-adaptive="{ bottomOffset: 60 }"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          align="left"
          width="45"
          :selectable="handleSelectTable"
        />
        <!-- 中心编号 -->
        <el-table-column
          prop="TrialSiteCode"
          :label="$t('trials:dicomForward:table:siteId')"
          show-overflow-tooltip
          min-width="90"
          sortable="custom"
        />
        <!-- 受试者编号 -->
        <el-table-column
          prop="SubjectCode"
          :label="$t('trials:dicomForward:table:subjectId')"
          show-overflow-tooltip
          min-width="90"
          sortable="custom"
        />
        <!-- 访视名称 -->
        <el-table-column
          prop="VisitName"
          :label="$t('trials:dicomForward:tableon:visitName')"
          show-overflow-tooltip
          min-width="90"
          sortable="custom"
        />
        <!-- 转发状态 -->
        <el-table-column
          prop="ForwardState"
          :label="$t('trials:dicomForward:table:forwardStatus')"
          show-overflow-tooltip
          min-width="90"
          sortable="custom"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.ForwardState * 1 === 0">--</span>
            <el-tag
              v-else-if="scope.row.ForwardState * 1 === 14"
              type="warning"
              >{{ $fd("ForwardStateEnum", scope.row.ForwardState * 1) }}</el-tag
            >
            <el-tag v-else-if="scope.row.ForwardState * 1 === 15">{{
              $fd("ForwardStateEnum", scope.row.ForwardState * 1)
            }}</el-tag>
            <el-tag
              v-else-if="scope.row.ForwardState * 1 === 16"
              type="danger"
              >{{ $fd("ForwardStateEnum", scope.row.ForwardState * 1) }}</el-tag
            >
          </template>
        </el-table-column>
        <!-- 转发人 -->
        <el-table-column
          prop="ForwardUserName"
          :label="$t('trials:dicomForward:table:forwardUser')"
          show-overflow-tooltip
          min-width="100"
          sortable="custom"
        />
        <!-- 转发时间 -->
        <el-table-column
          prop="ForwardTime"
          :label="$t('trials:dicomForward:table:forwardTime')"
          show-overflow-tooltip
          min-width="100"
          sortable="custom"
        />
        <el-table-column
          v-if="hasPermi(['trials:trials-panel:visit:forward-list:forward'])"
          :label="$t('common:action:action')"
          min-width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <!-- 转发 -->
            <el-button
              v-hasPermi="['trials:trials-panel:visit:forward-list:forward']"
              circle
              :title="$t('trials:dicomForward:action:forward')"
              icon="el-icon-document-copy"
              :disabled="scope.row.ForwardState === 15"
              @click="handleForward(scope.row)"
            />
          </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 {
  getTrialSiteSelect,
  getTrialVisitStageSelect,
  getForwardList,
  forwardSVDicomImage,
} from "@/api/trials";
import BaseContainer from "@/components/BaseContainer";
import Pagination from "@/components/Pagination";
const searchDataDefault = () => {
  return {
    SiteId: "",
    SubjectInfo: "",
    VisitPlanArray: [],
    ForwardState: null,
    PageIndex: 1,
    PageSize: 20,
  };
};
export default {
  name: "ForwardList",
  components: { BaseContainer, Pagination },
  data() {
    return {
      searchData: searchDataDefault(),
      total: 0,
      list: [],
      loading: false,
      userTypeEnumInt: zzSessionStorage.getItem("userTypeEnumInt") * 1,
      userId: zzSessionStorage.getItem("userId"),
      siteOptions: [],
      visitPlanOptions: [],
      trialId: this.$route.query.trialId,
      selectArr: [],
    };
  },
  mounted() {
    this.getList();
    this.getSite();
    this.getVisitPlanOptions();
  },
  methods: {
    // 获取qc审核列表
    getList() {
      this.loading = true;
      this.searchData.TrialId = this.trialId;
      getForwardList(this.searchData)
        .then((res) => {
          this.loading = false;
          this.list = res.Result.CurrentPageData;
          this.total = res.Result.TotalCount;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 单个转发
    handleForward(row) {
      this.$confirm(this.$t("trials:dicomForward:message:sureToForward"), {
        type: "warning",
        distinguishCancelAndClose: true,
      })
        .then(() => {
          this.forward([row.Id]);
        })
        .catch(() => {});
    },

    // 批量转发
    handleBulkForward() {
      this.$confirm(this.$t("trials:dicomForward:message:sureToForward"), {
        type: "warning",
        distinguishCancelAndClose: true,
      })
        .then(() => {
          this.forward(this.selectArr);
        })
        .catch(() => {});
    },
    // 影像转发
    forward(arr) {
      this.loading = true;
      forwardSVDicomImage(this.trialId, arr)
        .then((res) => {
          this.loading = false;
          this.$message.success(
            this.$t("trials:dicomForward:message:forwardSuccessfully")
          );
          this.getList();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 当选择项发生变化时会触发该事件
    handleSelectionChange(val) {
      const arr = [];
      for (let index = 0; index < val.length; index++) {
        arr.push(val[index].Id);
      }
      this.selectArr = arr;
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault();
      this.getList();
      this.$nextTick(() => {
        this.$refs.forwardList.clearSort();
      });
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1;
      this.getList();
    },
    // 排序
    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();
    },
    // 通过提交状态决定该行的 CheckBox 是否可以勾选
    handleSelectTable(row) {
      return row.ForwardState * 1 !== 15;
    },
    // 获取site下拉框数据
    getSite() {
      getTrialSiteSelect(this.trialId).then((res) => {
        this.siteOptions = res.Result;
      });
    },
    // 获取访视下拉框数据
    getVisitPlanOptions() {
      getTrialVisitStageSelect(this.trialId).then((res) => {
        this.visitPlanOptions = res.Result;
      });
    },
  },
};
</script>
