import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { SelectContract } from '@/components/SelectContract';
import { Apis } from '@/gen/Apis';
import {
  ContractTemplatesIncomeExpenseTypeEnum,
  ContractTemplatesSourceEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Create from './modals/Create';
import Show from './modals/Show';
import Update from './modals/Update';

export default function Index({ title = '合同模版' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="contract_templates_list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Contract.ContractTemplates.List,
          )
        }
        headerTitle="合同模版列表"
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              add: (
                <Create key="Create" reload={action?.reload} title={title} />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          MyColumns.EnumTag({
            title: '收支类型',
            dataIndex: 'income_expense_type',
            valueEnum: ContractTemplatesIncomeExpenseTypeEnum,
            search: false,
          }),
          SelectContract.ContractTypes({
            title: '合同类型',
            key: 'contract_types_id',
            colProps: { span: 12 },
            fieldProps: {
              placeholder: '请选择合同类型',
            },
            hidden: true,
          }),
          {
            title: '合同类型',
            dataIndex: ['contract_type', 'name'],
            // search: { transform: (value) => ({ type_name: value }) },
            // sorter: true,
            search: false,
          },
          {
            title: '模板名称',
            dataIndex: 'name',
            sorter: true,
          },
          {
            title: '模板编号',
            dataIndex: 'code',
            sorter: true,
          },
          {
            title: '模板描述',
            dataIndex: 'description',
            width: 200,
            search: false,
          },
          MyColumns.EnumTag({
            title: '来源',
            dataIndex: 'source',
            valueEnum: ContractTemplatesSourceEnum,
          }),
          MyColumns.Boolean({
            title: '是否启用',
            dataIndex: 'is_enabled',
            search: false,
          }),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                {item.is_enabled === 1 && (
                  <Show item={item} reload={action?.reload} title="查看" />
                )}
                <MyTableActions
                  actions={{
                    update: (
                      <Update
                        item={item}
                        reload={action?.reload}
                        title={title}
                      />
                    ),
                    enabled: (
                      <MyButtons.Default
                        title={item.is_enabled === 1 ? '禁用' : '启用'}
                        type={item.is_enabled === 1 ? 'default' : 'primary'}
                        danger={item.is_enabled === 1}
                        isConfirm={true}
                        description={
                          item.is_enabled === 1
                            ? `确定要禁用模板「${item.name}」吗？`
                            : `确定要启用模板「${item.name}」吗？`
                        }
                        onConfirm={() =>
                          Apis.Contract.ContractTemplates.Update({
                            id: item.id ?? 0,
                            is_enabled: item.is_enabled === 1 ? 0 : 1,
                            name: item.name,
                            code: item.code,
                            source: item.source,
                            income_expense_type: item.income_expense_type,
                            contract_types_id: item.contract_types_id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                    delete: (
                      <MyButtons.Delete
                        onConfirm={() =>
                          Apis.Contract.ContractTemplates.Delete({
                            id: item.id,
                          }).then(() => action?.reload())
                        }
                      />
                    ),
                  }}
                  maxVisible={3}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
