<template>
  <BaseContainer>
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <!-- 文件类型 -->
        <el-form-item :label="$t('trials:signRecords:table:fileType')">
          <el-select v-model="searchData.FileTypeId" clearable style="width: 150px">
            <el-option
              v-for="item of typeOptions"
              :key="item.FileTypeId"
              :label="item.FileType"
              :value="item.FileTypeId"
            />
          </el-select>
        </el-form-item>
        <!-- 文件名称 -->
        <el-form-item :label="$t('trials:signRecords:table:fileName')">
          <el-input v-model="searchData.Name" style="width: 120px" />
        </el-form-item>
        <!-- 签署人 -->
        <el-form-item :label="$t('trials:signRecords:table:user')">
          <el-select
            v-model="searchData.UserId"
            clearable
            filterable
            style="width: 140px"
          >
            <el-option
              v-for="item of userOptions"
              :key="item.UserId"
              :label="item.RealName"
              :value="item.UserId"
            >
              <span style="float: left">{{ item.RealName }}</span>
              <span
                style="
                  float: right;
                  color: #8492a6;
                  font-size: 13px;
                  margin-left: 5px;
                "
              >
                {{ item.UserName }}
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 用户类型 -->
        <el-form-item :label="$t('trials:signRecords:table:userType')">
          <el-select
            v-model="searchData.UserTypeId"
            clearable
            filterable
            style="width: 120px"
          >
            <el-option
              v-for="item of userTypeOptions"
              v-show="item.UserTypeEnum !== 26 && item.UserTypeEnum !== 27"
              :key="item.Id"
              :label="item.UserTypeShortName"
              :value="item.Id"
            >
              <span>{{ item.UserType }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trials:signRecords:table:isSign')">
          <el-select
            v-model="searchData.IsConfirmed"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="i of $d.YesOrNo"
              :key="'IsConfirmed' + i.label"
              :value="i.value"
              :label="i.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('trials:signRecords:table:isDeleted')">
          <el-select
            v-model="searchData.IsDeleted"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="i of $d.YesOrNo"
              :key="'IsDeleted' + i.label"
              :value="i.value"
              :label="i.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <!-- 查询 -->
          <el-button icon="el-icon-search" type="primary" @click="handleSearch">
            {{ $t("common:button:search") }}
          </el-button>
          <!-- 重置 -->
          <el-button
            icon="el-icon-refresh-left"
            type="primary"
            @click="handleReset"
          >
            {{ $t("common:button:reset") }}
          </el-button>
          <!-- 导出 -->
          <el-button
            type="primary"
            icon="el-icon-download"
            :loading="exportLoading"
            @click="handleExport"
          >
            {{ $t("common:button:export") }}
          </el-button>
        </el-form-item>
      </el-form>
    </template>

    <template slot="main-container">
      <!-- 系统文件列表 -->
      <el-table
        ref="AttachmentsManagement"
        v-loading="loading"
        v-adaptive="{ bottomOffset: 60 }"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortByColumn"
      >
        <el-table-column type="index" width="40" />
        <!-- 文件类型 -->
        <el-table-column
          prop="FileType"
          :label="$t('trials:signRecords:table:fileType')"
          show-overflow-tooltip
          sortable="custom"
          width="160"
        />
        <!-- 文件名称 -->
        <el-table-column
          prop="Name"
          :label="$t('trials:signRecords:table:fileName')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 文件级别 -->
        <el-table-column
          prop="IsSystemDoc"
          :label="$t('trials:signRecords:table:fileLevel')"
          show-overflow-tooltip
          width="160"
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsSystemDoc" type="primary">{{
              $fd("IsSystemDoc", scope.row.IsSystemDoc)
            }}</el-tag>
            <el-tag v-else type="warning">{{
              $fd("IsSystemDoc", scope.row.IsSystemDoc)
            }}</el-tag>
          </template>
        </el-table-column>
        <!-- 是否废除 -->
        <el-table-column
          prop="IsDeleted"
          :label="$t('trials:signRecords:table:isDeleted')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsDeleted" type="danger">{{
              $fd("YesOrNo", scope.row.IsDeleted)
            }}</el-tag>
            <el-tag v-else type="primary">{{
              $fd("YesOrNo", scope.row.IsDeleted)
            }}</el-tag>
          </template>
        </el-table-column>
        <!-- 上传时间 -->
        <el-table-column
          prop="CreateTime"
          :label="$t('trials:signRecords:table:uploadTime')"
          show-overflow-tooltip
          width="160"
          sortable="custom"
        />
        <!-- 是否签署 -->
        <el-table-column
          prop="ConfirmTime"
          :label="$t('trials:signRecords:table:isSign')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="!scope.row.IsConfirmed" type="primary">{{
              $fd("YesOrNo", scope.row.IsConfirmed)
            }}</el-tag>
            <el-tag v-else type="danger">{{
              $fd("YesOrNo", scope.row.IsConfirmed)
            }}</el-tag>
          </template>
        </el-table-column>
        <!-- 签署人 -->
        <el-table-column
          prop="RealName"
          :label="$t('trials:signRecords:table:user')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 用户名 -->
        <el-table-column
          prop="UserName"
          :label="$t('trials:signRecords:table:userName')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 用户类型 -->
        <el-table-column
          prop="UserTypeShortName"
          :label="$t('trials:signRecords:table:userType')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 签署时间 -->
        <el-table-column
          prop="ConfirmTime"
          :label="$t('trials:signRecords:table:confirmTime')"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column :label="$t('common:action:action')">
          <template slot-scope="scope">
            <!-- 预览 -->
            <el-button
              circle
              :title="$t('trials:signRecords:action:preview')"
              icon="el-icon-view"
              @click="handlePreview(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        class="page"
        :total="total"
        :page.sync="searchData.PageIndex"
        :limit.sync="searchData.PageSize"
        @pagination="getList"
      />
    </template>

    <!-- 预览文件 -->
    <el-dialog
      v-if="previewVisible"
      :visible.sync="previewVisible"
      :title="$t('trials:signRecords:action:preview')"
      :fullscreen="true"
      append-to-body
      custom-class="base-dialog-wrapper"
    >
      <div
        class="base-modal-body"
        style="border: 2px solid #ccc; padding: 10px"
      >
        <PreviewFile
          v-if="previewVisible"
          :file-path="currentPath"
          :file-type="currentType"
          @getList="getList"
        />
      </div>
    </el-dialog>
  </BaseContainer>
</template>
<script>
import {
  getDocumentConfirmList,
  getTrialUserSelect,
  getTrialUserTypeList,
  getTrialDocAndSystemDocType,
} from "@/api/trials";
import { pMTrainingRecordList_Export } from "@/api/export";
import BaseContainer from "@/components/BaseContainer";
import Pagination from "@/components/Pagination";
import PreviewFile from "@/components/PreviewFile/index";
const searchDataDefault = () => {
  return {
    FileTypeId: "",
    Name: "",
    UserId: "",
    UserTypeId: "",
    PageIndex: 1,
    PageSize: 20,
    IsConfirmed: null,
    IsDeleted: null,
  };
};
export default {
  name: "AttachmentsManagement",
  components: { BaseContainer, Pagination, PreviewFile },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      currentRow: {},
      currentPath: "",
      currentType: "",
      previewVisible: false,
      userOptions: [],
      userTypeOptions: [],
      currentUser: zzSessionStorage.getItem("userName"),
      typeOptions: [],
      trialId: this.$route.query.trialId,
      exportLoading: false,
    };
  },
  mounted() {
    this.getTypeOptions();
    this.getUserSelect();
    this.getUserType();
    this.getList();
  },
  methods: {
    handleExport() {
      this.exportLoading = true;
      pMTrainingRecordList_Export(this.searchData)
        .then(() => {
          this.exportLoading = false;
        })
        .catch((err) => {
          this.exportLoading = false;
        });
    },
    // 获取系统文件数据
    getList() {
      this.loading = true;
      this.searchData.TrialId = this.trialId;
      getDocumentConfirmList(this.searchData)
        .then(async (res) => {
          this.loading = false;
          this.list = res.Result.CurrentPageData;
          this.total = res.Result.TotalCount;
          console.log(this.total);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 获取文件类型下拉数据
    getTypeOptions() {
      getTrialDocAndSystemDocType(this.trialId).then((res) => {
        this.typeOptions = res.Result;
      });
    },
    // 获取当前项目下参与者信息
    getUserSelect() {
      getTrialUserSelect(this.trialId).then((res) => {
        this.userOptions = res.Result;
      });
    },
    // 获取用户类型下拉数据
    getUserType() {
      getTrialUserTypeList().then((res) => {
        this.userTypeOptions = res.Result;
      });
    },
    // 预览
    handlePreview(row) {
      this.currentRow = { ...row };
      const { Name, FullFilePath } = row;
      this.currentPath = FullFilePath;
      this.currentType = row.Name
        ? Name.substring(Name.lastIndexOf(".") + 1).toLocaleLowerCase()
        : "";
      this.previewVisible = true;
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault();
      this.getList();
      this.$nextTick(() => {
        this.$refs.AttachmentsManagement.clearSort();
      });
    },
    // 查询
    handleSearch() {
      this.getList();
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === "ascending") {
        this.searchData.Asc = true;
      } else {
        this.searchData.Asc = false;
      }
      this.searchData.SortField = column.prop;
      this.getList();
    },
  },
};
</script>
