import { Input, Label } from '@bjsdistribution/dms-component-library';
import React from 'react';
import styled from 'styled-components';
import CopyText from '../../../../../../common/copy/CopyText';
import { useOrderDetails } from '../../../../store';
// import { LocationIcon } from '../../../../assets/icons';

function Postcode() {
  // show copy icon if there is input value
  const [orderDetails, setOrderDetails] = useOrderDetails();
  const orderRecipient = orderDetails?.order_recipient;
  const showCopyIcon =
    orderRecipient?.postcode !== undefined &&
    orderRecipient?.postcode !== null &&
    orderRecipient?.postcode?.length > 0;

  const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const val = e.target.value;
    const updatedOrderRecipient = {
      ...(orderRecipient === null
        ? { postcode: val }
        : { ...orderRecipient, postcode: val }),
    };
    setOrderDetails(prev => {
      return {
        ...prev,
        order_recipient: updatedOrderRecipient,
      };
    });
  };
  return (
    <Container>
      <Wrapper>
        <Label label="Postcode" htmlFor="postcode" />
        {showCopyIcon && <CopyText text={orderRecipient?.postcode!} />}
      </Wrapper>
      <Input
        name="postcode"
        type="text"
        placeholder="Enter Postcode"
        value={orderRecipient?.postcode || ''}
        onChange={onChange}
        autoComplete="off"
        style={{ borderRadius: '25px' }}
      />
    </Container>
  );
}

const Wrapper = styled.div`
  display: flex;
  align-items: center;
  gap: 5px;
`;

const Container = styled.div`
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
`;

export default Postcode;
