<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <el-card shadow="always">
        <el-form :model="queryForm" :inline="true">
          <el-form-item label="版本号" prop="versions">
            <el-select v-model="queryForm.versions" placeholder="选择版本号">
              <el-option v-for="item in options" :key="item.versions" :label="item.versions" :value="item.versions" />
            </el-select>
          </el-form-item>
          <el-form-item label="表名" prop="sheet">
            <el-select v-model="queryForm.sheet" placeholder="选择表名" @change="changeSheet">
              <el-option v-for="item in sheets" :key="item" :label="item" :value="item" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary">一键展开</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary">一键收起</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </transition>
    <el-card shadow="never" class="mb8">
      <el-table
        v-loading="loading"
        :data="tableData"
        row-key="id"
        border
        lazy
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        default-expand-all
      >
        <el-table-column prop="num" label="编号" />
        <el-table-column prop="name" label="工程或费用名称" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column prop="quantity" label="数量" />
        <el-table-column prop="remark" label="备注" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { useUserStoreHook } from '@/store/modules/user';
import { listBillofquantitiesLimitList, obtainAllVersionNumbers, sheetList } from '@/api/contract/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const userStore = useUserStoreHook();
const currentProject = computed(() => userStore.selectedProject);
const queryForm = ref({
  versions: '',
  sheet: ''
});
const loading = ref(false);
const options = ref<any[]>([]);
const sheets = ref<any[]>([]);
const tableData = ref<any[]>([]);

//获取版本号
const getVersionNums = async () => {
  try {
    const params = {
      projectId: currentProject.value?.id,
      pageSize: 1000,
      pageNum: 1
    };

    const res = await obtainAllVersionNumbers(params);
    if (res.code == 200) {
      options.value = res.rows;
      if (res.rows.length > 0) {
        queryForm.value.versions = res.rows[0].versions;
        getSheetName();
      } else {
        queryForm.value.versions = '';
        ElMessage({
          message: '获取版本号失败',
          type: 'warning'
        });
      }
    }
  } catch (error) {
    console.log(error);
    ElMessage({
      message: '获取版本号失败',
      type: 'warning'
    });
  }
};
//选择表名
const changeSheet = () => {
  getTableData();
};

//获取表名
const getSheetName = async () => {
  try {
    const params = {
      projectId: currentProject.value?.id
      // versions: queryForm.value.versions
    };
    const res = await sheetList(params);
    if (res.code == 200) {
      sheets.value = res.data;
      if (res.data.length > 0) {
        queryForm.value.sheet = res.data[0];
      } else {
        queryForm.value.sheet = '';
        ElMessage({
          message: '获取表名失败',
          type: 'warning'
        });
      }
      getTableData();
    }
  } catch (error) {
    console.log(error);
    ElMessage({
      message: '获取表名失败',
      type: 'warning'
    });
  }
};
//获取表格
const getTableData = async () => {
  loading.value = true;
  const params = {
    projectId: currentProject.value?.id,
    versions: queryForm.value.versions,
    sheet: queryForm.value.sheet
  };
  console.log(params);

  const res = await listBillofquantitiesLimitList(params);
  if (res.code == 200) {
    tableData.value = res.data;
  }
  loading.value = false;
};
//监听项目id刷新数据
const listeningProject = watch(
  () => currentProject.value?.id,
  (nid, oid) => {
    getVersionNums();
  }
);

onUnmounted(() => {
  listeningProject();
});
onMounted(() => {
  // getVersionNums();
  getSheetName();
});
</script>

<style scoped></style>
