// material-ui import { Grid, Button, Checkbox, FormControlLabel, Typography } from '@mui/material'; // import { FormControlLabel } from '@material-ui/core'; import MainCard from "../../components/MainCard"; import * as React from "react"; import { useFormik } from 'formik'; import * as yup from 'yup'; import { useEffect, useState } from "react"; import * as HttpUtils from '../../utils/HttpUtils'; import * as UrlUtils from "../../utils/ApiPathConst"; import * as FieldUtils from "../../utils/FieldUtils"; import * as ComboData from "../../utils/ComboData"; const LoadingComponent = Loadable(lazy(() => import('../extra-pages/LoadingComponent'))); import Loadable from 'components/Loadable'; import { lazy } from 'react'; import { notifySaveSuccess } from 'utils/CommonFunction'; // ==============================|| DASHBOARD - DEFAULT ||============================== // const OrganizationCard = ({ userData, loadDataFun, id }) => { const [currentUserData, setCurrentUserData] = useState({}); const [editMode, setEditMode] = useState(false); const [createMode, setCreateMode] = useState(false); const [onReady, setOnReady] = useState(false); useEffect(() => { //if state data are ready and assign to different field // console.log(currentApplicationDetailData) if (Object.keys(currentUserData).length > 0) { setOnReady(true); } }, [currentUserData]); const formik = useFormik({ enableReinitialize: true, initialValues: currentUserData, validationSchema: yup.object().shape({ enCompanyName: yup.string().max(255).required('請輸入英文名稱'), chCompanyName: yup.string().max(255, '請輸入中文名稱').nullable(), addressLine1: yup.string().max(255).required('請輸入第一行地址'), addressLine2: yup.string().max(255, "length must <= 255"), addressLine3: yup.string().max(255, "length must <= 255"), fax_countryCode: yup.string().min(3, '請輸入國際區號').nullable(), tel_countryCode: yup.string().min(3, '請輸入國際區號'), phoneNumber: yup.string().min(8, '請輸入有效聯絡電話').required('請輸入聯絡電話'), faxNumber: yup.string().min(8, '請輸入8位數字').nullable(), brExpiryDate: yup.string().min(8).required('請輸入商業登記證有效日期'), brNo: yup.string().min(8, '請輸入有效商業登記證號碼').max(8, '請輸入有效商業登記證號碼').required('請輸入商業登記證號碼'), }), onSubmit: vaule => { console.log(vaule) HttpUtils.post({ url: UrlUtils.POST_ORG_SAVE_PATH, params: { id: id > 0 ? id : null, enCompanyName: vaule.enCompanyName, chCompanyName: vaule.chCompanyName, brNo: vaule.brNo, brExpiryDate: vaule.brExpiryDate, enCompanyNameTemp: vaule.enCompanyNameTemp, chCompanyNameTemp: vaule.chCompanyNameTemp, brExpiryDateTemp: vaule.brExpiryDateTemp, contactPerson: vaule.contactPerson, contactTel: { countryCode: vaule.tel_countryCode, phoneNumber: vaule.phoneNumber }, faxNo: { countryCode: vaule.fax_countryCode, faxNumber: vaule.faxNumber }, addressTemp: { country: vaule.country, district: vaule.district, addressLine1: vaule.addressLine1, addressLine2: vaule.addressLine2, addressLine3: vaule.addressLine3, }, creditor: vaule.creditor, }, onSuccess: function () { notifySaveSuccess() loadDataFun(); setEditMode(false); } }); } }); useEffect(() => { if (Object.keys(userData).length > 0) { setCreateMode(id <= 0); setEditMode(id <= 0); setCurrentUserData(userData); } }, [userData]); // useEffect(() => { // if (Object.keys(userData).length > 0) { // if(userData.creditor === undefined||userData.creditor === null){ // userData.creditor = false // } // setCurrentUserData(userData); // } // }, []); const onEditClick = () => { setEditMode(true); }; return (
{/*top*/} {editMode ? <> {createMode ? <> : <> } : <> } {/*top*/} {!onReady ? : Organization Details {FieldUtils.getTextField({ label: FieldUtils.notNullFieldLabel("BR No.:"), valueName: "brNo", disabled: (!editMode && !createMode), form: formik })} } label="is Creditor" name="creditor" onChange={() => { formik.setFieldValue("creditor", !formik.values.creditor); }} disabled={!editMode && !createMode} /> {FieldUtils.getTextField({ label: FieldUtils.notNullFieldLabel("Name (Eng):"), valueName: "enCompanyName", disabled: (!editMode && !createMode), form: formik })} {FieldUtils.getTextField({ label: "Name (Ch):", valueName: "chCompanyName", disabled: (!editMode && !createMode), form: formik })} {FieldUtils.getDateField({ label: FieldUtils.notNullFieldLabel("Expiry Date:"), valueName: "brExpiryDate", disabled: (!editMode && !createMode), form: formik })} {FieldUtils.getTextField({ label: FieldUtils.notNullFieldLabel("Contact Person:"), valueName: "contactPerson", disabled: (!editMode && !createMode), form: formik })} {FieldUtils.getPhoneField({ label: FieldUtils.notNullFieldLabel("Contact Tel:"), valueName: { code: "tel_countryCode", num: "phoneNumber" }, disabled: (!editMode && !createMode), form: formik })} {FieldUtils.getPhoneField({ label: "Fax No:", valueName: { code: "fax_countryCode", num: "faxNumber" }, disabled: (!editMode && !createMode), form: formik })} {FieldUtils.getComboField({ label: FieldUtils.notNullFieldLabel("Country:"), valueName: "country", disabled: (!editMode && !createMode), dataList: ComboData.country, form: formik })} {FieldUtils.getComboField({ label: FieldUtils.notNullFieldLabel("District:"), valueName: "district", disabled: (!editMode && !createMode), dataList: ComboData.district, form: formik })} {FieldUtils.getAddressField({ label: FieldUtils.notNullFieldLabel("Address:"), valueName: ["addressLine1", "addressLine2", "addressLine3"], disabled: (!editMode && !createMode), form: formik })} }
); }; export default OrganizationCard;