import React, { useState } from 'react';
import { Form, Button, Modal, Row, Col, Card } from 'antd';
import { SysModel } from '../../../../utils';
import Swiper from '../../../index';
import Group from '../../../../components/group';
import { PREFIX_CLS } from '../../../../config';
import templateList from './data';
import './index.less';

const { Meta } = Card;
const prefixCls = `${PREFIX_CLS}-swiper-template`;

const templateImgUrl =
  'https://node-upload-service.nissan-digital-marketing-platform.dev.dndc.cloud/nissan-uat/image/202106/807f29e0-d231-11eb-87d0-6dd8ec81cb29.jpg';
export default ({ config = {}, node, dispatch }) => {
  const [visible, setVisible] = useState(false);
  const defaultGroup = {
    label: config.label,
  };
  const { group = defaultGroup } = config;

  const handleClick = item => {
    dispatch({
      type: 'tree/updateByTemplate',
      payload: { id: node.id, data: item },
    });
    setVisible(false);
  };
  return (
    <>
      <Group group={group}>
        <Form.Item>
          <div className="cell-row">
            <Button
              onClick={() => {
                setVisible(true);
              }}
            >
              选择模板
            </Button>
          </div>
        </Form.Item>
      </Group>
      <Modal
        title="选择轮播模板"
        visible={visible}
        closable={false}
        onCancel={() => {
          setVisible(false);
        }}
        onOk={() => {
          setVisible(false);
        }}
        width="94%"
      >
        <div className={prefixCls}>
          <Row gutter={[16, 24]}>
            {templateList.map(templateItem => {
              return (
                <Col className="gutter-row" span={8}>
                  <Card hoverable bodyStyle={{ padding: 15 }}>
                    <div className={`${prefixCls}-item`}>
                      <Swiper model={SysModel.DEV} data={templateItem.data}>
                        {templateItem.data.props.sliders.map(() => {
                          return (
                            <img
                              src={templateImgUrl}
                              alt=""
                              style={{ width: '100%' }}
                            />
                          );
                        })}
                      </Swiper>
                    </div>
                    <div className={`${prefixCls}-meta`}>
                      <Meta
                        description={templateItem.name}
                        style={{ marginTop: 10 }}
                      />
                      <Button
                        style={{ width: '100%', marginTop: 20 }}
                        type="primary"
                        onClick={() => handleClick(templateItem.data)}
                      >
                        选择它
                      </Button>
                    </div>
                  </Card>
                </Col>
              );
            })}
          </Row>
        </div>
      </Modal>
    </>
  );
};
