import { Form, Statistic, Tooltip } from "antd";
import React, { 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 FormDatePicker from "../form-date-picker/form-date-picker.component";
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.reduce((acc, val) => acc + val.mod_lb_hrs, 0) /
    (bodyshop.target_touchtime === 0 ? 1 : bodyshop.target_touchtime);

  return (
    <div>
      <FormRow header={t("jobs.forms.estdates")}>
        <Form.Item
          label={t("jobs.fields.date_estimated")}
          name="date_estimated"
        >
          <FormDatePicker disabled={jobRO} />
        </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>
      </FormRow>

      <FormRow header={t("jobs.forms.scheddates")}>
        <Form.Item
          label={t("jobs.fields.date_scheduled")}
          name="date_scheduled"
        >
          <FormDatePicker disabled={jobRO} />
        </Form.Item>
        <Tooltip title={t("jobs.labels.scheduledinchange")}>
          <Form.Item label={t("jobs.fields.scheduled_in")} name="scheduled_in">
            <DateTimePicker disabled={true || jobRO} />
          </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>
          {() => {
            return (
              <Form.Item
                label={t("jobs.fields.actual_completion")}
                name="actual_completion"
                rules={[
                  {
                    required: jobInPostProduction,
                    //message: t("general.validation.required"),
                  },
                ]}
              >
                <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 || jobRO} />
        </Form.Item>

        <Form.Item label={t("jobs.fields.date_exported")} name="date_exported">
          <DateTimePicker disabled={true || jobRO} />
        </Form.Item>
      </FormRow>
    </div>
  );
}
export default connect(mapStateToProps, null)(JobsDetailDatesComponent);
