@model YourProjectNamespace.Models.Clsprp_Agent


@using (Html.BeginForm("RegIndAgent", "Agent", FormMethod.Post, new { enctype = "multipart/form-data", id = "agentForm" }))
{
    @Html.AntiForgeryToken()

    <div class="border p-3 mb-3">
        <h5>Select Option</h5>
        <div class="form-group">
            <label>Do you Already have Delhi RERA Number (Yes/No)? <span class="text-danger">*</span></label>
            <div>
                <label class="mr-3">
                    @Html.RadioButtonFor(m => m.IsAlready_RERANumber, "Yes") Yes
                </label>
                <label>
                    @Html.RadioButtonFor(m => m.IsAlready_RERANumber, "No") No
                </label>
            </div>
            @Html.ValidationMessageFor(m => m.IsAlready_RERANumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="border p-3 mb-3">
        <h5>Agent Registration Details</h5>
        <div class="form-row">
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.Agent_FirstName) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.Agent_FirstName, new { @class = "form-control", placeholder = "First Name" })
                @Html.ValidationMessageFor(m => m.Agent_FirstName, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.Agent_MiddleName)
                @Html.TextBoxFor(m => m.Agent_MiddleName, new { @class = "form-control", placeholder = "Middle Name" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.Agent_LastName) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.Agent_LastName, new { @class = "form-control", placeholder = "Last Name" })
                @Html.ValidationMessageFor(m => m.Agent_LastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.Father_FirstName) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.Father_FirstName, new { @class = "form-control", placeholder = "Father's First Name" })
                @Html.ValidationMessageFor(m => m.Father_FirstName, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.Father_MiddleName)
                @Html.TextBoxFor(m => m.Father_MiddleName, new { @class = "form-control", placeholder = "Father's Middle Name" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.Father_LastName) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.Father_LastName, new { @class = "form-control", placeholder = "Father's Last Name" })
                @Html.ValidationMessageFor(m => m.Father_LastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-row align-items-end">
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.PAN_Number) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.PAN_Number, new { @class = "form-control", placeholder = "PAN Number" })
                <small class="text-muted">PAN Number of Real Estate Agent as per PAN Card</small>
                @Html.ValidationMessageFor(m => m.PAN_Number, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.MobileNumber) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.MobileNumber, new { @class = "form-control", placeholder = "Mobile Number" })
                @Html.ValidationMessageFor(m => m.MobileNumber, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.Occupation) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.Occupation, new { @class = "form-control", placeholder = "Occupation" })
                @Html.ValidationMessageFor(m => m.Occupation, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <label>Do you have other State/UT RERA registration No.(Yes/No)? <span class="text-danger">*</span></label>
            <div>
                <label class="mr-3">
                    @Html.RadioButtonFor(m => m.IsOtherStateUT_RERAregistration, "Yes") Yes
                </label>
                <label>
                    @Html.RadioButtonFor(m => m.IsOtherStateUT_RERAregistration, "No") No
                </label>
            </div>
            @Html.ValidationMessageFor(m => m.IsOtherStateUT_RERAregistration, "", new { @class = "text-danger" })
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.EmailAddress) <span class="text-danger">*</span>
            @Html.TextBoxFor(m => m.EmailAddress, new { @class = "form-control", placeholder = "Email Address" })
            @Html.ValidationMessageFor(m => m.EmailAddress, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="border p-3 mb-3">
        <h5>Upload Photograph of Agent</h5>
        <div class="form-group">
            <label>Upload Photo <span class="text-danger">*</span></label>
            <input type="file" name="ImageFile" accept=".jpg,.jpeg,.png" class="form-control-file" />
            <small class="form-text text-muted">Photo should be in .jpg or .png format and less than 512KB</small>
        </div>

        @* Show preview if image path exists *@
        @if (!string.IsNullOrEmpty(Model.Image_FilePath) && !string.IsNullOrEmpty(Model.Image_FileName))
        {
            <div>
                <img src="@Url.Content("~/" + Model.Image_FilePath + Model.Image_FileName)" alt="Agent Photo" style="max-width:120px; max-height:120px;" />
            </div>
        }
    </div>

    <div class="border p-3 mb-3">
        <h5>Permanent Residence Address</h5>
        <div class="form-row">
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.P_AddressLine1) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.P_AddressLine1, new { @class = "form-control", placeholder = "Address Line 1" })
                @Html.ValidationMessageFor(m => m.P_AddressLine1, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.P_AddressLine2)
                @Html.TextBoxFor(m => m.P_AddressLine2, new { @class = "form-control", placeholder = "Address Line 2" })
            </div>
            <div class="form-group col-md-2">
                @Html.LabelFor(m => m.P_AddressStateCode) <span class="text-danger">*</span>
                @Html.DropDownListFor(m => m.P_AddressStateCode, new SelectList(Model.stateMaster, "State_Code", "State_Name"), "Select State", new { @class = "form-control", id = "ddlPState" })
                @Html.ValidationMessageFor(m => m.P_AddressStateCode, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-2">
                @Html.LabelFor(m => m.P_AddressDistrictCode) <span class="text-danger">*</span>
                @Html.DropDownListFor(m => m.P_AddressDistrictCode, new SelectList(Model.districtMaster, "District_Code", "District_Name"), "Select District", new { @class = "form-control", id = "ddlPDistrict" })
                @Html.ValidationMessageFor(m => m.P_AddressDistrictCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group col-md-2">
            @Html.LabelFor(m => m.P_AddressPIN) <span class="text-danger">*</span>
            @Html.TextBoxFor(m => m.P_AddressPIN, new { @class = "form-control", placeholder = "PIN Code" })
            @Html.ValidationMessageFor(m => m.P_AddressPIN, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="border p-3 mb-3">
        <h5>Address for Place of Business</h5>
        <div class="form-row">
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.BusinessPlace_AddressLine1) <span class="text-danger">*</span>
                @Html.TextBoxFor(m => m.BusinessPlace_AddressLine1, new { @class = "form-control", placeholder = "Address Line 1" })
                @Html.ValidationMessageFor(m => m.BusinessPlace_AddressLine1, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-4">
                @Html.LabelFor(m => m.BusinessPlace_AddressLine2)
                @Html.TextBoxFor(m => m.BusinessPlace_AddressLine2, new { @class = "form-control", placeholder = "Address Line 2" })
            </div>
            <div class="form-group col-md-2">
                @Html.LabelFor(m => m.BusinessPlace_AddressStateCode) <span class="text-danger">*</span>
                @Html.DropDownListFor(m => m.BusinessPlace_AddressStateCode, new SelectList(Model.stateMaster, "State_Code", "State_Name"), "Select State", new { @class = "form-control", id = "ddlBState" })
                @Html.ValidationMessageFor(m => m.BusinessPlace_AddressStateCode, "", new { @class = "text-danger" })
            </div>
            <div class="form-group col-md-2">
                @Html.LabelFor(m => m.BusinessPlace_AddressDistrictCode) <span class="text-danger">*</span>
                @Html.DropDownListFor(m => m.BusinessPlace_AddressDistrictCode, new SelectList(Model.districtMaster, "District_Code", "District_Name"), "Select District", new { @class = "form-control", id = "ddlBDistrict" })
                @Html.ValidationMessageFor(m => m.BusinessPlace_AddressDistrictCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group col-md-2">
            @Html.LabelFor(m => m.BusinessPlace_AddressPIN) <span class="text-danger">*</span>
            @Html.TextBoxFor(m => m.BusinessPlace_AddressPIN, new { @class = "form-control", placeholder = "PIN Code" })
            @Html.ValidationMessageFor(m => m.BusinessPlace_AddressPIN, "", new { @class = "text-danger" })
        </div>

        <div class="form-group col-md-4">
            @Html.LabelFor(m => m.SubDivision_Code, "Select Sub Division") <span class="text-danger">*</span>
            @Html.DropDownListFor(m => m.SubDivision_Code, new SelectList(Model.SubdivMaster, "SubDiv_Code", "SubDiv_Name"), "Select Sub Division", new { @class = "form-control", id = "ddlSubDiv" })
            @Html.ValidationMessageFor(m => m.SubDivision_Code, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="border p-3 mb-3">
        <h5>Address for Official Communication</h5>
        <div class="form-group form-check">
            @Html.CheckBoxFor(m => m.IsSameBussinessAdd_CommAdd, new { @class = "form-check-input", id = "chkSameAsBusiness" })
            <label class="form-check-label" for="chkSameAsBusiness">Is Your Communication Address same as Business Address?</label>
        </div>

        <div id="commAddressFields">
            <div class="form-row">
                <div class="form-group col-md-4">
                    @Html.LabelFor(m => m.BComm_AddressLine1)
                    @Html.TextBoxFor(m => m.BComm_AddressLine1, new { @class = "form-control", placeholder = "Address Line 1" })
                    @Html.ValidationMessageFor(m => m.BComm_AddressLine1, "", new { @class = "text-danger" })
                </div>
                <div class="form-group col-md-4">
                    @Html.LabelFor(m => m.BComm_AddressLine2)
                    @Html.TextBoxFor(m => m.BComm_AddressLine2, new { @class = "form-control", placeholder = "Address Line 2" })
                </div>
                <div class="form-group col-md-2">
                    @Html.LabelFor(m => m.BComm_AddressStateCode)
                    @Html.DropDownListFor(m => m.BComm_AddressStateCode, new SelectList(Model.stateMaster, "State_Code", "State_Name"), "Select State", new { @class = "form-control", id = "ddlCommState" })
                    @Html.ValidationMessageFor(m => m.BComm_AddressStateCode, "", new { @class = "text-danger" })
                </div>
                <div class="form-group col-md-2">
                    @Html.LabelFor(m => m.BComm_AddressDistrictCode)
                    @Html.DropDownListFor(m => m.BComm_AddressDistrictCode, new SelectList(Model.districtMaster, "District_Code", "District_Name"), "Select District", new { @class = "form-control", id = "ddlCommDistrict" })
                    @Html.ValidationMessageFor(m => m.BComm_AddressDistrictCode, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group col-md-2">
                @Html.LabelFor(m => m.BComm_AddressPIN)
                @Html.TextBoxFor(m => m.BComm_AddressPIN, new { @class = "form-control", placeholder = "PIN Code" })
                @Html.ValidationMessageFor(m => m.BComm_AddressPIN, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <button type="submit" class="btn btn-primary">Update</button>
    <button type="reset" class="btn btn-secondary ml-2">Reset</button>
}

@section Scripts {
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <script>
        // Toggle communication address fields based on checkbox
        $(document).ready(function () {
            function toggleCommAddress() {
                if ($('#chkSameAsBusiness').is(':checked')) {
                    $('#commAddressFields').hide();
                } else {
                    $('#commAddressFields').show();
                }
            }

            toggleCommAddress();
            $('#chkSameAsBusiness').change(toggleCommAddress);

            // State-District cascading dropdown for Permanent Residence
            $('#ddlPState').change(function () {
                var stateCode = $(this).val();
                $.ajax({
                    url: '@Url.Action("GetDistrictsByState", "Agent")',
                    data: { stateCode: stateCode },
                    success: function (data) {
                        var $district = $('#ddlPDistrict');
                        $district.empty().append('<option value="">Select District</option>');
                        $.each(data, function (i, item) {
                            $district.append('<option value="' + item.District_Code + '">' + item.District_Name + '</option>');
                        });
                    }
                });
            });

            // Business Address State-District cascading
            $('#ddlBState').change(function () {
                var stateCode = $(this).val();
                $.ajax({
                    url: '@Url.Action("GetDistrictsByState", "Agent")',
                    data: { stateCode: stateCode },
                    success: function (data) {
                        var $district = $('#ddlBDistrict');
                        $district.empty().append('<option value="">Select District</option>');
                        $.each(data, function (i, item) {
                            $district.append('<option value="' + item.District_Code + '">' + item.District_Name + '</option>');
                        });
                    }
                });
            });

            // Communication Address State-District cascading
            $('#ddlCommState').change(function () {
                var stateCode = $(this).val();
                $.ajax({
                    url: '@Url.Action("GetDistrictsByState", "Agent")',
                    data: { stateCode: stateCode },
                    success: function (data) {
                        var $district = $('#ddlCommDistrict');
                        $district.empty().append('<option value="">Select District</option>');
                        $.each(data, function (i, item) {
                            $district.append('<option value="' + item.District_Code + '">' + item.District_Name + '</option>');
                        });
                    }
                });
            });
        });
    </script>
}

if i click on check button for is address same as permant address, will it populate all data in textboxes?