import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  useCurrentPermissions,
} from '@/common';
import { MyExport } from '@/components/MyExport';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  AssetProjectsPropertyTypeEnum,
  AssetProjectsStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import AssetInfo from './modals/AssetInfo';
import AssetUpdate from './modals/AssetUpdate';
import HasProjects from './modals/HasProjects';

export default function Index({ title = '项目信息' }) {
  const navigate = useNavigate();
  const [getParams, setParams] = useState({});

  const getCurrentPermissions = useCurrentPermissions();

  let toolBarRender = (action: any) => {
    return getCurrentPermissions({
      export: (
        <MyExport
          key="export"
          item={getParams}
          download={Apis.Asset.AssetProjects}
        />
      ),
      // add: <AssetCreate key="Create" reload={action?.reload} title={'项目'} />,
    });
  };
  let tableRender = (item: any, action: any) => {
    console.log(item, 'item');
    let permissions = getCurrentPermissions({
      config: (
        <MyButtons.View
          title="配置"
          key="configInfo"
          onClick={() => {
            navigate(`/asset/asset_list/show/${item.id}`);
          }}
        />
      ),
      has: (
        <HasProjects
          item={item}
          key="HasProjects"
          reload={action?.reload}
          title="组织"
        />
      ),
      update: (
        <AssetUpdate
          item={item}
          title="编辑项目"
          key="updateInfo"
          reload={action?.reload}
        />
      ),
    });
    return [...permissions];
  };
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="asset-projects"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        // search={false}
        request={async (params, sort) => {
          setParams(params);
          return MyProTableProps.request(
            params,
            sort,
            Apis.Asset.AssetProjects.List,
          );
        }}
        headerTitle="项目列表"
        // tooltip="【项目列表】的【配置】功能，受角色权限控制"
        toolBarRender={(action) => [toolBarRender(action)]}
        columns={[
          MyColumns.ID(),
          // 组织树选择器
          Selects?.OrganizationSearch({
            title: '所属组织',
            key: 'organizations_id',
            colProps: { span: 24 },
            fieldProps: {
              showSearch: true,
            },
            search: {
              transform: (value) => {
                return {
                  organization_name:
                    value.length > 0 ? value[value.length - 1] : '',
                };
              },
            },
          }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: 'name',
            search: false,
            // render: (_, item: any) => (
            //   <AssetInfo item={{ ...item, type: 'link' }} title={item?.name} />
            // ),
          },
          {
            title: '所在城市',
            render: (_, i: any) => {
              return ` ${i?.city || ''} `;
            },
            search: {
              transform: (value) => {
                return { city: value };
              },
            },
          },
          {
            title: '项目地址',
            render: (_, i: any) => {
              return `  ${i?.area || ''}${i?.district || ''}${
                i?.address || ''
              } `;
            },
            search: false,
          },
          MyColumns.EnumTag({
            title: '项目状态',
            dataIndex: 'status',
            valueEnum: AssetProjectsStatusEnum,
            search: false,
          }),
          {
            title: '接管日期',
            dataIndex: 'takeover_date',
            search: false,
          },

          MyColumns.EnumTag({
            title: '项目类型',
            dataIndex: 'property_type',
            valueEnum: AssetProjectsPropertyTypeEnum,
          }),
          {
            title: '接管面积(㎡)',
            dataIndex: 'takeover_building_area',
            render: (_, render: any) => {
              return ` ${
                render?.takeover_building_area
                  ? Math.round(render?.takeover_building_area || '')
                  : ''
              }`;
            },
            search: false,
          },
          {
            title: '所属组织',
            dataIndex: ['organization', 'name'],
            search: false,
          },
          // {
          //   title: '品牌',
          //   dataIndex: ['company_property_brand', 'name'],
          //   search: {
          //     transform: (value) => {
          //       return { property_brand_name: value };
          //     },
          //   },
          // },
          // MyColumns.SoftDelete({
          //   title: '启/禁用',
          //   onRestore: Apis.Asset.AssetProjects.Restore,
          //   onSoftDelete: Apis.Asset.AssetProjects.SoftDelete,
          //   search: false,
          //   setPermissions: getCurrentPermissions({
          //     enableDisable: true,
          //   }),
          // }),
          // MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => {
              console.log(item, 'item2');
              return (
                <Space key={index}>
                  <AssetInfo item={item} />
                  <>{tableRender(item, action)}</>
                </Space>
              );
            },
          }),
        ]}
      />
    </MyPageContainer>
  );
}
