<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="showpointModal(record.id)">点位上报</a-button>
              <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>
              <a-button type="link" @click="handleDetail(record.id)">详情</a-button>
            </template>
          </template>
        </BasicTable>
        <detailDrawer ref="detailDrawerRef" />
        <addModal ref="addModalRef" @success="reload()"/>
        <pointModal ref="pointModalRef" />
      </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 detailDrawer from './detailDrawer.vue';
  import addModal from './addModal.vue';
  import { ref, onMounted } from 'vue';
  import pointModal from './pointModal.vue';

  defineOptions({ name: 'Point' });

  const [registerTable, { reload,getDataSource}] = useTable({
    title: '点位信息',
    api: list,
    showIndexColumn: true,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchemas,
      name: 'point',
      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],getDataSource());
  };
  const handleEdit = (id: any) => {
    addModalRef.value.showModal(2, id,checkedTree.value[0],getDataSource());
  };
  const handleDelete = async (id: any) => {
    await removeByIds([id]);
    reload();
  };
  //点位上报
  //弹窗内容
  const pointModalRef = ref();
  const showpointModal = (id: any) => {
    pointModalRef.value.showModal(id);
  };
  //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();
  });
  //抽屉
  const detailDrawerRef = ref();
  const handleDetail = (id: any) => {
    detailDrawerRef.value.showDrawer(id);
  };
</script>

<style scoped></style>
