import { Collapse, Form, Input, InputNumber, Select, Space, Switch } from "antd";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import InstanceRenderManager from "../../utils/instanceRenderMgr";
import DateTimePicker from "../form-date-time-picker/form-date-time-picker.component.jsx";
import CurrencyInput from "../form-items-formatted/currency-form-item.component";
import FormItemEmail from "../form-items-formatted/email-form-item.component";
import FormItemPhone, { PhoneItemFormatterValidation } from "../form-items-formatted/phone-form-item.component";
import JobsDetailChangeEstimator from "../jobs-detail-change-estimator/jobs-detail-change-estimator.component";
import JobsDetailChangeFilehandler from "../jobs-detail-change-filehandler/jobs-detail-change-filehandler.component";
import JobsDetailRatesChangeButton from "../jobs-detail-rates-change-button/jobs-detail-rates-change-button.component";
import JobsDetailRatesLabor from "../jobs-detail-rates/jobs-detail-rates.labor.component";
import JobsDetailRatesMaterials from "../jobs-detail-rates/jobs-detail-rates.materials.component";
import JobsDetailRatesOther from "../jobs-detail-rates/jobs-detail-rates.other.component";
import JobsDetailRatesParts from "../jobs-detail-rates/jobs-detail-rates.parts.component";
import JobsDetailRatesTaxes from "../jobs-detail-rates/jobs-detail-rates.taxes.component";
import JobsMarkPstExempt from "../jobs-mark-pst-exempt/jobs-mark-pst-exempt.component";
import LayoutFormRow from "../layout-form-row/layout-form-row.component";

const mapStateToProps = createStructuredSelector({
  //currentUser: selectCurrentUser
  bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});

export function JobsCreateJobsInfo({ bodyshop, form, selected }) {
  const { t } = useTranslation();
  const { getFieldValue } = form;

  const handleInsCoChange = (value) => {
    const selectedCompany = bodyshop.md_ins_cos.find((s) => s.name === value);
    if (selectedCompany) {
      form.setFieldValue("ins_addr1", selectedCompany.street1);
      form.setFieldValue("ins_city", selectedCompany.city);
    }
  };

  return (
    <div>
      <Collapse
        defaultActiveKey="insurance"
        items={[
          {
            key: "insurance",
            label: t("menus.jobsdetail.insurance"),
            forceRender: true,
            children: (
              <LayoutFormRow>
                <Form.Item label={t("jobs.fields.clm_no")} name="clm_no">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.policy_no")} name="policy_no">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.regie_number")} name="regie_number">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.ins_co_nm")} name="ins_co_nm">
                  <Select
                    onChange={handleInsCoChange}
                    options={bodyshop.md_ins_cos.map((s) => ({
                      value: s.name,
                      label: s.name
                    }))}
                  />
                </Form.Item>
                <Form.Item label={t("jobs.fields.ins_addr1")} name="ins_addr1">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.ins_city")} name="ins_city">
                  <Input />
                </Form.Item>
                <Form.Item
                  label={
                    <Space>
                      {t("jobs.fields.ins_ct_ln")}
                      <JobsDetailChangeFilehandler form={form} />
                    </Space>
                  }
                  name="ins_ct_ln"
                >
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.ins_ct_fn")} name="ins_ct_fn">
                  <Input />
                </Form.Item>
                <Form.Item
                  label={t("jobs.fields.ins_ph1")}
                  name="ins_ph1"
                  rules={[({ getFieldValue }) => PhoneItemFormatterValidation(getFieldValue, "ins_ph1")]}
                >
                  <FormItemPhone />
                </Form.Item>
                <Form.Item
                  label={t("jobs.fields.ins_ea")}
                  name="ins_ea"
                  rules={[
                    {
                      type: "email",
                      message: "This is not a valid email address."
                    }
                  ]}
                >
                  <FormItemEmail email={getFieldValue("ins_ea")} />
                </Form.Item>
                <Form.Item label={t("jobs.fields.loss_date")} name="loss_date">
                  <DateTimePicker isDateOnly />
                </Form.Item>
                <Form.Item label={t("jobs.fields.kmin")} name="kmin">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.est_co_nm")} name="est_co_nm">
                  <Input />
                </Form.Item>
                <Form.Item
                  label={
                    <Space>
                      {t("jobs.fields.est_ct_fn")}
                      <JobsDetailChangeEstimator form={form} />
                    </Space>
                  }
                  name="est_ct_fn"
                >
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.est_ct_ln")} name="est_ct_ln">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.est_ph1")} name="est_ph1">
                  <Input />
                </Form.Item>
                <Form.Item
                  label={t("jobs.fields.est_ea")}
                  name="est_ea"
                  rules={[
                    {
                      type: "email",
                      message: "This is not a valid email address."
                    }
                  ]}
                >
                  <FormItemEmail email={getFieldValue("est_ea")} />
                </Form.Item>
                <Form.Item label={t("jobs.fields.selling_dealer")} name="selling_dealer">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.servicing_dealer")} name="servicing_dealer">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.selling_dealer_contact")} name="selling_dealer_contact">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.servicing_dealer_contact")} name="servicing_dealer_contact">
                  <Input />
                </Form.Item>
              </LayoutFormRow>
            )
          },
          {
            key: "claim",
            label: t("menus.jobsdetail.claimdetail"),
            forceRender: true,
            children: (
              <LayoutFormRow>
                <Form.Item label={t("jobs.fields.loss_desc")} name="loss_desc">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.loss_of_use")} name="loss_of_use">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.ponumber")} name="po_number">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.unitnumber")} name="unit_number">
                  <Input />
                </Form.Item>
                <Form.Item
                  label={t("jobs.fields.specialcoveragepolicy")}
                  valuePropName="checked"
                  name="special_coverage_policy"
                >
                  <Switch />
                </Form.Item>
                <Form.Item label={t("jobs.fields.kmin")} name="kmin">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.kmout")} name="kmout">
                  <Input />
                </Form.Item>
                <Form.Item label={t("jobs.fields.referralsource")} name="referral_source">
                  <Select
                    showSearch={{
                      optionFilterProp: "label"
                    }}
                    options={bodyshop.md_referral_sources.map((s) => ({
                      value: s,
                      label: s
                    }))}
                  />
                </Form.Item>
                <Form.Item label={t("jobs.fields.referral_source_extra")} name="referral_source_extra">
                  <Input />
                </Form.Item>
              </LayoutFormRow>
            )
          },
          {
            key: "financial",
            label: t("menus.jobsdetail.financials"),
            forceRender: true,
            children: (
              <>
                <JobsDetailRatesChangeButton form={form} />
                {InstanceRenderManager({
                  imex: <JobsMarkPstExempt form={form} />
                })}
                <LayoutFormRow>
                  <Form.Item label={t("jobs.fields.ded_amt")} name="ded_amt">
                    <CurrencyInput min={0} />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.ded_status")} name="ded_status">
                    <Select
                      allowClear
                      options={[
                        { value: "W", label: t("jobs.labels.deductible.waived") },
                        { value: "Y", label: t("jobs.labels.deductible.stands") }
                      ]}
                    />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.depreciation_taxes")} name="depreciation_taxes">
                    <CurrencyInput />
                  </Form.Item>
                  {bodyshop.region_config.toLowerCase().startsWith("ca") && (
                    <Form.Item
                      label={t("jobs.fields.ca_gst_registrant")}
                      name="ca_gst_registrant"
                      valuePropName="checked"
                    >
                      <Switch />
                    </Form.Item>
                  )}
                  <Form.Item label={t("jobs.fields.other_amount_payable")} name="other_amount_payable">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.towing_payable")} name="towing_payable">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.storage_payable")} name="storage_payable">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.adjustment_bottom_line")} name="adjustment_bottom_line">
                    <CurrencyInput />
                  </Form.Item>
                </LayoutFormRow>
                {InstanceRenderManager({
                  imex: (
                    <LayoutFormRow>
                      <Form.Item label={t("jobs.fields.federal_tax_rate")} name="federal_tax_rate">
                        <InputNumber min={0} max={1} precision={2} />
                      </Form.Item>
                      <Form.Item label={t("jobs.fields.state_tax_rate")} name="state_tax_rate">
                        <InputNumber min={0} max={1} precision={2} />
                      </Form.Item>
                      <Form.Item label={t("jobs.fields.local_tax_rate")} name="local_tax_rate">
                        <InputNumber min={0} max={1} precision={2} />
                      </Form.Item>
                    </LayoutFormRow>
                  )
                })}
                <LayoutFormRow>
                  <Form.Item label={t("jobs.fields.rate_lab")} name="rate_lab">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_lad")} name="rate_lad">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_lae")} name="rate_lae">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_lar")} name="rate_lar">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_las")} name="rate_las">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_laf")} name="rate_laf">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_lam")} name="rate_lam">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_lag")} name="rate_lag">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_lau")} name="rate_lau">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_la1")} name="rate_la1">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_la2")} name="rate_la2">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_la3")} name="rate_la3">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_la4")} name="rate_la4">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_mapa")} name="rate_mapa">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_mash")} name="rate_mash">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_mahw")} name="rate_mahw">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_ma2s")} name="rate_ma2s">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_ma3s")} name="rate_ma3s">
                    <CurrencyInput />
                  </Form.Item>
                  {
                    // <Form.Item label={t("jobs.fields.rate_mabl")} name="rate_mabl">
                    //   <CurrencyInput />
                    // </Form.Item>
                    // <Form.Item label={t("jobs.fields.rate_macs")} name="rate_macs">
                    //   <CurrencyInput />
                    // </Form.Item>
                  }
                  <Form.Item label={t("jobs.fields.rate_matd")} name="rate_matd">
                    <CurrencyInput />
                  </Form.Item>
                  <Form.Item label={t("jobs.fields.rate_laa")} name="rate_laa">
                    <CurrencyInput />
                  </Form.Item>
                </LayoutFormRow>
              </>
            )
          }
        ]}
      />
      <JobsDetailRatesParts jobRO={false} expanded required={selected && true} form={form} />
      {InstanceRenderManager({
        rome: (
          <>
            <JobsDetailRatesLabor form={form} />
            <JobsDetailRatesMaterials form={form} />
            <JobsDetailRatesOther form={form} />
            <JobsDetailRatesTaxes form={form} />
          </>
        )
      })}
    </div>
  );
}

export default connect(mapStateToProps, mapDispatchToProps)(JobsCreateJobsInfo);
