<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable
      @register="registerTableItem"
      @row-click="handleRowClick"
      class="w-2/5 xl:w-2/5 mr-2"
    >
      <template #toolbar>
        <a-button type="primary" v-auth="'iotgroup:add'" @click="handleItemCreate">{{
          t('新增分组')
        }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          stopButtonPropagation
          :actions="[
            {
              label: '编辑',
              auth: 'iotgroup:edit',
              onClick: handleItemEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'iotgroup:delete',
              color: 'error',
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleItemDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <BasicTable @register="registerTableDetail" class="w-3/5 xl:w-3/5">
      <template #toolbar>
        <a-button type="primary" v-auth="'iotdic:add'" @click="handleDetailCreate">{{
          t('新增字典')
        }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          stopButtonPropagation
          :actions="[
            {
              label: '编辑',
              auth: 'iotdic:edit',
              onClick: handleDetailEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'iotdic:delete',
              color: 'error',
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleDetailDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <IotGroupDrawer @register="registerItemDrawer" @success="handleItemSuccess" />

    <IotDicDrawer @register="registerDetailDrawer" @success="handleDetailSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';

  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';

  import { PageWrapper } from '/@/components/Page';

  import IotGroupDrawer from './components/IotGroupDrawer.vue';
  import IotDicDrawer from './components/IotDicDrawer.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useModal } from '/@/components/Modal';
  import { deleteIotDic, deleteIotGroup, getIotDicPageList, getIotGroupPageList } from '/@/api/iot';
  const { t } = useI18n();

  const staticTransOption = {
    '0': '无',
    '1': '过滤',
    '2': '开方',
    '3': '线性',
    '4': '偏移',
  };

  const staticSaveOption = {
    '0': '无',
    '1': '改变',
    '2': '间隔',
    '3': '每次',
  };
  const searchFormSchemaItem: FormSchema[] = [
    {
      field: 'name',
      label: t('分组名'),
      component: 'Input',
    },
  ];

  const groupColumns: BasicColumn[] = [
    {
      title: t('分组名'),
      dataIndex: 'name',
      resizable: true,
    },
    {
      title: t('排序'),
      dataIndex: 'sortCode',
      resizable: true,
    },
  ];

  const searchFormSchemaDetail: FormSchema[] = [
    {
      field: 'keyword',
      label: t('字典名'),
      component: 'Input',
    },
  ];

  const dicColumns: BasicColumn[] = [
    {
      title: t('字典名'),
      dataIndex: 'name',
      width: 120,
      resizable: true,
    },
    {
      title: t('分组'),
      dataIndex: 'groupName',
      width: 120,
      resizable: true,
    },
    {
      title: t('链接'),
      dataIndex: 'linkName',
      width: 120,
      resizable: true,
    },
    // {
    //   title: t('取值方式'),
    //   dataIndex: 'getStr',
    //   width: 120,
    //   align: 'left',
    // },
    {
      title: t('转换'),
      dataIndex: 'trans',
      width: 120,
      resizable: true,
      customRender: ({ record }) => {
        return record.trans ? staticTransOption[record.trans] : '无';
      },
    },
    {
      title: t('保存类型'),
      resizable: true,
      dataIndex: 'saveType',
      width: 120,
      customRender: ({ record }) => {
        return record.saveType ? staticSaveOption[record.saveType] : '无';
      },
    },
    {
      title: t('对应值'),
      resizable: true,
      dataIndex: 'saveValue',
      width: 120,
    },
  ];
  const { notification } = useMessage();

  const selectItemId = ref('');

  const [registerItemDrawer, { openModal: openItemDrawer }] = useModal();
  const [registerDetailDrawer, { openModal: openDetailDrawer }] = useModal();

  const [registerTableItem, { reload: itemReload }] = useTable({
    title: t('分组'),
    api: getIotGroupPageList,
    rowKey: 'id',
    columns: groupColumns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchemaItem,
      showResetButton: false,
    },
    rowSelection: {
      type: 'radio',
      onChange: onSelectChange,
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 120,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    tableSetting: {
      size: false,
    },
  });
  const [registerTableDetail, { reload: detailReload }] = useTable({
    title: t('字典'),
    api: getIotDicPageList,
    rowKey: 'id',
    columns: dicColumns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchemaDetail,
      showResetButton: false,
    },
    beforeFetch: (params) => {
      //发送请求默认新增  左边树结构所选机构id
      return { ...params, groupId: selectItemId.value };
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 120,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    tableSetting: {
      size: false,
    },
  });

  function handleRowClick(record: any) {
    selectItemId.value = record.id;
    detailReload();
  }
  function onSelectChange(rowKey: string[]) {
    selectItemId.value = rowKey[0];
    detailReload();
  }
  function handleItemCreate() {
    openItemDrawer(true, {
      isUpdate: false,
    });
  }

  function handleDetailCreate() {
    if (!selectItemId.value) {
      notification.warning({
        message: t('提示'),
        description: t('没有选择任意一个分组！'),
      });
      return;
    }

    openDetailDrawer(true, {
      groupId: selectItemId.value,
      isUpdate: false,
    });
  }

  function handleItemEdit(record: Recordable) {
    console.log(record);
    openItemDrawer(true, {
      record,
      isUpdate: true,
    });
  }
  function handleDetailEdit(record: Recordable) {
    console.log(record);
    openDetailDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  function handleItemDelete(record: Recordable) {
    deleteIotGroup([record.id]).then((_) => {
      itemReload();
      notification.success({
        message: t('提示'),
        description: t('删除成功'),
      }); //提示消息
    });
  }

  function handleDetailDelete(record: Recordable) {
    deleteIotDic([record.id]).then((_) => {
      detailReload();
      notification.success({
        message: t('提示'),
        description: t('删除成功'),
      }); //提示消息
    });
  }

  function handleItemSuccess() {
    itemReload();
  }

  function handleDetailSuccess() {
    detailReload();
  }
</script>
<style scoped>
  :deep(.vben-basic-table-header__toolbar) {
    min-width: auto;
  }
</style>
