import React, { useState, useEffect } from 'react';
import { Form, Icon, Input, Button, DatePicker, Select, Card, Row, Col, Pagination, Alert, Table, Avatar, Radio, Modal, Descriptions, notification } from 'antd';
import moment from 'moment';
import request from '../../../../../utils/request';
import apis from '../../../../../services/apis';
import Styles from '../style.less';
import ImageUpload from '../../../../../components/XForm/ImageUpload'
import ImageListUpload from '../../../../../components/XForm/ImageListUpload'
import Wangedit from '../../../../../components/Wangedit/Wangedit'


const { Option } = Select;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { Meta } = Card;

const { TextArea } = Input;

const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 2 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 16 },
  },
};

const saleType = [
  {
    id: 1,
    name: '待售',
  },
  {
    id: 2,
    name: '售罄',
  },
  {
    id: 3,
    name: '在售',
  },
]

const houseType = [
  {
    id: '1',
    name: '1室',
  },
  {
    id: '2',
    name: '2室',
  },
  {
    id: '3',
    name: '3室',
  },
  {
    id: '4',
    name: '4室',
  },
  {
    id: '5',
    name: '5室及以上',
  },
]
  
/**
 * 图片信息
 *
 * @param {*} props
 * @returns
 */
class ModalPanoramaImage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
       visibleData: { visible: false, apartmentId: '', buildingId: '',panoramaType: "apartment" },
    }
  }

  // 挂载之后
  // componentDidMount() {
  //
  // }

  componentDidUpdate(preProps, preState) {
    if (this.props.visibleData.visible !== preState.visibleData.visible) {
      console.log(this.props.visibleData)
      this.getById()
      this.setState({ visibleData: this.props.visibleData });
    }
  }

  // 弹框确定按钮
  // eslint-disable-next-line react/sort-comp
  handleOk() {
    this.setState({ visibleData: { visible: false, apartmentId: '', buildingId: '' } })
  }

  // 弹框取消按钮
  handleCancel() {
    // this.setState({ visibleData: { visible: false, apartmentId: '', buildingId: '' } })
    this.props.onSuccess()
  }

  getById(params) {
    const { apartmentId } = this.props.visibleData
    if (apartmentId === '' || apartmentId === undefined) {
      return
    }

    // 网路请求
    request({ ...apis.building.buildingApartmentGetById, urlData: { id: apartmentId }, params: { ...params } }).then(res => {
      // res.img = res.buildingImgList.map(item => item.url)
      if (res.buildingImgList) {
        res.img = res.buildingImgList[0].url
      }
      
      this.props.form.setFieldsValue(res)
    }).catch(err => {
     this.openNotificationWithIcon('error', err)
    })
  }

  openNotificationWithIcon = (type, message) => {
    notification[type]({
      message,
      description:
        '',
    });
  };

  // 提交
  handleSubmit(e) {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        this.submitData(values)
      }
    });
  }

  submitData(data) {
    data.buildingId = this.props.visibleData.buildingId;
    const api = apis.paorama.add;

    // 网路请求
    request({ ...api, data: { ...data } }).then(() => {
      // eslint-disable-next-line no-unused-expressions
      this.openNotificationWithIcon('success', '操作成功')

      // 传递父组件事件
      // onSuccess() 是自定义
      this.props.onSuccess()

      // this.setState({ visibleData: { visible: false, apartmentId: '', buildingId: '' } }, () => console.log('回调：', this.state.visibleData))
    }).catch(err => {
      // eslint-disable-next-line no-unused-expressions
      this.openNotificationWithIcon('error', err)
    })
  }

  radioOnChange(e) {
    this.setState({ visibleData: { ...this.state.visibleData, panoramaType: e.target.value } })
  }

  /**
   * 取消按钮
   *
   * @memberof ModalImage
   */
  closeModal() {
    this.setState({ visibleData: { visible: false, apartmentId: '', buildingId: '' } })
  }

  render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <>
        <Modal
            title="新增全景图"
            width={1100}
            destroyOnClose="true"
            footer={null}
            visible={this.state.visibleData.visible}
            onOk={() => this.handleOk()}
            onCancel={e => this.handleCancel(e)}
          >
            <Form {...formItemLayout} onSubmit={e => this.handleSubmit(e)}>
              <Form.Item label="全景类型" help="全景图展示位置: 项目详情Banner,户型介绍">
                {getFieldDecorator('panoramaType', {
                  rules: [
                    {
                      required: true,
                      message: '请选择全景类型',
                    },
                  ],
                })(
                  <Radio.Group onChange={(e) => this.radioOnChange(e)}>
                    <Radio value="apartment">户型</Radio>
                    <Radio value="building">项目</Radio>
                  </Radio.Group>,
                )}
              </Form.Item>
              { this.state.visibleData.panoramaType == "apartment" && <Form.Item label="全景内容">
                {getFieldDecorator('apartmentId', {
                  rules: [{ required: true, message: '请选择全景内容' }],
                })(
                  <Select placeholder="户型">
                    {
                      (this.state.visibleData.panoramaList || []).map((item, _) => <Option value={item.apartmentId}>{item.apartmentName}</Option>)
                    }
                  </Select>,
                )}
              </Form.Item>}
              { this.state.visibleData.panoramaType == "building" && <Form.Item label="全景内容">
                {getFieldDecorator('content', {
                  rules: [{ required: true, message: '请输入全景内容' }],
                })(
                  <Input />,
                )}
              </Form.Item>}
              <Form.Item label="选择封面" help="建议图片尺寸750px*600px，比例5:4,格式：jpg">
              {getFieldDecorator('coverImg', {
                  rules: [{ required: true, message: '请上传封面图片' }],
              })(
                // <ImageListUpload />,
                <ImageUpload />,
              )}
              </Form.Item>
              <Form.Item label="链接地址">
                {getFieldDecorator('panoramaLink', {
                  rules: [{ required: true, message: '请输入链接地址' }],
                })(<Input />)}
              </Form.Item>
              <Form.Item style={{ width: '400px', margin: 'auto', display: 'flex', justifyContent: 'space-between' }}>
                <Button type="primary" htmlType="submit">保存</Button>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <Button onClick={() => this.handleCancel()}>取消</Button>
              </Form.Item>
            </Form>
        </Modal>
      </>
    );
  }
}

const WrappedModalPanoramaImageForm = Form.create({ name: 'modalPanoramaImage' })(ModalPanoramaImage);

export default WrappedModalPanoramaImageForm
