<template>
  <div>
    <BasicTable  :clickToRowSelect="false" @register="registerTable">
      <template #toolbar>
        <Authority value="api:sw:txxdgl:add">
          <a-button type="primary" @click="handleCreate">新增</a-button>
        </Authority>
        <!-- <Authority value="api:sw:txxdgl:batchDelete">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDeleteOrBatchDelete(null)"
          >
            <a-button color="error"> 批量删除 </a-button>
          </Popconfirm>
        </Authority> -->
        <Authority>
          <a-button type="primary" @click="exportDataFn">导出</a-button>
        </Authority>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '详情',
              auth: 'api:sw:txxdgl:detail',
              icon: 'clarity:info-standard-line',
              onClick: handleViewDetail.bind(null, record),
            },
            {
              label: '编辑',
              auth: 'api:sw:txxdgl:eidt',
              icon: 'clarity:note-edit-line',
              onClick: handleViewEidt.bind(null, record),
            },
            {
              label: '删除',
              auth: 'api:sw:txxdgl:del',
              icon: 'ant-design:delete-outlined',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleViewdel.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { defineComponent, ref, watch, onMounted, reactive } from 'vue';
  import { useModal } from '/@/components/Modal';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getAllCom, getDeleteTree } from '/@/api/swHome/index';
  import { exportData } from '/@/utils/fnUtils.ts';
  let dataCenter = reactive({
    columns: [],
    accountFormSchema: [],
  });
  const [registerTable, { reload, getColumns, getDataSource, setLoading, setColumns, setTableData }] = useTable({
    title: '信道管理',
    // api: getAllCom,
    columns: [],
    // resizeHeightOffset:40,
    showIndexColumn: false,
    clickToRowSelect: false,
    pagination: false,
    loading: false,
    useSearchForm: false,
    showTableSetting: true,
    bordered: true,
    rowKey: 'id',
    actionColumn: {
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    fetchSetting: {
      listField: 'data.value',
    },
  });
  //弹窗开始
  import AccountModal from './components/AccountModal.vue';
  const [registerModal, { openModal }] = useModal();

  function handleSuccess(record) {
    // console.log('提交成功');
    getAllComData();
  }
  // 新增
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }
  // 详情
  function handleViewDetail(record) {
    openModal(true, {
      details: true,
      formData: record,
    });
  }
  // 编辑
  function handleViewEidt(record) {
    console.log(record);
    openModal(true, {
      isUpdate: true,
      formData: record,
    });
  }
  // 删除
  function handleViewdel(record) {
    let formData = {};
    formData.type = 'com';
    formData.attr = {
      com_name: record.com_name,
      basin_id: 0,
      port: record.port,
      stcd: 0,
      pid: 0,
    };
    getDeleteTree(formData).then((res) => {
      if(res.code == 200){
        getAllComData()
      }
    });
  }

  function processingData(table_head) {
    table_head.forEach((element) => {
      for (let key in element) {
        let item = element[key];
        element.title = item;
        element.key = key;
        element.dataIndex = key;
      }
    });
    return table_head;
  }
  function getAllComData() {
    getAllCom().then((res) => {
      dataCenter.accountFormSchema = res.data.table_head;
      setColumns(processingData(res.data.table_head));
      setTableData(res.data.table_value);
    });
  }
  const exportDataFn = () => {
    exportData(getColumns(),getDataSource(),'信道管理.xlsx')
  }
  onMounted(() => {
    getAllComData();
  });
</script>
<style scoped lang="less">
.vben-basic-table-form-container {
  padding: 0px;
}
</style>
