import { PlusOutlined } from '@ant-design/icons';
import { Button, message, Drawer, Popconfirm } from 'antd';
import React, { useState, useRef, useEffect } from 'react';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import ProDescriptions from '@ant-design/pro-descriptions';
import CreateForm from './components/CreateForm';
import api from './service';

/**
 * 更新节点
 *
 * @param fields
 */

const handleUpdate = async (fields) => {
  try {

    await api.updateCompany(fields).then((r) => {
      if (!r.succeeded) {
        message.error(r.errors);
        return false;
      }
      if (r.data) {
        message.success('审核完成');
      } else {
        message.error('审核失败请重试！');
      }
    });
    return true;
  } catch (error) {
    message.error('审核失败请重试！');
    return false;
  }
};

const companyManage = () => {
  /** 新建窗口的弹窗 */
  const [createModalVisible, handleModalVisible] = useState();
  /*菜单授权*/
  const [updateMenuModalVisible, handleUpdateMenuModalVisible] = useState();
  const [stepFormValues, setStepFormValues] = useState({});
  /**详情 */
  const [showDetail, setShowDetail] = useState();
  /*绑定数据 */
  const actionRef = useRef();
  const [currentRow, setCurrentRow] = useState();
  /*选中*/
  const [selectedRowsState, setSelectedRows] = useState();

  //获取功能菜单数据
  const [treeData, setTreeData] = useState();

  const [row, setRow] = useState();

  /** 国际化配置 */
  const columns = [
    {
      title: '主键',
      dataIndex: 'id',
      tip: 'key',
      hideInSearch: true,
      hideInTable: true,
      render: (dom, entity) => {
        return (
          <a
            onClick={() => {
              setCurrentRow(entity);
              setShowDetail(true);
            }}
          >
            {dom}
          </a>
        );
      },
    },
    {
      title: '企业名称',
      dataIndex: 'companyName',
    },
    {
      title: '联系人姓名',
      dataIndex: 'userName',
    },
    {
      title: '电话',
      dataIndex: 'phone',
    },
    {
      title: '状态',
      dataIndex: 'status',
      hideInForm: true,
      valueEnum: {
        0: {
          text: '待审核',
          status: 'Processing',
        },
        1: {
          text: '已审核',
          status: 'Success',
        },
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        record.status === 0 && (
          <a
            onClick={() => {
              handleModalVisible(true);
              setStepFormValues(record);
            }}
          >
            {' '}
            审核
          </a>
      )]
    },
  ];
  return (
    <PageContainer>
      <ProTable
        headerTitle="申请试用列表"
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        request={async (params) => {
          var companyData = [];
          var total = 0;
          await api.companyPage(params).then((r) => {
            companyData = r.data.data;
            total = r.data.total;
          });
          return {
            data: companyData,
            success: true,
            total: total,
          };
        }}
        columns={columns}
        rowSelection={{
          onChange: (_, selectedRows) => {
            setSelectedRows(selectedRows);
          },
        }}
      />
      {selectedRowsState?.length > 0 && (
        <FooterToolbar
          extra={
            <div>
              已选择{' '}
              <a
                style={{
                  fontWeight: 600,
                }}
              >
                {selectedRowsState.length}
              </a>{' '}
              项 &nbsp;&nbsp;
            </div>
          }
        >
        </FooterToolbar>
      )}
      <CreateForm
        onFinish={async (value) => {
          var success = false;
          success = await handleUpdate(value);
          if (success) {
            handleModalVisible(false);
            setCurrentRow(undefined);

            if (actionRef.current) {
              actionRef.current.reload();
            }
          }
          actionRef.current.reload();
        }}
        onCancel={() => {
          handleModalVisible(false);
          setStepFormValues({});
        }}
        modalVisible={createModalVisible}
        values={stepFormValues || {}}
      />

    </PageContainer>
  );
};

export default companyManage;
