<?php 
if (!defined('IN_SITE')) {
    die('The Request Not Found');
}
$title = 'Chỉnh Sửa Thành Viên | ' . $DVR->site('title');
$body = [
    'title' => 'Chỉnh sửa thành viên'
];
$body['header'] = '';
$body['footer'] = '';
require_once(__DIR__.'/Header.php');
require_once(__DIR__.'/Sidebar.php');
require_once(__DIR__ . '/../../../core/is_user.php');
CheckLogin();
CheckAdmin();
?>
<?php
if(isset($_GET['id']))
{
    $row = $DVR->get_row(" SELECT * FROM `gdv` WHERE `id` = '".check_string($_GET['id'])."'  ");
    if(!$row)
    {
        echo "éo tồn tại";
    }
}
else
{
    
}
?>
<div class="main-content app-content">
    <div class="container-fluid">
        <div class="d-md-flex d-block align-items-center justify-content-between my-4 page-header-breadcrumb">
            <h1 class="page-title fw-semibold fs-18 mb-0">
                <a type="button" class="btn btn-dark btn-raised-shadow btn-wave btn-sm me-1" href="/admin/list-gdv">
                    <i class="fa-solid fa-arrow-left"></i>
                </a> 
                Sửa GDV <?=$row['name'];?>
            </h1>
        </div>
        <form id="form-gdv" method="POST" enctype="multipart/form-data">
            <input type="hidden" name="token" value="<?=$getUser['token']?>" id="token">
             <input type="hidden" name="id" value="<?=check_string($_GET['id'])?>" id="id">
            <div class="row">
                <div class="col-xl-8">
                    <div class="card custom-card">
                        <div class="card-header justify-content-between">
                            <div class="card-title">
                                Chỉnh Sửa GDV
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row mb-3 gy-2">
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Chức Vụ: <span class="text-danger">*</span></label>
                                    <select class="form-control" name="dich_vu" required>
                                        <option value="">-- Chọn loại --</option>
                                        <?php foreach ($DVR->get_list("SELECT * FROM `category`") as $dvr) {?>
                                            <option value="<?=$dvr['code'];?>" <?= ($dvr['code'] == $row['dich_vu']) ? 'selected' : ''; ?>><?=$dvr['code'];?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Họ Và Tên <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" placeholder="Nhập Họ Tên" name="title" value="<?=$row['name'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Link Ảnh Đại Diện <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" placeholder="Nhập Link Ảnh" name="image" value="<?=$row['image'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Số Tiền Cọc: <span class="text-danger">*</span></label>
                                    <input type="number" class="form-control" placeholder="Nhập Số Tiền" name="money" value="<?=$row['money'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">SĐT: <span class="text-danger">*</span></label>
                                    <input type="number" class="form-control" name="sdt" placeholder="Nhập Số ĐT" value="<?=$row['sdt'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">telegram: <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="telegram" placeholder="Nhập id telegram" value="<?=$row['telegram'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Link FB: <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="linkfb" placeholder="https://www.facebook.com/g.phu2712" value="<?=$row['linkfb'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">UID FB: <span class="text-danger">*</span></label>
                                    <input type="number" class="form-control" name="idfb" placeholder="Nhập UID FB" value="<?=$row['id_fb'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">WEBSTIE <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="website" placeholder="Nhập link website" value="<?=$row['website'];?>">
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Dịch Vụ Cung Cấp <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="dichvucungcap" placeholder="Nhập Dịch Vụ Mà GDV cung cấp" value="<?=$row['dichvucungcap'];?>">
                                    <small>Mỗi Dịch Vụ Cách Nhau Bằng Dấu (|)</small>
                                </div>
                                <div class="col-sm-12 mb-2">
                                    <label class="form-label" for="example-hf-email">Ngày Cọc: <span class="text-danger">*</span></label>
                                    <input type="text" class="form-control" name="ngay" placeholder="01-01-2000" value="<?=$row['ngay'];?>">
                                </div>
                                <div class="col-12 mb-2" id="rowItem">
                              <?php 
$i = 0; 
foreach ($DVR->get_list("SELECT * FROM `gdv` WHERE `id` = '".check_string($_GET['id'])."'") as $bank) {
    $lines = explode("\n", $bank['ngan_hang']);
    $stk = explode("\n", $bank['stk']); 
    $logos = [];

    // Kiểm tra nếu cả $lines và $stk không rỗng thì mới tiếp tục
    if (!empty($lines) && !empty($stk)) {
        // Duyệt qua từng cặp ngân hàng và STK
        foreach ($lines as $index => $line) {
            $short_name_to_check = trim($line);
            $short_stk_to_check = isset($stk[$index]) ? trim($stk[$index]) : '';
            if ($index == 0) {
?>
                <div class="row row5 row-item">
                    <div class="col-md-4">
                        <div class="section-form_item" id="bank">
                            <label class="form-label" for="">Ngân hàng</label>
                            <select class="form-control" data-trigger name="bank[]">
                                <option value="">--- chọn ngân hàng ---</option>
                                <?php foreach ($DVR->get_list("SELECT * FROM `api_logo`") as $logo) {?>
                                    <option value="<?=$logo['shortName'];?>" <?= ($logo['shortName'] == $short_name_to_check) ? 'selected' : ''; ?>><?=$logo['name'];?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-6 mb-2">
                        <div class="section-form_item">
                            <label class="form-label" for="">STK</label>
                            <input type="text" value="<?=$short_stk_to_check?>" class="form-theme_item__input form-control" name="account_num[]">
                        </div>
                    </div>
                    <div class="col-md-1" style="padding-left: 0px">
                        <div class="section-form_item">
                            <label class="form-label" for="">Action</label>
                            <a href="javascript:void(0)" class="btn btn-sm btn-success" id="addbank">Thêm</a>
                        </div>
                    </div>
                </div>
<?php 
            } else {
    $randomNumber = random_so();
?>
                <div class="row row5 row-item mt-2 delete-<?=$randomNumber?>">
                    <div class="col-md-6">
                        <div class="section-form_item" id="bank">
                            <label class="form-label" for="">Ngân hàng</label>
                            <select class="form-control" name="bank[]">
                                <option value="">--- chọn ngân hàng ---</option>
                                <?php foreach ($DVR->get_list("SELECT * FROM `api_logo`") as $logo) {?>
                                    <option value="<?=$logo['shortName'];?>" <?= ($logo['shortName'] == $short_name_to_check) ? 'selected' : ''; ?>><?=$logo['name'];?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="section-form_item">
                            <label class="form-label" for="">STK</label>
                            <input type="text" value="<?=$short_stk_to_check?>" class="form-theme_item__input form-control" name="account_num[]">
                        </div>
                    </div>
                   <div class="col-md-1" style="padding-left: 0px">
                       <label class="form-label" for="">Action</label>
                                <div class="section-form_item">
                                    <a href="javascript:void(0)" class="btn btn-sm btn-danger deleteItem" data-id="<?=$randomNumber?>">Delete</a>
                                </div>
                            </div>
                </div>
<?php 
            }
        }
    }
}
?>

                            </div>
                            </div>
                            <div class="d-grid gap-2 mb-3">
                                <button type="submit" id="submitgdv" class="btn btn-primary shadow-primary">
                                    <i class="fa fa-fw fa-save me-1"></i> Save
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-xl-4">
                    <div class="card custom-card">
                        <div class="card-body">
                            <p>Cảm ơn quý khách đã sử dụng code của chúng tôi mọi thắc mắc xin vui lòng ib tele @gphu2712.</p>
                            <p>chức năng này được tạo ra để admin có thể thêm được giao dịch viên.</p>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />

<script>
    $(".select2").select2();
    $("body").on("click", "#addbank", function () {
        let bankRender = $(this).closest('.row-item').find('#bank').clone();
        bankRender.find('option').attr('selected', false);
        var data_row = Date.now();
        let rowRender = `<div class="row row5 row-item mt-2 delete-${data_row}">
                            <div class="col-md-6">
                                <div class="section-form_item">
                                      <label class="form-label" for="">Ngân hàng</label>
                                                <select class="form-control" data-trigger name="bank[]">
                                                    <option value="">--- chọn ngân hàng -- </option>
                                                    <?php foreach ($DVR->get_list("SELECT * FROM `api_logo`") as $bank) {?>
                                            <option value="<?=$bank['shortName'];?>"><?=$bank['name'];?></option>
                                        <?php } ?>
                                                </select>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="section-form_item">
                                    <label class="form-label" for="">STK</label>
                                    <input type="text" value="" class="form-theme_item__input form-control" name="account_num[]">
                                </div>
                            </div>
                            <div class="col-md-1" style="padding-left: 0px">
                            <label class="form-label" for="">Action</label>
                                <div class="section-form_item">
                                    <a href="javascript:void(0)" class="btn btn-sm btn-danger deleteItem" data-id="${data_row}">Delete</a>
                                </div>
                            </div>
                        </div>`;
        $("#rowItem").append(rowRender);
    });
    $("body").on("click", ".deleteItem", function () {
        var item = $(this).attr("data-id");
        $(".delete-" + item).remove();
    });
    
    
     $("#submitgdv").on("click", function() {
        $('#submitgdv').html('<i class="fa fa-spinner fa-spin"></i> Đang xử lý...').prop(
            'disabled',
            true);

        var formData = new FormData(document.getElementById('form-gdv'));

        $.ajax({
            url: "/ajaxs/admin/update_gdv.php",
            type: "POST",
            dataType: "json",
            data: formData,
            processData: false,
            contentType: false,
            
            success: function(res) {
                if (res.status == 'success') {
                   showMessage(res.msg, "success");
                      setTimeout(function() {
                     location.reload(); }, 2000);
                } else {
                     showMessage(res.msg, res.status);
                }
                $('#submitgdv').html('Save').prop('disabled',
                    false);
            },
            error: function(xhr, status, error) {
                Swal.fire({
                    icon: 'error',
                    title: 'Lỗi',
                    text: 'Có lỗi xảy ra khi gửi đơn. Vui lòng thử lại!'
                });
            }
        });
    });
</script>>
<?php 
    require_once(__DIR__."/Footer.php");
?>