import React from 'react';
import { Col, Form, Input, message, Modal, Radio, Row, Select } from 'antd';
import { createEdition, editEdition } from '@/services/EditionManagement';

const { TextArea } = Input;

interface userEditPros {
  visible: boolean;
  editCallback: () => void;
  params: any;
}

/**
 *  版本编辑页面
 * @param props
 * @constructor
 */
const Edit: React.FC<userEditPros> = (props) => {
  const { visible, editCallback, params } = props;
  const [form] = Form.useForm();

  /**
   * 确定
   */
  const onOk = () => {
    form.validateFields().then((values) => {
      if (values) {
        const data = { ...values };
        if (values?.force_update) {
          data.force_update = JSON.parse(values?.force_update);
        }
        if (params) {
          data.record_id = params.record_id;
          //  编辑
          editEdition(data)
            .then((res) => {
              if (res.code === 0) {
                message.success('编辑成功');
                editCallback();
              } else {
                message.error(res?.message);
                editCallback();
              }
            })
            .catch((e) => {
              message.error(e?.message);
              editCallback();
            });
        } else {
          // 新增
          createEdition(data)
            .then((res) => {
              if (res.code === 0) {
                message.success('保存成功');
                editCallback();
              } else {
                message.error(res?.message);
                editCallback();
              }
            })
            .catch((e) => {
              message.error(e?.message);
              editCallback();
            });
        }
      }
    });
  };

  /**
   * 取消
   */
  const onCancel = () => {
    editCallback();
  };

  const formItemLayout = {
    labelCol: {
      span: 6,
    },
    wrapperCol: {
      span: 16,
    },
  };

  return (
    <Modal
      title={`${params ? '编辑' : '新增'}`}
      open={visible}
      onOk={onOk}
      onCancel={onCancel}
      width={800}
    >
      <Form form={form}>
        <Row>
          <Col span={24}>
            <Form.Item
              {...formItemLayout}
              name="version"
              label="版本号"
              rules={[{ required: true, message: '请输入版本号' }]}
              initialValue={params?.version}
            >
              <Input placeholder="请输入版本号" />
            </Form.Item>
          </Col>
          <Col span={24}>
            <Form.Item
              {...formItemLayout}
              name="download_url"
              label="apk下载地址"
              rules={[{ required: true, message: '请输入apk下载地址' }]}
              initialValue={params?.download_url}
            >
              <Input placeholder="请输入apk下载地址" />
            </Form.Item>
          </Col>
          <Col span={24}>
            <Form.Item
              {...formItemLayout}
              name="force_update"
              label="是否强制更新"
              rules={[{ required: true, message: '请选择是否强制更新' }]}
              initialValue={params ? (params?.force_update).toString() : ''}
            >
              <Radio.Group>
                <Radio value="true">强制</Radio>
                <Radio value="false">不强制</Radio>
              </Radio.Group>
            </Form.Item>
          </Col>
          <Col span={24}>
            <Form.Item
              {...formItemLayout}
              name="platform"
              label="设备类型"
              rules={[{ required: true, message: '请选择设备类型' }]}
              initialValue={params?.platform}
            >
              <Select placeholder="请选择设备类型">
                <Select.Option value="IOS">iphone</Select.Option>
                <Select.Option value="ANDROID">安卓</Select.Option>
              </Select>
            </Form.Item>
          </Col>
          <Col span={24}>
            <Form.Item
              {...formItemLayout}
              name="content"
              label="内容"
              rules={[{ required: false, message: '请输入内容' }]}
              initialValue={params?.content ? params?.content : ''}
            >
              <TextArea placeholder="请输入内容" rows={4} />
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </Modal>
  );
};
export default Edit;
