<?= $this->extend('template/layout') ?>

<?= $this->section('title') ?>Edit Data Nasabah - <?= env('app.name') ?><?= $this->endSection() ?>

<!-- main section start-->
<?= $this->section('main') ?>
<?php
    $uri      = current_url(true);
    $segment1 = $uri->setSilent()->getSegment(1);
    $segment2 = $uri->setSilent()->getSegment(2);
    $segment3 = $uri->setSilent()->getSegment(3);
?>
<!-- Breadcumb -->
<ol class="breadcrumb page-breadcrumb">
    <li class="breadcrumb-item"><a href="javascript:void(0);"><?= env('app.name') ?></a></li>
    <li class="breadcrumb-item"><?= ucfirst($segment1) ?></li>
    <li class="breadcrumb-item">Data Nasabah</li>
    <li class="breadcrumb-item">Edit</li>
    <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
</ol>
<div class="row">
    <div class="col-xl-12">
        <div id="panel-1" class="panel">
            <div class="panel-hdr border-faded border-top-1 border-right-0 border-left-0 shadow-0">
                <h2>Edit Data Nasabah</h2>
                <div class="panel-toolbar pr-3">
                    <a href="<?= $back ?>" class="btn btn-secondary btn-sm"><i class="fal fa-arrow-left"></i> KEMBALI</a>
                </div>
            </div>

            <div class="panel-container show">
                <div class="panel-content">
                    <div class="row">
                        <div class="col-12 col-sm-6">
                            <?= form_open_multipart("", 'id="nasabah_form"', ['id' => $id, 'foto_ktp_lama' => $foto_ktp, 'foto_closing_lama' => $foto_closing]) ?>
                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Nama:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="text" id="nama_calon" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginputx" name="nama_calon" value="<?= $nama_calon ?>" placeholder="Nama Nasabah" maxlength="100">
                                        <div class="invalid-feedback" id="em_nama_calon"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Tgl. Lahir:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="date" id="tgl_lahir" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent" name="tgl_lahir" value="<?= $tgl_lahir ?>">
                                        <div class="invalid-feedback" id="em_tgl_lahir"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Alamat:</label>
                                    <div class="col-9 col-sm-9">
                                        <textarea id="alamat" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginputx" name="alamat" rows="3" placeholder="Alamat" maxlength="200"><?= $alamat ?></textarea>
                                        <div class="invalid-feedback" id="em_alamat"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">No. WA:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="text" id="no_wa" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent" name="no_wa" value="<?= $no_wa ?>" placeholder="Nomor WhatsApp" maxlength="15">
                                        <div class="invalid-feedback" id="em_no_wa"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">NIK:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="text" id="nik" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent" name="nik" value="<?= $nik ?>" placeholder="NIK" maxlength="16">
                                        <div class="invalid-feedback" id="em_nik"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Email:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="email" id="emailx" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginputx" name="emailx" value="<?= $emailx ?>" placeholder="Email Nasabah" maxlength="100">
                                        <div class="invalid-feedback" id="em_emailx"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Nama Ibu:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="text" id="maiden" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginputx" name="maiden" value="<?= $maiden ?>" placeholder="Nama Gadis Ibu Kandung" maxlength="150">
                                        <div class="invalid-feedback" id="em_maiden"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Pekerjaan:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="text" id="pekerjaan" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent ginputx" name="pekerjaan" value="<?= $pekerjaan ?>" placeholder="Pekerjaan/ Sumber Penghasilan" maxlength="150">
                                        <div class="invalid-feedback" id="em_pekerjaan"></div>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Ket.:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="text" id="ket" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent" name="ket" value="<?= $ket ?>" placeholder="Keterangan Lainnya (jika ada)" maxlength="200">
                                        <div class="invalid-feedback" id="em_ket"></div>
                                    </div>
                                </div>
                            </form>
                        </div>

                        <div class="col-12 col-sm-6">
                            <div class="form-group">
                                <h5 class="mb-3">Dokumen</h5>
                                
                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Foto KTP:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="file" id="foto_ktp" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent" name="foto_ktp" accept="image/*" form="nasabah_form">
                                        <div class="invalid-feedback" id="em_foto_ktp"></div>
                                        <?php if (!empty($foto_ktp) && file_exists(ROOTPATH . 'public/fotoktp/' . $foto_ktp)) { ?>
                                            <div class="mt-2">
                                                <img src="<?= base_url('fotoktp/' . $foto_ktp) ?>" alt="Foto KTP" class="img-thumbnail" style="max-width: 200px; max-height: 150px;">
                                                <br><small class="text-muted">Foto saat ini: <?= $foto_ktp ?></small>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>

                                <div class="row mb-3">
                                    <label class="col-3 col-sm-3 col-form-label text-right">Foto Closing:</label>
                                    <div class="col-9 col-sm-9">
                                        <input type="file" id="foto_closing" class="form-control rounded-0 border-top-0 border-left-0 border-right-0 px-0 bg-transparent" name="foto_closing" accept="image/*" form="nasabah_form">
                                        <div class="invalid-feedback" id="em_foto_closing"></div>
                                        <?php if (!empty($foto_closing) && file_exists(ROOTPATH . 'public/fotoclosing/' . $foto_closing)) { ?>
                                            <div class="mt-2">
                                                <img src="<?= base_url('fotoclosing/' . $foto_closing) ?>" alt="Foto Closing" class="img-thumbnail" style="max-width: 200px; max-height: 150px;">
                                                <br><small class="text-muted">Foto saat ini: <?= $foto_closing ?></small>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>

                                <hr>
                                <div class="invalid-feedback text-center" id="em_message"></div>
                                <div class="clearfix">
                                    <button type="submit" id="btn_nasabah" class="btn btn-primary waves-effect waves-themed float-right" form="nasabah_form"><?= $button ?></button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div><!-- end panel 1 -->
    </div><!-- end col-xl-6 -->
</div><!-- end row -->
<?= $this->endSection() ?>
<!-- main section end-->

<!-- pageScripts section start-->
<?= $this->section('pageScripts') ?>
<script type="text/javascript">
    $('.ginputx').inputmask({
        regex: "^[a-zA-Z0-9\\s.,@-]*$"
    });

    $('#no_wa').inputmask({
        regex: "^[0-9]*$"
    });

    $('#nik').inputmask({
        regex: "^[0-9]*$"
    });

    $("#nasabah_form").submit(function(e) {
        e.preventDefault();
        var url = '<?= $action ?>';
        var formData = new FormData(this);
        
        $.ajax({
            url: url,
            type: "POST",
            dataType: 'json',
            data: formData,
            processData: false,
            contentType: false,
            beforeSend: function() {
                // Clear previous validation errors
                $(".form-control").removeClass("is-invalid");
                $(".invalid-feedback").html('').hide();
                $("#em_message").html('').hide();
                
                // Show loading button
                BtnLoading($("#btn_nasabah"));
            },
            error: function(xhr, status, message) {
                try {
                    var res = JSON.parse(xhr.responseText);
                    
                    // Handle validation errors
                    if (res.messages) {
                        $(res.messages).each(function(i, val) {
                            var inputname = Object.keys(val)[0];
                            $.each(val, function(k, v) {
                                $("#" + k).addClass("is-invalid");
                                $("#em_" + k).html(v).fadeIn();
                                $("#" + inputname).focus();
                            });
                        });
                    }
                    
                    // Handle general error message
                    if (res.message) {
                        $("#em_message").html(res.message).fadeIn();
                    }
                    
                } catch (e) {
                    console.error('Error parsing response:', e);
                    $("#em_message").html('Terjadi kesalahan sistem').fadeIn();
                }
            },
            success: function(res) {
                // Show success message
                toastr.success(res.message, 'Berhasil!', {
                    timeOut: 2000,
                    onHidden: function() {
                        // Redirect to list page after toast
                        window.location.href = '<?= $back ?>';
                    }
                });
            },
            complete: function() {
                // Reset button state
                BtnReset($("#btn_nasabah"));
            }
        });
    });
</script>
<?= $this->endSection() ?>
<!-- pageScripts section end-->
