<template>
  <ResizePageWrapper>
    <template #resizeLeft>
      <BasicTree
        search
        toolbar
        :title="t('类别')"
        :clickRowToExpand="true"
        defaultExpandAll
        :treeData="treeData"
        :selectedKeys="selectedKeys"
        @select="handleSelect"
      />
    </template>

    <template #resizeRight>
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="primary" v-auth="'store:add'" @click="handleCreate">{{
            t('新增')
          }}</a-button>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex == 'type'">
            <span :style="getType('style', record.type)">{{ getType('text', record.type) }}</span>
          </template>
          <template v-if="column.dataIndex == 'action'">
            <TableAction
              :actions="[
                {
                  label: '编辑',
                  auth: 'store:edit',
                  onClick: handleEdit.bind(null, record),
                },
                {
                  label: '删除',
                  auth: 'store:delete',
                  color: 'error',
                  onClick: handleDelete.bind(null, record),
                },
              ]"
            />
          </template>
        </template>
      </BasicTable>
    </template>
    <ManagementModal @register="registerModal" @success="reload" />
  </ResizePageWrapper>
</template>
<script lang="ts" setup>
  import { ref, createVNode } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTree, TreeItem } from '/@/components/Tree';
  import { Modal } from 'ant-design-vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import { getStoreManagementPageList, deleteStoreManagement } from '/@/api/erp/store/management';
  import { useModal } from '/@/components/Modal';
  import ManagementModal from './components/ManagementModal.vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { ResizePageWrapper } from '/@/components/Page';
  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';
  import { usePermission } from '/@/hooks/web/usePermission';

  const { t } = useI18n();
  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: '',
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入单据编号或名称',
      },
    },
  ];

  const columns: BasicColumn[] = [
    {
      title: '单据编号',
      dataIndex: 'code',
      resizable: true,
    },
    {
      title: '单据名称',
      dataIndex: 'name',
      resizable: true,
    },
    {
      title: '单据类型',
      dataIndex: 'type',
      resizable: true,
    },
    {
      title: '时间',
      dataIndex: 'createDate',
      resizable: true,
    },
    {
      title: '经手人员',
      dataIndex: 'personNames',
      resizable: true,
    },
    {
      title: t('备注'),
      dataIndex: 'remark',
      resizable: true,
    },
  ];

  const treeData = ref<TreeItem[]>([
    {
      title: '全部',
      key: 'all',
      children: [
        {
          title: '入库单',
          key: 1,
        },
        {
          title: '出库单',
          key: 0,
        },
      ],
    },
  ]);

  const { notification } = useMessage();
  const { hasPermission } = usePermission();

  const type = ref('');
  const selectedKeys = ref<string[]>(['all']);

  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: '仓库管理',
    api: getStoreManagementPageList,
    rowKey: 'id',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    beforeFetch: (params) => {
      return { ...params, type: type.value };
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 88,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    customRow: (record) => {
      return {
        ondblclick: () => {
          if (hasPermission('store:edit')) {
            handleEdit(record);
          }
        },
      };
    },
    tableSetting: {
      size: false,
    },
  });

  const getType = (type, record) => {
    if (type === 'style') {
      return { color: record === 1 ? '#5e95ff' : '#f59a23' };
    } else {
      return record === 1 ? '入库单' : '出库单';
    }
  };

  const handleCreate = () => {
    openModal(true, {
      isUpdate: false,
    });
  };
  0;
  const handleEdit = (record: Recordable) => {
    openModal(true, {
      id: record.id,
      isUpdate: true,
    });
  };

  const handleDelete = (record) => {
    Modal.confirm({
      title: t('提示信息'),
      icon: createVNode(ExclamationCircleOutlined),
      content: t('是否确认删除？'),
      okText: t('确认'),
      cancelText: t('取消'),
      async onOk() {
        await deleteStoreManagement(record.id);
        notification.success({
          message: t('提示'),
          description: t('删除成功'),
        });
        reload();
      },
      onCancel() {},
    });
  };

  const handleSelect = (typeKey) => {
    type.value = typeKey[0] === 'all' ? '' : typeKey[0];
    reload();
  };
</script>
<style lang="less" scoped>
  .left-box {
    margin-right: 8px;
    overflow: hidden;
    background: #fff;
    height: 100%;
  }
</style>
