<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch">
        <el-card shadow="hover" class="mb-[10px]">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="项目名称" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入项目名称"></el-input>
            </el-form-item>
            <el-form-item label="项目阶段" prop="reviewSn">
              <el-select v-model="queryParams.reviewSn" placeholder="请选择项目">
                <el-option v-for="item in item_stage" :key="item.value" :label="`${item.label}阶段`" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目规模" prop="productScale">
              <el-select v-model="queryParams.productScale" placeholder="请选择项目" clearable>
                <el-option v-for="project in scaleLists" :key="project.value" :label="project.label" :value="project.value" />
              </el-select>
            </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">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5"> </el-col>
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>

          <el-table v-loading="loading" :data="tableRows">
            <el-table-column label="序号" type="index" width="50px"></el-table-column>
            <el-table-column label="项目名称" prop="productName" />
            <el-table-column label="项目阶段" prop="reviewSn">
              <template #default="{ row }">
                {{ (item_stage.find((e) => e.value == row.reviewSn)?.label || '') + '阶段' || '-' }}
              </template>
            </el-table-column>
            <el-table-column label="项目年份" prop="projectYear" />
            <el-table-column label="项目规模" prop="productScale">
              <template #default="{ row }">{{ row.productScale }}万元</template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="100px">
              <template #default="scope">
                <el-button link type="primary" @click="handleShow(scope.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 lang="ts">
import { getTableList } from '@/api/expertMoneyConfig/audit';
const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { item_stage } = toRefs<any>(proxy?.useDict('item_stage'));
const showSearch = ref(true);

const scaleLists = [
  {
    label: '100万以下',
    value: '1'
  },
  {
    label: '100万~500万',
    value: '2'
  },
  {
    label: '500万~1000万',
    value: '3'
  },
  {
    label: '1000万以上',
    value: '4'
  }
];

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

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const tableRows = ref([]);
const loading = ref(false);
const getList = async () => {
  loading.value = true;
  let params = {
    ...queryParams.value,
    ...page.value
  };
  if (params.productScale) {
    let json = {
      1: [null, 100],
      2: [100, 500],
      3: [500, 1000],
      4: [1000, null]
    };
    const [min, max] = json[params.productScale];
    params.minProductScale = min;
    params.maxProductScale = max;
    delete params.productScale;
  }
  let res = await getTableList(params);
  tableRows.value = res.rows.map((e) => {
    return {
      ...e,
      projectYear: e.createTime.substring(0, 4) + '年'
    };
  });
  loading.value = false;
};
const handleShow = (row) => {
  console.log(row);
  router.push({
    path: '/expertmoneyconfig/auditDetail',
    query: {
      id: row.productId,
      productName: row.productName,
      reviewSn: row.reviewSn,
      projectYear: row.projectYear,
      productScale: row.productScale
    }
  });
};
onMounted(() => {
  getList();
});
</script>
