<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]">
        <el-card v-show="showSearch" shadow="hover" class="filter-main">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="80px">
            <el-form-item label="文件名称" prop="fileName">
              <el-input v-model="queryParams.fileName" placeholder="请输入文件名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <el-card shadow="never" class="mt-[10px]">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button-group>
                  <el-button :type="activeTab == 'expert' ? 'primary' : ''" @click="changeTab('expert')">专家</el-button>
                  <el-button :type="activeTab == 'project' ? 'primary' : ''" @click="changeTab('project')">项目</el-button>
                </el-button-group>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getData"></right-toolbar>
            </el-row>
          </template>
          <div class="card-main">
            <el-table v-loading="loading" :data="lists">
              <el-table-column label="序号" width="50px" type="index"></el-table-column>
              <el-table-column
                :label="label"
                :align="align || 'center'"
                :min-width="minWidth || '100px'"
                v-for="{ label, prop, align, minWidth } in headers"
                :key="prop"
              >
                <template #default="{ row }">
                  <span v-if="prop == 'reviewSn'">{{ stageObj[row.reviewSn] || '-' }}阶段</span>
                  <span v-else-if="prop == 'originalName'">
                    <a :href="row.url || ''" :download="row.originalName" class="download" target="_blank">{{ row.originalName }}</a>
                  </span>
                  <span v-else-if="prop == 'type'">{{ fileType[row.type] || '-' }}</span>
                  <span v-else>{{ row[prop] || '-' }}</span>
                </template>
              </el-table-column>
            </el-table>

            <pagination v-show="page.total > 0" :total="page.total" v-model:page="page.pageNum" v-model:limit="page.pageSize" @pagination="getList" />
          </div>
        </el-card>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { getProjectFileList, getExpertFileList } from '@/api/filemanage';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const showSearch = ref(true);
const queryFormRef = ref<ElFormInstance>();
const { item_stage } = toRefs<any>(proxy?.useDict('item_stage'));
const stageObj = computed(() => {
  return item_stage.value.reduce((acc, cur) => {
    acc[cur.value] = cur.label;
    return acc;
  }, {});
});

const fileType = {
  1: '专利',
  2: '论文'
};

const activeTab = ref('expert');
const changeTab = async (key) => {
  activeTab.value = key;
  await proxy?.$nextTick();
  getData();
};
const lists = ref([]);
const expertHeader = [
  {
    label: '文档名称',
    prop: 'originalName'
  },
  {
    label: '专家姓名',
    prop: 'expertName'
  },
  {
    label: '类型',
    prop: 'type'
  },
  {
    label: '期刊号/专利号',
    prop: 'number'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
];
const projectHeader = [
  {
    label: '文档名称',
    prop: 'originalName'
  },
  {
    label: '项目名称',
    prop: 'productName'
  },
  {
    label: '项目阶段',
    prop: 'reviewSn'
  },
  {
    label: '类型',
    prop: 'type'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
];
const headers = computed(() => {
  return activeTab.value == 'expert' ? expertHeader : projectHeader;
});

const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  fileName: ''
});

/** 搜索按钮操作 */
const handleQuery = () => {
  page.value.pageNum = 1;
  getData();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const loading = ref(false);
const getData = async () => {
  loading.value = true;
  let ajax = activeTab.value == 'expert' ? getExpertFileList : getProjectFileList;
  let res = await ajax({ ...queryParams.value, ...page.value });
  lists.value = res.rows;
  page.value.total = res.total;
  loading.value = false;
};
onActivated(() => {
  getData();
});
onMounted(() => {
  getData();
});
</script>

<style scoped lang="scss">
:deep(aside) {
  margin-bottom: 0;
  background-color: #fff;
  border-right: 1px solid #ddd;
}
ul {
  list-style-type: none;
  padding: 0;
  li {
    text-align: center;
    line-height: 36px;
    font-size: 14px;
    &.active {
      color: var(--el-color-primary);
    }
  }
}
.download {
  cursor: pointer;
  color: var(--el-color-primary);
}
.filter-main {
  :deep(.el-card__body) {
    padding-bottom: 0 !important;
  }
}
</style>
