import { NavLink } from "react-router-dom";
import { useState } from 'react';
import { observer } from "mobx-react";
import { toJS } from "mobx";
import { Row, Col, Space, Button, Table, Input, Typography, DatePicker, Radio, Modal, App } from 'antd';
import { useStore } from '@/stores/StoreContext.js';

const { Title } = Typography;

function Newest() {
  const reservationListColumns = [
    {
      title: 'Reference number',
      dataIndex: 'referenceNumber',
      key: 'Reference number',
      render: (text, record) => <NavLink to={`/reservation/${record.id}`}>{text}</NavLink>,
    },
    {
      title: 'Arrival date',
      dataIndex: 'arrivalDate',
      key: 'Arrival date',
    },
    {
      title: 'Pax',
      key: 'Pax',
      dataIndex: 'pax'
    },
    {
      title: 'Status',
      key: 'Status',
      dataIndex: 'status'
    },
    {
      title: 'Reservation date',
      key: 'Reservation date',
      dataIndex: 'reservationDate'
    },
    {
      title: 'Guide',
      key: 'Guide',
      dataIndex: 'guide',
      render: guideRender
    },
  ];
  
  function guideRender(text, reservation) {
    if (reservation.guide === '') {
      return (
        <Space size="middle">
          <Button type="link" onClick={() => showModal(reservation)}>Fill in</Button>
        </Space>
      );
    } else {
      return (
        <Space size="middle">
          <span>{reservation.guide}</span>
          <Button type="link" onClick={() => showModal(reservation)}>Modify</Button>
        </Space>
      );
    }
  }
  const { reservationStore } = useStore();
  const { reservationList, reservationPage, cityList } = reservationStore;
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [arrivalDateRange, onDateRangeChange] = useState([]);
  const [referenceNo, onNumberChange] = useState('');
  const [dataLoading, setDataLoading] = useState(false);
  const { notification } = App.useApp();

  const showModal = (reservation) => {
    console.info(reservation);
    
    reservationStore.fetchCityList(reservation.referenceId)
      .catch(ex => {
        notification.error({
          message: `Notification`,
          description: ex.message,
          placement: 'top',
          duration: 4,
        });
      })
      .finally(() => {
        setDataLoading(false);
      });
    setIsModalOpen(true);
  };
  const handleOk = () => {
    setIsModalOpen(false);
  };
  const handleCancel = () => {
    setIsModalOpen(false);
  };
  
  const onSearchClick = (current=1) => {
    setDataLoading(true);
    reservationStore.fetchReservationList(current, referenceNo, arrivalDateRange[0], arrivalDateRange[1])
      .catch(ex => {
        notification.error({
          message: `Notification`,
          description: ex.message,
          placement: 'top',
          duration: 4,
        });
      })
      .finally(() => {
        setDataLoading(false);
      });
  }

  return (
    <>
      <Modal
        centered
        open={isModalOpen} onOk={handleOk} onCancel={handleCancel}
      >
        <Space direction="vertical" style={{ width: '100%' }}>
          <Row>
            <Col span={24}>
              <Table
                bordered
                pagination={false}
                columns={[
                  {
                    title: 'City',
                    dataIndex: 'cityName',
                    key: 'cityName'
                  },
                  {
                    title: 'Tour Guide',
                    dataIndex: 'tourGuide',
                    key: 'tourGuide'
                  }
                ]}
                dataSource={toJS(cityList)}
              />
            </Col>
          </Row>
          <Row gutter={{ md: 24 }}>
            <Col span={24}>
              <Title level={5}>Guide</Title>
            </Col>
          </Row>
          
        <Row gutter={{ md: 24 }}>
          <Col span={16}>
            <Input placeholder="Guide" />
          </Col>
          <Col span={8}>
            <Button type='primary' onClick={() => {}}>Search</Button>
          </Col>
        </Row>
          <Row gutter={{ md: 24 }}>
            <Col span={24}>
              <Radio.Group options={[
                {
                  label: 'Jim',
                  value: '1',
                },
                {
                  label: 'Giffigan',
                  value: '2',
                },
                {
                  label: 'Herry',
                  value: '3',
                },
                {
                  label: 'Giffigan',
                  value: '4',
                },
                {
                  label: 'Herry',
                  value: '5',
                },
                {
                  label: 'Giffigan',
                  value: '6',
                },
                {
                  label: 'Herry',
                  value: '7',
                },
              ]} />
            </Col>
          </Row>
        </Space>
      </Modal>
      <Space direction="vertical" style={{ width: '100%' }}>
        <Title level={3}>Newest Reservations</Title>
        <Row gutter={{ md: 24 }}>
          <Col span={4}>
            <Input placeholder="Reference number" onChange={(e) => {onNumberChange(e.target.value)}} />
          </Col>
          <Col span={6}>
            <Space direction="horizontal">
              Arrival Date
              <DatePicker.RangePicker
                allowClear={true}
                inputReadOnly={true}
                placeholder={['From', 'Thru']}
                onChange={(date, dateRange) => { onDateRangeChange(dateRange)}}
              />
            </Space>
          </Col>
          <Col span={14}>
            <Button type='primary' onClick={() => onSearchClick()} loading={dataLoading}>Search</Button>
          </Col>
        </Row>
        <Row>
          <Col span={24}>
            <Table
              title={() => 'Reservations without the tour guide information will be highlighted in red if the arrival date is within 3 days.'}
              bordered
              loading={dataLoading}
              pagination={{
                position: ['bottomCenter'],
                current: reservationPage.current,
                pageSize: reservationPage.size,
                total: reservationPage.total,
                simple: true
              }}
              onChange={(pagination, filters, sorter, extra) => {onSearchClick(pagination.current);}}
              columns={reservationListColumns} dataSource={toJS(reservationList)}
            />
          </Col>
        </Row>
      </Space>
    </>
  );
}

export default observer(Newest);