<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="productName">
              <el-input v-model="queryParams.productName" 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 icon="Plus" type="primary" @click="handle(null, 'add')">新建</el-button>
              </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>

          <el-table v-loading="loading" :data="extractLogList">
            <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>{{ row[prop] || '-' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" min-width="150px" fixed="right" class-name="small-padding fixed-width">
              <template #default="{ row }">
                <el-button link type="primary" @click="handle(row, 'show')">详情</el-button>
                <el-button link type="primary" @click="handle(row, 'edit')">编辑</el-button>
                <el-button link type="danger" @click="DelProjectFile(row)">删除</el-button>
              </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" />
        </el-card>
      </div>
    </transition>
  </div>
</template>

<script setup name="ExtractLog" lang="ts">
import { getTableList, delList } from '@/api/filemanage';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
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 extractLogList = ref<any[]>([]);
const loading = ref(true);
const showSearch = ref(true);

const route = useRoute();

const queryFormRef = ref<ElFormInstance>();

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}
const headers = ref<Header[]>([
  {
    label: '项目名称',
    prop: 'productName'
  },
  {
    label: '项目编号',
    prop: 'productSn'
  },
  {
    label: '项目阶段',
    prop: 'reviewSn'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]);

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

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await getTableList({ ...queryParams.value, ...page.value });
  extractLogList.value = res.rows;
  page.value.total = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const handle = async (row: any, type) => {
  let query = {
    type,
    from: 'manage'
  };
  if (row) {
    query.id = row.productReviewId;
  }
  proxy?.$router.push({
    path: '/backStage/fileAdd',
    query
  });
};
const DelProjectFile = (row) => {
  ElMessageBox.confirm(`是否确定删除该项目文档`, '系统提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      let res = await delList(row.id);
      if (res.code != 200) {
        ElNotification({
          title: '提示',
          message: res.msg,
          type: 'error'
        });
        return;
      }
      ElNotification({
        title: '提示',
        message: '删除成功',
        type: 'success'
      });
      getList();
    })
    .catch(() => {});
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.filter-main {
  :deep(.el-card__body) {
    padding-bottom: 0 !important;
  }
}
</style>
