import type { ActionType, ProColumns, ProFormInstance } from '@ant-design/pro-components';
import { ModalForm, PageContainer, ProFormSelect, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { Select } from 'antd';
import dayjs from 'dayjs';
import React, { useRef, useState } from 'react';

import Previewer from '@/components/previewer';
import { CITY_CODE_TO_NAME_MAP, CITY_OPTIONS } from '@/constants/city';
import { TIME_FORMAT } from '@/constants/global';
import { getMaterialList, updateMaterialInfo } from '@/services/list';

const STATUS_OPTIONS = [
  { label: '开放', value: true },
  { label: '封禁', value: false },
];

const TableList: React.FC = () => {
  const [updateModalOpen, handleUpdateModalOpen] = useState<boolean>(false);
  const [currentRow, setCurrentRow] = useState<API.MaterialListItem>();
  const actionRef = useRef<ActionType>();
  const formRef = useRef<ProFormInstance>();

  const columns: ProColumns<API.MaterialListItem>[] = [
    {
      title: '视频',
      dataIndex: 'userId',
      valueType: 'textarea',
      copyable: true,
      search: false,
      render(_dom, entity) {
        return <Previewer videos={entity.materialVideoInfoList} />;
      },
    },
    {
      title: '主播ID',
      dataIndex: 'userId',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '模卡昵称',
      dataIndex: 'name',
      valueType: 'textarea',
    },
    {
      title: '主播昵称',
      dataIndex: 'nickname',
      valueType: 'textarea',
      search: false,
    },
    {
      title: '自身优势',
      dataIndex: 'advantages',
      valueType: 'textarea',
      search: false,
      width: 200,
    },
    {
      title: '模卡状态',
      dataIndex: 'isOpen',
      valueType: 'textarea',
      renderText(status: boolean) {
        return STATUS_OPTIONS.find(option => option.value === status)?.label;
      },
      renderFormItem() {
        return <Select showSearch allowClear options={STATUS_OPTIONS} />;
      },
      search: false,
    },
    {
      title: '主播模卡状态',
      dataIndex: 'userOpen',
      valueType: 'textarea',
      renderText(status: boolean) {
        return STATUS_OPTIONS.find(option => option.value === status)?.label;
      },
      renderFormItem() {
        return <Select showSearch allowClear options={STATUS_OPTIONS} />;
      },
    },
    {
      title: '后台模卡状态',
      dataIndex: 'adminOpen',
      valueType: 'textarea',
      renderText(status: boolean) {
        return STATUS_OPTIONS.find(option => option.value === status)?.label;
      },
      renderFormItem() {
        return <Select showSearch allowClear options={STATUS_OPTIONS} />;
      },
    },
    {
      title: '意向城市',
      dataIndex: 'cityCode',
      valueType: 'textarea',
      renderText(cityCode: string) {
        return CITY_CODE_TO_NAME_MAP.get(cityCode);
      },
      renderFormItem() {
        return (
          <Select
            showSearch
            allowClear
            options={CITY_OPTIONS}
            filterOption={(input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase())}
          />
        );
      },
    },
    {
      title: '播过的品类',
      dataIndex: 'workedSecCategoryStr',
      valueType: 'textarea',
      search: false,
      width: 200,
    },
    {
      title: '创建时间',
      dataIndex: 'created',
      valueType: 'dateTime',
      renderText(created: string) {
        return dayjs(Number(created)).format(TIME_FORMAT);
      },
      search: false,
      sorter: true,
    },
    {
      title: '修改时间',
      dataIndex: 'updated',
      valueType: 'dateTime',
      renderText(created: string) {
        return dayjs(Number(created)).format(TIME_FORMAT);
      },
      search: false,
      sorter: true,
    },
    {
      title: '操作',
      valueType: 'option',
      fixed: 'right',
      width: 100,
      align: 'center',
      render: (_, record) => (
        <a
          key="config"
          onClick={() => {
            handleUpdateModalOpen(true);
            setCurrentRow(record);
          }}
        >
          修改
        </a>
      ),
    },
  ];
  return (
    <PageContainer>
      <ProTable<API.MaterialListItem, API.PageParams>
        headerTitle="查询表格"
        actionRef={actionRef}
        rowKey="key"
        search={{ labelWidth: 120, collapsed: false, collapseRender: false }}
        request={getMaterialList}
        columns={columns}
        scroll={{ x: 'max-content' }}
      />
      <ModalForm
        title="更新模卡信息"
        width="400px"
        formRef={formRef}
        open={updateModalOpen}
        onOpenChange={handleUpdateModalOpen}
        onFinish={async data => {
          const params: API.UpdateMaterialParams = {
            id: currentRow!.id,
            adminOpen: Number(data.adminOpen),
          };
          console.log('update confirm', data, params);
          try {
            await updateMaterialInfo(params);
            actionRef.current?.reload();
            formRef.current?.resetFields();
          } catch (e) {}
          handleUpdateModalOpen(false);
        }}
      >
        <ProFormSelect
          name="adminOpen"
          label="后台模卡状态"
          options={STATUS_OPTIONS as any}
          rules={[{ required: true, message: '必填项' }]}
        />
      </ModalForm>
    </PageContainer>
  );
};
export default TableList;
