import { Card, Form, Statistic, Tooltip } from "antd";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectJobReadOnly } from "../../redux/application/application.selectors";
import { selectBodyshop } from "../../redux/user/user.selectors";
import dayjs from "../../utils/day";
import DateTimePicker from "../form-date-time-picker/form-date-time-picker.component";
import FormRow from "../layout-form-row/layout-form-row.component";

const mapStateToProps = createStructuredSelector({
  jobRO: selectJobReadOnly,
  bodyshop: selectBodyshop
});

export function JobsDetailDatesComponent({ jobRO, job, bodyshop }) {
  const { t } = useTranslation();

  const jobInPostProduction = useMemo(() => {
    return bodyshop.md_ro_statuses.post_production_statuses.includes(job.status);
  }, [job.status, bodyshop.md_ro_statuses.post_production_statuses]);

  const calcRepairDays = job?.joblines?.length
    ? job.joblines.reduce((acc, val) => acc + val.mod_lb_hrs, 0) /
      (bodyshop.target_touchtime === 0 ? 1 : bodyshop.target_touchtime)
    : [];

  return (
    <Card>
      <FormRow header={t("jobs.forms.estdates")}>
        <Form.Item label={t("jobs.fields.date_estimated")} name="date_estimated">
          <DateTimePicker disabled={jobRO} isDateOnly />
        </Form.Item>
        <Form.Item label={t("jobs.fields.date_open")} name="date_open">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.date_towin")} name="date_towin">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.date_rentalresp")} name="date_rentalresp">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.estimate_sent_approval")} name="estimate_sent_approval">
          <DateTimePicker
            disabled={jobRO}
            value={job.estimate_sent_approval ? dayjs(job.estimate_sent_approval) : null}
            placeholder={t("general.labels.na")}
          />
        </Form.Item>
        <Form.Item label={t("jobs.fields.estimate_approved")} name="estimate_approved">
          <DateTimePicker
            disabled={jobRO}
            value={job.estimate_approved ? dayjs(job.estimate_approved) : null}
            placeholder={t("general.labels.na")}
          />
        </Form.Item>
      </FormRow>

      <FormRow header={t("jobs.forms.scheddates")}>
        <Form.Item label={t("jobs.fields.date_scheduled")} name="date_scheduled">
          <DateTimePicker disabled={jobRO} isDateOnly />
        </Form.Item>
        <Tooltip title={t("jobs.labels.scheduledinchange")}>
          <Form.Item label={t("jobs.fields.scheduled_in")} name="scheduled_in">
            <DateTimePicker disabled={true} />
          </Form.Item>
        </Tooltip>
        <Form.Item label={t("jobs.fields.actual_in")} name="actual_in">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
        <Tooltip
          title={t("jobs.labels.calc_repair_days_tt", {
            target_touchtime: bodyshop.target_touchtime
          })}
        >
          <Statistic value={calcRepairDays} precision={1} title={t("jobs.labels.calc_repair_days")} />
        </Tooltip>
        <Form.Item label={t("jobs.fields.date_repairstarted")} name="date_repairstarted">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
      </FormRow>
      <FormRow header={t("jobs.forms.repairdates")}>
        <Form.Item label={t("jobs.fields.date_last_contacted")} name="date_last_contacted">
          <DateTimePicker />
        </Form.Item>
        <Form.Item label={t("jobs.fields.date_next_contact")} name="date_next_contact">
          <DateTimePicker />
        </Form.Item>
        <Form.Item label={t("jobs.fields.scheduled_completion")} name="scheduled_completion">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
        <Form.Item shouldUpdate>
          {() => (
            <Form.Item
              label={t("jobs.fields.actual_completion")}
              name="actual_completion"
              rules={[{ required: jobInPostProduction }]}
            >
              <DateTimePicker disabled={jobRO} />
            </Form.Item>
          )}
        </Form.Item>
        <Form.Item label={t("jobs.fields.scheduled_delivery")} name="scheduled_delivery">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.actual_delivery")} name="actual_delivery">
          <DateTimePicker disabled={jobRO} />
        </Form.Item>
      </FormRow>
      <FormRow header={t("jobs.forms.admindates")}>
        <Form.Item label={t("jobs.fields.date_invoiced")} name="date_invoiced">
          <DateTimePicker disabled={true} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.date_exported")} name="date_exported">
          <DateTimePicker disabled={true} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.date_void")} name="date_void">
          <DateTimePicker disabled={true} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.date_lost_sale")} name="date_lost_sale">
          <DateTimePicker disabled={true} />
        </Form.Item>
      </FormRow>
    </Card>
  );
}

export default connect(mapStateToProps, null)(JobsDetailDatesComponent);
