<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #name="{ record }">
        <a @click="handleView(record)"> {{ record.name }} </a>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除？',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DeviceDrawer @register="registerDrawer" @success="handleSuccess" />
    <!-- <DetailModal @register="registerModal" v-model:visible="modalVisible" :userData="userData" /> -->
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { useGo } from '/@/hooks/web/usePage';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { search, remove } from '/@/api/test/modelApi';

  import { useDrawer } from '/@/components/Drawer';
  import DeviceDrawer from './DeviceDrawer.vue';
  import { useModal } from '/@/components/Modal';
  import DetailModal from './detail.vue';

  import { columns, searchFormSchema } from './model.data';

  export default defineComponent({
    name: 'DeviceManagement',
    components: { BasicTable, DeviceDrawer, TableAction, DetailModal },
    setup() {
      const go = useGo();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '',
        api: search,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',

          fixed: undefined,
        },
        handleSearchInfoFn(info) {
          const queryParam = {};
          searchFormSchema
            .filter((item) => item.component === 'Input')
            .forEach((p) => {
              const val = info[p.field];
              if (typeof val !== 'undefined' && val !== '') {
                queryParam[p.field] = `%${val}%`;
              }
            });
          return queryParam;
        },
      });

      const [registerModal] = useModal();
      const modalVisible = ref<Boolean>(false);
      const userData = ref<any>(null);

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        remove(record.id).then((res) => {
          console.log('res', res);
          reload();
        });
      }

      function handleSuccess() {
        reload();
      }

      function handleView(record) {
        go('/config/model_detail/' + record.id);
      }

      return {
        userData,
        modalVisible,
        registerTable,
        registerDrawer,
        registerModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleView,
      };
    },
  });
</script>
