<template>
  <PageWrapper dense>
    <a-row>
      <a-col :span="4" style="margin-top: 1%">
        <a-tree
          :show-icon="true"
          :tree-data="treeData"
          v-model:selectedKeys="checkedTree"
          @select="onSelect"
          :fieldNames="{
            children: 'children',
            title: 'projectName',
            key: 'projectId',
            disabled: 'disabled',
          }"
        />
      </a-col>
      <a-col :span="20">
        <BasicTable @register="registerTable">
          <template #toolbar>
            <a-button type="primary" @click="handleAdd" v-if="checkedTree[0]">新增</a-button>
            <a-button type="primary">导入</a-button>
            <a-button type="primary">导出</a-button>
          </template>
          <template #bodyCell="{ column, record }">
            <template v-if="column && record && column.key === 'action'">
              <a-button type="link" @click="handleEdit(record.id)">编辑</a-button>
              <a-popconfirm
                title="确定要删除吗?"
                ok-text="是"
                cancel-text="否"
                @confirm="handleDelete(record.id)"
              >
                <a-button type="link">删除</a-button>
              </a-popconfirm>
            </template>
          </template>
        </BasicTable>
        <addModal ref="addModalRef" @success="reload()"/>
      </a-col>
    </a-row>
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable } from '@/components/Table';
  import { list, tree,removeByIds } from './api';
  import { formSchemas, columns } from './data';
  import addModal from './addModal.vue';
  import { ref, onMounted } from 'vue';

  defineOptions({ name: 'meterial' });

  const [registerTable, { reload }] = useTable({
    rowSelection: {
      type: 'checkbox',
    },
    title: '材料库',
    api: list,
    showIndexColumn: true,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchemas,
      name: 'meterial',
      baseColProps: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 6,
      },
    },
    beforeFetch(params: any) {
      if (checkedTree.value.length === 1) {
        params.projectId = checkedTree.value[0];
      }
      return params;
    },
    immediate: false,
    columns: columns,
    actionColumn: {
      width: 300,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });
  //新增编辑弹窗
  const addModalRef = ref();
  const handleAdd = () => {
    addModalRef.value.showModal(1, null, checkedTree.value[0]);
  };
  const handleEdit = (id: any) => {
    addModalRef.value.showModal(2, id, checkedTree.value[0]);
  };
  const handleDelete = async (id: any) => {
    await removeByIds([id]);
    reload();
  };

  //tree
  const checkedTree = ref([]);
  const treeData = ref([]);
  const getTree = async () => {
    const res = await tree();
    treeData.value = res;
    treeData.value.forEach((i: any) => {
      i.disabled = true;
    });
    console.log(res);
  };
  const onSelect = (selectedKeys: any, info: any) => {
    console.log('selected', selectedKeys, info);
    console.log(checkedTree.value);
    reload();
  };
  onMounted(() => {
    getTree();
  });
  //抽屉
</script>

<style scoped></style>
