import { useEffect, useState } from 'react';
import SearchBox from '~/components/search-box';
import { Button, Table } from 'antd';
import { ColumnsType } from 'antd/es/table';
import { ChangeModal } from '~/pages/customManage/customList/comp/changeModal';
import { InterListType, InterReqType } from '~/api/interface';
import { listAppUserType } from '~/api/interface/customManageType';
import { CustomManageAPI } from '~/api';

// 表格数据类型
type TableType = InterListType<listAppUserType>;
// 请求数据的类型
type ReqType = InterReqType<listAppUserType>;
// 搜索表单的数据
let query: ReqType = {};
//来源列表
const portTypeList = [
  { value: 100, label: '自然流' },
  { value: 200, label: '海报' },
  { value: 300, label: '抖音' },
  { value: 400, label: '公众号' },
  { value: 500, label: '社群' },
  { value: 600, label: '招投标' },
];

function CustomListView() {
  // 是否打开变更弹窗
  const [isChangeVisModal, setIsChangeVisModal] = useState<boolean>(false);
  // 表格分页配置
  const [pagination, setPagination] = useState({
    total: 0,
    pageSize: 10,
    current: 1,
    totalPage: 0,
  });
  // 表格数据
  const [tableData, setTableData] = useState<TableType>([]);
  // 需要编辑的数据
  const [editData] = useState<TableType[0]>();
  // 加载列表
  const getTableList = async (value = {}) => {
    // 只需要修改这个地方的接口即可
    const res = await CustomManageAPI.listAppUser({
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
      ...value,
      ...query,
    });
    if (res && res.code === '200') {
      const { list, pageNo, totalCount, pageSize, totalPage } = res.result; // 解构
      setPagination({
        total: totalCount,
        current: pageNo,
        pageSize,
        totalPage,
      });
      setTableData(list);
      // console.log('加载列表 --->', list);
    }
  };
  // 翻页
  const paginationChange = (pageNo: number, pageSize: number) => {
    getTableList({ pageNo, pageSize }).then();
  };
  // 表单提交
  const onFinish = (data: ReqType) => {
    pagination.current = 1;
    query = data;
    getTableList(data).then();
  };
  // componentDidMount
  useEffect(() => {
    query = {};
    getTableList().then();
  }, []);
  // 表格结构
  const columns: ColumnsType<TableType[0]> = [
    {
      title: '用户UID',
      dataIndex: 'uid',
      align: 'center',
      width: '100px',
      render: (text) => `UID${text}`,
    },
    {
      title: '用户名称',
      dataIndex: 'userName',
      align: 'center',
      render: (text, record) => text || record.nickName,
    },
    {
      title: '手机号',
      dataIndex: 'phoneNum',
      align: 'center',
    },
    {
      title: '认证企业',
      dataIndex: 'companyName',
      align: 'center',
    },
    {
      title: '企业认证',
      dataIndex: 'companyAuthStatus',
      align: 'center',
      render: (text) => (text === 0 ? '未认证' : '已认证'),
    },
    {
      title: '电子签约认证',
      dataIndex: 'userName',
      align: 'center',
      render: (_text) => `未认证`,
    },
    {
      title: '用户来源',
      dataIndex: 'portType',
      align: 'center',
      render: (text) => portTypeList.find((i) => i.value === text)?.label || text,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      align: 'center',
    },
    {
      title: '账号类型',
      dataIndex: 'phoneNum',
      align: 'center',
      render: (text) => (text ? '普通用户' : '游客用户'),
    },
    {
      title: '渠道等级',
      dataIndex: 'cooperationTagId',
      align: 'center',
      render: (text) => text,
    },
    {
      title: '上级推荐人',
      dataIndex: 'remark',
      align: 'center',
      render: (_text) => `--`,
    },
    {
      title: '推荐伙伴',
      dataIndex: 'remark',
      align: 'center',
      render: (_text) => `--`,
    },
    {
      title: '相关运营',
      dataIndex: 'remark',
      align: 'center',
      render: (_text) => `--`,
    },
    {
      title: '相关销售',
      dataIndex: 'remark',
      align: 'center',
      render: (_text) => `--`,
    },
    {
      title: '操作',
      dataIndex: 'action',
      align: 'center',
      fixed: 'right',
      width: '150px',
      render: (_text) => (
        <>
          <Button
            type={'link'}
            onClick={() => {
              setIsChangeVisModal(true);
            }}
          >
            变更
          </Button>
          <Button type={'link'} danger>
            删除
          </Button>
        </>
      ),
    },
  ];
  return (
    <>
      <SearchBox
        search={[
          {
            label: '用户名称',
            name: 'keyword',
            type: 'input',
            placeholder: '请输入UID、用户名称',
          },
          {
            label: '手机号',
            name: 'phoneNum',
            type: 'input',
            placeholder: '请输入用户账号',
          },
          {
            label: '来源',
            name: 'form',
            type: 'Select',
            placeholder: '请选择相关来源',
            options: portTypeList,
          },
          {
            label: '创建时间',
            name: 'time',
            type: 'rangePicker',
            placeholder: '请选择创建时间',
          },
          {
            label: '企业认证',
            name: 'enterprise',
            type: 'Select',
            placeholder: '请选择是否认证',
            options: [
              { value: 1, label: '未认证' },
              { value: 2, label: '已认证' },
            ],
          },
          {
            label: '电子签约认证',
            name: 'contract',
            type: 'Select',
            placeholder: '请选择是否认证',
            options: [
              { value: 1, label: '未认证' },
              { value: 2, label: '已认证' },
            ],
          },
        ]}
        searchData={onFinish}
      />
      <Table
        size='small'
        dataSource={tableData}
        columns={columns}
        rowKey='id'
        scroll={{ x: 1200 }}
        pagination={{
          total: pagination.total,
          pageSize: pagination.pageSize,
          current: pagination.current,
          showSizeChanger: true,
          showQuickJumper: true,
          onChange: (page: number, pageSize: number) => paginationChange(page, pageSize),
          showTotal: (total, range) => `当前 ${range[0]}-${range[1]} 条记录 / 共 ${total} 条数据`,
        }}
        // rowSelection={{ selectedRowKeys, onChange: onSelectChange }}
      />
      <ChangeModal
        visible={isChangeVisModal}
        closed={() => {
          setIsChangeVisModal(false);
        }}
        data={editData}
        state={{ levelTags: [] }}
      />
    </>
  );
}

export default CustomListView;
