import styles from '../../../GLQ/index.less';
import { useEffect, useState } from 'react';
import { ConfigProvider, Pagination, Table, Upload, UploadProps, message } from 'antd';

import { rowClassName } from '@/utils';
import ButtonComp from '@/components/ButtonComp';
import { upholdAlgorithmInfoDelOrRestore, upholdAlgorithmInfoEditOrAdd, upholdAlgorithmInfoList } from '@/services/sf';
import { testData } from './testData';

export default function Page() {
  const [tableData, setTableData] = useState([]);
  const [pageNumber, setPageNumber] = useState(1);
  const [pageSize, setpageSize] = useState(10);
  const [total, setTotal] = useState(0);
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);

  useEffect(() => {
    getList();
    setSelectedRowKeys([])
  }, [pageNumber]);

  const columns: any = [
    {
      title: '序号', key: 'index', align: 'center', width: 80,
      render: (a: any, b: any, c: any) => {
        return <span>{(pageNumber - 1) * pageSize + c + 1}</span>;
      },
    },
    { title: '密码算法简称', dataIndex: 'algorithmShortName', key: 'algorithmShortName', align: 'center' },
    { title: '算法标识', dataIndex: 'algorithmName', key: 'algorithmName', align: 'center' },
    { title: '密码类型', dataIndex: 'pwdType', key: 'pwdType', align: 'center' },
    { title: '算法用途', dataIndex: 'algorithmUsage', key: 'algorithmUsage', align: 'center' },
    { title: '算法体制', dataIndex: 'algorithmStyle', key: 'algorithmStyle', align: 'center' },
    { title: '算法模式简称', dataIndex: 'algorithmModelShortName', key: 'algorithmModelShortName', align: 'center' },
    { title: '参数简称', dataIndex: 'argsShortName', key: 'argsShortName', align: 'center' },
    { title: '信息长度', dataIndex: 'infoSize', key: 'infoSize', align: 'center' },
    { title: '工作模式', dataIndex: 'workModel', key: 'workModel', align: 'center' },
    { title: '秘钥长度', dataIndex: 'pwdSize', key: 'pwdSize', align: 'center' },
    {
      title: '使用状态', dataIndex: 'useFlag', key: 'useFlag', align: 'center',
      render: (val: any) => { return <span>{val == 1 ? '已使用' : '未使用'}</span>}
    },
    {
      title: '删除状态', dataIndex: 'delFlag', key: 'delFlag', align: 'center',
      render: (val: any) => { return <span>{val == 1 ? '已删除' : '未删除'}</span>}
    }
  ]

  const updataList = () => {
    pageNumber == 1 ? getList() : setPageNumber(1);
  }

  const getList = () => {
    upholdAlgorithmInfoList({ pageNumber, pageSize }).then((res) => {
      if (res?.result == "success") {
        setTotal(res.data[0].total);
        setTableData(res.data[0].list);
      } else {
        message.error(res?.errorMsg);
      }
    })
  }

  const props: UploadProps = {
    maxCount: 1,
    beforeUpload: (file: any) => {
      if (file.name == 'aaaa.zip') {
        let promises = testData.map(async item => {
          const res = await upholdAlgorithmInfoEditOrAdd(item);
          if (res?.result === "success") {
            return 1;
          } else {
            message.error(res?.errorMsg);
            return 0;
          }
        });

        Promise.all(promises).then(results => {
          let recordCount = results.reduce((acc: any, val: any) => acc + val, 0);
          if (recordCount === testData.length) {
            message.success('导入成功');
            updataList()
          }
        });
      } else {
        message.error('导入失败');
      }
      return
    }
  };

  const pageOnChange = (pageNumber: number) => {
    setPageNumber(pageNumber);
  }

  const onShowSizeChange = (current: any, pageSize: any) => {
    setpageSize(pageSize);
    getList();
  }

  const sending = (delStatus: number) => {
    if (selectedRowKeys.length == 0) {
      message.info('请勾选数据');
      return
    }

    upholdAlgorithmInfoDelOrRestore({ ids: selectedRowKeys, delStatus }).then((res) => {
      if (res?.result === "success") {
        message.success((delStatus == 1 ? '删除' : '还原') + '成功');
        pageNumber == 1 ? getList() : setPageNumber(1);
        setSelectedRowKeys([]);
      } else {
        message.error(res?.errorMsg);
      }
    });
  }

  return (
    <div className={`${styles.params_warp}`}>
      <ButtonComp style={{ marginBottom: 30 }} type={'special'} text={'算法信息维护'} onClick={() => { }} />
      <Table
        scroll={tableData.length > 0 ? { y: 41 * 10 } : {}}
        pagination={false}
        bordered
        columns={columns}
        dataSource={tableData}
        rowKey={(record: any) => record?.id}
        rowClassName={rowClassName}
        rowSelection={{
          selectedRowKeys,
          onChange: (selectedKeys: any) => {
            setSelectedRowKeys(selectedKeys);
          }
        }}
      />

      {total > 0 && <div className='flex_aiC_jB mt20'>
        <div>共 {total} 条</div>
        <Pagination
          current={pageNumber}
          pageSize={pageSize}
          total={total}
          showQuickJumper
          onChange={pageOnChange}
          onShowSizeChange={onShowSizeChange}
        />
      </div>}

      <div className='flex_aiC_jB mt30'>
        <Upload {...props} showUploadList={false}>
          <ButtonComp text={'导入算法'} />
        </Upload>
        <div className='flex_aiC_jB'>
          <ButtonComp style={{ marginRight: 20 }} text={'查询'} onClick={() => { }} />
          <ButtonComp style={{ marginRight: 20 }} type={'delete'} text={'删除'} onClick={() => sending(1)} />
          <ButtonComp style={{ marginRight: 20 }} type={'cancel'} text={'恢复'} onClick={() => sending(0)} />
          <ButtonComp type={'cancel'} text={'清理'} onClick={() => { }} />
        </div>
      </div>
    </div>
  );
}