<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <BasicTable @register="registerTable" v-if="!isView">
      <template #toolbar>
        <a-button type="primary" @click.stop="handleAdd" v-auth="'order:add'">{{
          t('新增')
        }}</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'state'">
          <a-switch v-model:checked="record.state" :checkedValue="1" :unCheckedValue="0" disabled />
        </template>
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'order:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                auth: 'order:delete',
                color: 'error',
                onClick: handleDelete.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>

    <Form v-if="data.visible" @cancel="cancelForm" :data="data.editData" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { getOrderList, getOrderDetail, deleteOrder } from './api/index';
  import Form from './Form.vue';
  import { useI18n } from '/@/hooks/web/useI18n';

  import { usePermission } from '/@/hooks/web/usePermission';

  const { t } = useI18n();
  const columns: BasicColumn[] = [
    {
      title: t('订单名称'),
      dataIndex: 'OrderName',
    },
    {
      title: t('订单编码'),
      dataIndex: 'OrderNum',
      width: 120,
    },
    {
      title: t('订单总金额'),
      dataIndex: 'OrderPriceCount',
      width: 120,
    },
    {
      title: t('负责人'),
      dataIndex: 'OrderManagerName',
      width: 120,
    },
  ];

  const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: t('关键字'),
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请输入关键字',
      },
    },
    {
      field: 'userID',
      label: t('负责人'),
      component: 'User',
      colProps: { span: 8 },
      componentProps: {
        placeholder: '请选择负责人',
        getPopupContainer: () => document.body,
      },
    },
  ];
  interface DataItem {
    OrderNum: number;
    OrderName: string;
    OrderPriceCount: number;
    OrderManager: string;
  }
  const defaultFormData: DataItem = {
    OrderNum: 0,
    OrderName: '',
    OrderPriceCount: 0,
    OrderManager: '',
  };
  let data: {
    visible: boolean;
    dataSource: Array<DataItem>;
    editData: DataItem;
    params: {
      keyword: string;
      userID: string;
    };
  } = reactive({
    visible: false,
    dataSource: [],
    editData: defaultFormData,
    params: {
      keyword: '',
      userID: '',
    },
  });
  const { notification } = useMessage();
  const { hasPermission } = usePermission();

  const isView = ref(false);

  const customRow = (record) => {
    return {
      onClick: () => {
        let selectedRowKeys = [...getSelectRowKeys()];
        if (selectedRowKeys.indexOf(record.id) >= 0) {
          let index = selectedRowKeys.indexOf(record.id);
          selectedRowKeys.splice(index, 1);
        } else {
          selectedRowKeys.push(record.id);
        }
        setSelectedRowKeys(selectedRowKeys);
      },
      ondblclick: () => {
        if (hasPermission('materialList:edit')) {
          handleEdit(record);
        }
      },
    };
  };

  const [registerTable, { reload, setSelectedRowKeys, getSelectRowKeys }] = useTable({
    title: '订单列表',
    api: getOrderList,
    rowKey: 'OrderID',
    columns,
    formConfig: {
      rowProps: {
        gutter: 16,
      },
      schemas: searchFormSchema,
      showResetButton: false,
    },
    useSearchForm: true,
    showTableSetting: true,
    striped: false,
    actionColumn: {
      width: 88,
      title: t('操作'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    rowSelection: {
      type: 'checkbox',
    },
    customRow,
    tableSetting: {
      size: false,
    },
  });

  function handleAdd() {
    data.editData = defaultFormData;
    data.visible = true;
  }
  async function handleEdit(record) {
    try {
      let res = await getOrderDetail(record.OrderID);
      data.editData = res;
      data.visible = true;
    } catch (error) {}
  }
  async function handleDelete(record) {
    try {
      let res = await deleteOrder(record.OrderID);
      if (res) {
        notification.success({
          message: '提示',
          description: '删除成功',
        }); //提示消息
        cancelForm();
      } else {
        notification.error({
          message: '提示',
          description: '删除失败',
        }); //提示消息
      }
    } catch (error) {}
  }
  function cancelForm() {
    data.visible = false;
    reload();
  }
</script>
