import React, { useEffect, useState, useRef } from 'react';
import {
  ProForm,
  ProFormDigit,
  ProFormText,
  ProFormRadio,
  ProFormSelect,
  ProFormCascader,
  ProFormTextArea,
  EditableProTable,
  ActionType
} from '@ant-design/pro-components';
import { Form, Modal, Input, TimePicker } from 'antd';
import { useIntl, FormattedMessage } from '@umijs/max';
import { getMerchantSelect, getSiteTypeSelect } from '@/services/merchant/index'


const UserForm: React.FC = (props: any) => {
  const actionRef = useRef<ActionType>();


  const [form] = Form.useForm();

  const { values } = props;

  const [defaultData, setDefaultData] = useState<readonly any[]>([])
  const [editableKeys, setEditableRowKeys] = useState<React.Key[]>(() =>
    defaultData.map((item) => item.id),
  );


  const columns = [
    {
      title: '开始时间',
      dataIndex: 'startTime',
      // valueType: 'time',
      render: (_) => {
        return _
      }
    },
    {
      title: '结束时间',
      dataIndex: 'endTime',
      valueType: 'time',
      render: (_) => {
        return _
      }
    },
    {
      title: '模板类型',
      dataIndex: 'type',
      valueType: 'select',
      fieldProps: {
        options: [
          {
            label: '尖',
            value: 1,
          },
          {
            label: '峰',
            value: 2,
          },
          {
            label: '平',
            value: 3,
          },
          {
            label: '谷',
            value: 4,
          },
        ],
      },
    },
  ];

  useEffect(() => {
    if (values.segmentList) {
      values.priceTemplateSegmentList = values.segmentList
      setDefaultData(values.priceTemplateSegmentList)
    }


    form.resetFields();
    form.setFieldsValue(values);
  }, [form, props]);



  const intl = useIntl();
  const handleOk = () => {
    form.submit();
  };
  const handleCancel = () => {
    props.onCancel();
  };
  const handleFinish = async (values: any) => {
    console.log(values, 'priceTemplateSegmentAddParamsList');

    if (values.priceTemplateSegmentAddParamsList) {
      values.priceTemplateSegmentAddParamsList.map((i) => {
        if (i.id.includes('AAA')) {
          delete i.id
        }
      })
    } else {
      values.priceTemplateSegmentAddParamsList = defaultData
    }
    if (values.id) {
      values.priceTemplateSegmentUpdateParams = values.priceTemplateSegmentAddParamsList
    }
    props.onSubmit(values);
  };

  return (
    <Modal
      width={800}
      title={'价格模板信息'}
      open={props.open}
      forceRender
      destroyOnClose
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <ProForm
        form={form}
        grid={true}
        submitter={false}
        layout="horizontal"
        onFinish={handleFinish}>
        <ProFormDigit
          name="id"
          label={'ID'}
          disabled
          hidden={true}
        />

        <ProForm.Group>
          <ProFormSelect
            name="merchantId"
            label={'选择商户'}
            colProps={{ md: 8, xl: 8 }}
            placeholder="请选择商户"
            disabled
            fieldProps={
              {
                fieldNames: {
                  label: 'label',
                  value: 'id'
                },
              }
            }
            request={async () => {
              let { data } = await getMerchantSelect()
              return data
            }}
          />
        </ProForm.Group>


        <ProForm.Group>
          <ProFormText
            name="templateName"
            label={'模板名字'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入模板名字"
            disabled
          />
        </ProForm.Group>
        <ProForm.Group>

          <ProFormText
            name="tipElectricityRate"
            label={'尖电费'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入尖电费"
            disabled
          />
          <ProFormText
            name="tipServiceFeeRate"
            label={'尖服务费'}
            colProps={{ md: 8, xl: 8 }}
            placeholder="请输入尖服务费"
            disabled
          />
          <ProFormText
            name="peakElectricityRate"
            label={'峰电费'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入峰电费"
            disabled
          />
          <ProFormText
            name="peakServiceFeeRate"
            label={'峰服务费'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入峰服务费"
            disabled
          />
          <ProFormText
            name="flatElectricityRate"
            label={'平电费'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入平电费"
            disabled
          />
          <ProFormText
            name="flatServiceFeeRate"
            label={'平服务费'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入平服务费"
            disabled
          />
          <ProFormText
            name="valleyElectricityRate"
            label={'谷电费'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入谷电费"
            disabled
          />
          <ProFormText
            name="valleyServiceFeeRate"
            label={'谷服务费'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入谷服务费"
            disabled
          />
        </ProForm.Group>
        <ProForm.Group>
          <ProFormText
            name="parkCostInfo"
            label={'停车收费说明'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入停车收费说明"
            disabled
          />
          <ProFormText
            name="occupyCostInfo"
            label={'占用收费说明'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入	占用收费说明"
            disabled
          />
        </ProForm.Group>
        <ProFormRadio.Group
          name="status"
          disabled
          options={[
            {
              label: '启用',
              value: 1,
            },
            {
              label: '禁用',
              value: 2,
            },
          ]}
          label={'状态'}
          colProps={{ md: 12, xl: 12 }}
        />
        <ProForm.Item
          label="价格模板"
          name="priceTemplateSegmentAddParamsList"
          trigger="onValuesChange"
        >
          <EditableProTable
            rowKey="id"
            toolBarRender={false}
            columns={columns}
            actionRef={actionRef}
            value={defaultData}
            onChange={setDefaultData}
          />
        </ProForm.Item>
      </ProForm>
    </Modal>
  );
};

export default UserForm;
