import React, { useEffect, useState } from 'react';

import { LockOutlined } from '@ant-design/icons';
import { Card, Form, Input } from 'antd';
import axios from 'axios';
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 JobCostingStatistics from '../job-costing-statistics/job-costing-statistics.component';
import LoadingSkeleton from '../loading-skeleton/loading-skeleton.component';

const mapStateToProps = createStructuredSelector({
  //currentUser: selectCurrentUser
  bodyshop: selectBodyshop,
  jobRO: selectJobReadOnly,
});
const mapDispatchToProps = (dispatch) => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(JobCloseRoGuardProfit);

export function JobCloseRoGuardProfit({ job, jobRO, bodyshop, form, warningCallback }) {
  const [costingData, setCostingData] = useState(null);
  const [loading, setLoading] = useState(false);
  const { t } = useTranslation();

  useEffect(() => {
    async function getData() {
      try {
        if (job.id) {
          setLoading(true);
          const { data } = await axios.post('/job/costing', { jobid: job.id });
          setCostingData(data);
        }
      } catch (error) {}
      setLoading(false);
    }

    getData();
  }, [job.id]);

  const enforceProfitPassword =
    parseFloat(costingData?.summaryData.gppercent) < bodyshop?.md_ro_guard?.totalgppercent_minimum;

  useEffect(() => {
    if (enforceProfitPassword && typeof warningCallback === 'function') {
      warningCallback({ key: 'profit', warning: t('jobs.labels.profitbypassrequired') });
    }
  }, [enforceProfitPassword, t, warningCallback]);

  if (loading || !costingData) return <LoadingSkeleton />;

  return (
    <Card title={t('jobs.labels.profits')} style={{ height: '100%' }}>
      <JobCostingStatistics summaryData={costingData?.summaryData} onlyGP />
    </Card>
  );
}
