<?php

$pageTitle =
    'Địa chỉ giao hàng - PET SPA';

$pageStyles = [
    'customer.css'
];

$activeCustomerPage =
    'addresses';


$successMessage =
    $_SESSION['flash_success']
    ?? null;

$errorMessage =
    $_SESSION['flash_error']
    ?? null;


unset(
    $_SESSION['flash_success'],
    $_SESSION['flash_error']
);


require BASE_PATH
    . '/app/views/layouts/header.php';

require BASE_PATH
    . '/app/views/layouts/navbar.php';

?>

<main class="customer-area">

    <div class="container">

        <div class="row g-4">

            <div class="col-lg-3">

                <?php require BASE_PATH
                    . '/app/views/layouts/customer-sidebar.php';
                ?>

            </div>


            <div class="col-lg-9">

                <div
                    class="customer-page-header d-flex justify-content-between align-items-center"
                >

                    <div>

                        <h1>
                            Địa chỉ giao hàng
                        </h1>

                        <p>
                            Quản lý địa chỉ nhận hàng.
                        </p>

                    </div>


                    <button
                        class="btn btn-primary"
                        data-bs-toggle="modal"
                        data-bs-target="#addAddressModal"
                    >

                        <i class="bi bi-plus"></i>

                        Thêm địa chỉ

                    </button>

                </div>


                <?php if ($successMessage): ?>

                    <div class="alert alert-success">

                        <?= htmlspecialchars(
                            $successMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <?php if ($errorMessage): ?>

                    <div class="alert alert-danger">

                        <?= htmlspecialchars(
                            $errorMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <div class="row g-3">

                    <?php foreach (
                        $addresses
                        as $address
                    ): ?>

                        <div class="col-md-6">

                            <article class="address-card">

                                <?php if (
                                    (int)
                                    $address['is_default']
                                    === 1
                                ): ?>

                                    <span
                                        class="badge bg-success mb-3"
                                    >
                                        Mặc định
                                    </span>

                                <?php endif; ?>


                                <h3>

                                    <?= htmlspecialchars(
                                        $address['recipient_name']
                                    ) ?>

                                </h3>


                                <p>

                                    <i class="bi bi-telephone"></i>

                                    <?= htmlspecialchars(
                                        $address['phone']
                                    ) ?>

                                </p>


                                <p>

                                    <i class="bi bi-geo-alt"></i>

                                    <?= htmlspecialchars(
                                        implode(
                                            ', ',
                                            array_filter([
                                                $address['address_line'],
                                                $address['ward'],
                                                $address['district'],
                                                $address['province']
                                            ])
                                        )
                                    ) ?>

                                </p>


                                 <div
                                     class="d-flex gap-2 flex-wrap mt-3"
                                 >

                                    <button
                                        type="button"
                                        class="btn btn-sm btn-outline-primary"
                                        data-bs-toggle="collapse"
                                        data-bs-target="#editAddress<?= (int) $address['id'] ?>"
                                        aria-expanded="false"
                                        aria-controls="editAddress<?= (int) $address['id'] ?>"
                                    >
                                        Sửa
                                    </button>

                                     <?php if (
                                         (int)
                                        $address['is_default']
                                        !== 1
                                    ): ?>

                                        <form
                                            method="POST"
                                            action="<?= BASE_URL ?>/?url=customer/address/default"
                                        >

                                            <input
                                                type="hidden"
                                                name="_token"
                                                value="<?= htmlspecialchars(
                                                    $_SESSION['csrf_token']
                                                ) ?>"
                                            >

                                            <input
                                                type="hidden"
                                                name="address_id"
                                                value="<?= (int)
                                                    $address['id']
                                                ?>"
                                            >

                                            <button
                                                class="btn btn-sm btn-light"
                                                type="submit"
                                            >
                                                Đặt mặc định
                                            </button>

                                        </form>

                                    <?php endif; ?>


                                    <form
                                        method="POST"
                                        action="<?= BASE_URL ?>/?url=customer/address/delete"
                                        onsubmit="return confirm('Xóa địa chỉ này?')"
                                    >

                                        <input
                                            type="hidden"
                                            name="_token"
                                            value="<?= htmlspecialchars(
                                                $_SESSION['csrf_token']
                                            ) ?>"
                                        >

                                        <input
                                            type="hidden"
                                            name="address_id"
                                            value="<?= (int)
                                                $address['id']
                                            ?>"
                                        >

                                        <button
                                            type="submit"
                                            class="btn btn-sm btn-outline-danger"
                                        >
                                            Xóa
                                        </button>

                                    </form>

                                 </div>


                                <div
                                    class="collapse mt-3"
                                    id="editAddress<?= (int) $address['id'] ?>"
                                >

                                    <form
                                        method="POST"
                                        action="<?= BASE_URL ?>/?url=customer/address/update"
                                        class="border-top pt-3"
                                    >

                                        <input
                                            type="hidden"
                                            name="_token"
                                            value="<?= htmlspecialchars(
                                                $_SESSION['csrf_token']
                                            ) ?>"
                                        >

                                        <input
                                            type="hidden"
                                            name="address_id"
                                            value="<?= (int) $address['id'] ?>"
                                        >

                                        <?php if ((int) $address['is_default'] === 1): ?>

                                            <input
                                                type="hidden"
                                                name="is_default"
                                                value="1"
                                            >

                                        <?php endif; ?>


                                        <div class="row g-3">

                                            <div class="col-md-6">

                                                <label class="form-label">
                                                    Người nhận *
                                                </label>

                                                <input
                                                    type="text"
                                                    name="recipient_name"
                                                    class="form-control"
                                                    value="<?= htmlspecialchars(
                                                        $address['recipient_name']
                                                    ) ?>"
                                                    required
                                                >

                                            </div>


                                            <div class="col-md-6">

                                                <label class="form-label">
                                                    Số điện thoại *
                                                </label>

                                                <input
                                                    type="tel"
                                                    name="phone"
                                                    class="form-control"
                                                    value="<?= htmlspecialchars(
                                                        $address['phone']
                                                    ) ?>"
                                                    required
                                                >

                                            </div>


                                            <div class="col-12">

                                                <label class="form-label">
                                                    Số nhà, tên đường *
                                                </label>

                                                <input
                                                    type="text"
                                                    name="address_line"
                                                    class="form-control"
                                                    value="<?= htmlspecialchars(
                                                        $address['address_line']
                                                    ) ?>"
                                                    required
                                                >

                                            </div>


                                            <div class="col-md-4">

                                                <label class="form-label">
                                                    Phường / Xã
                                                </label>

                                                <input
                                                    type="text"
                                                    name="ward"
                                                    class="form-control"
                                                    value="<?= htmlspecialchars(
                                                        $address['ward'] ?? ''
                                                    ) ?>"
                                                >

                                            </div>


                                            <div class="col-md-4">

                                                <label class="form-label">
                                                    Quận / Huyện
                                                </label>

                                                <input
                                                    type="text"
                                                    name="district"
                                                    class="form-control"
                                                    value="<?= htmlspecialchars(
                                                        $address['district'] ?? ''
                                                    ) ?>"
                                                >

                                            </div>


                                            <div class="col-md-4">

                                                <label class="form-label">
                                                    Tỉnh / Thành phố
                                                </label>

                                                <input
                                                    type="text"
                                                    name="province"
                                                    class="form-control"
                                                    value="<?= htmlspecialchars(
                                                        $address['province'] ?? ''
                                                    ) ?>"
                                                >

                                            </div>


                                            <?php if ((int) $address['is_default'] !== 1): ?>

                                                <div class="col-12">

                                                    <div class="form-check">

                                                        <input
                                                            class="form-check-input"
                                                            type="checkbox"
                                                            name="is_default"
                                                            id="editDefault<?= (int) $address['id'] ?>"
                                                        >

                                                        <label
                                                            class="form-check-label"
                                                            for="editDefault<?= (int) $address['id'] ?>"
                                                        >
                                                            Đặt làm địa chỉ mặc định
                                                        </label>

                                                    </div>

                                                </div>

                                            <?php endif; ?>

                                        </div>


                                        <button
                                            type="submit"
                                            class="btn btn-primary btn-sm mt-3"
                                        >
                                            Lưu thay đổi
                                        </button>

                                    </form>

                                </div>

                             </article>

                        </div>

                    <?php endforeach; ?>


                    <?php if (
                        empty($addresses)
                    ): ?>

                        <div class="col-12">

                            <div class="customer-empty">

                                <i class="bi bi-geo-alt"></i>

                                <p>
                                    Bạn chưa lưu địa chỉ nào.
                                </p>

                            </div>

                        </div>

                    <?php endif; ?>

                </div>

            </div>

        </div>

    </div>

</main>


<!-- ADD ADDRESS -->

<div
    class="modal fade"
    id="addAddressModal"
    tabindex="-1"
>

    <div class="modal-dialog modal-lg">

        <div class="modal-content">

            <form
                method="POST"
                action="<?= BASE_URL ?>/?url=customer/address/create"
            >

                <input
                    type="hidden"
                    name="_token"
                    value="<?= htmlspecialchars(
                        $_SESSION['csrf_token']
                    ) ?>"
                >


                <div class="modal-header">

                    <h5 class="modal-title">
                        Thêm địa chỉ
                    </h5>

                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="modal"
                    ></button>

                </div>


                <div class="modal-body">

                    <div class="row g-3">

                        <div class="col-md-6">

                            <label class="form-label">
                                Người nhận *
                            </label>

                            <input
                                type="text"
                                name="recipient_name"
                                class="form-control"
                                required
                            >

                        </div>


                        <div class="col-md-6">

                            <label class="form-label">
                                Số điện thoại *
                            </label>

                            <input
                                type="tel"
                                name="phone"
                                class="form-control"
                                required
                            >

                        </div>


                        <div class="col-12">

                            <label class="form-label">
                                Số nhà, tên đường *
                            </label>

                            <input
                                type="text"
                                name="address_line"
                                class="form-control"
                                required
                            >

                        </div>


                        <div class="col-md-4">

                            <label class="form-label">
                                Phường / Xã
                            </label>

                            <input
                                type="text"
                                name="ward"
                                class="form-control"
                            >

                        </div>


                        <div class="col-md-4">

                            <label class="form-label">
                                Quận / Huyện
                            </label>

                            <input
                                type="text"
                                name="district"
                                class="form-control"
                            >

                        </div>


                        <div class="col-md-4">

                            <label class="form-label">
                                Tỉnh / Thành phố
                            </label>

                            <input
                                type="text"
                                name="province"
                                class="form-control"
                            >

                        </div>


                        <div class="col-12">

                            <div class="form-check">

                                <input
                                    class="form-check-input"
                                    type="checkbox"
                                    name="is_default"
                                    id="newDefault"
                                >

                                <label
                                    class="form-check-label"
                                    for="newDefault"
                                >
                                    Đặt làm địa chỉ mặc định
                                </label>

                            </div>

                        </div>

                    </div>

                </div>


                <div class="modal-footer">

                    <button
                        type="button"
                        class="btn btn-light"
                        data-bs-dismiss="modal"
                    >
                        Hủy
                    </button>

                    <button
                        type="submit"
                        class="btn btn-primary"
                    >
                        Lưu địa chỉ
                    </button>

                </div>

            </form>

        </div>

    </div>

</div>


<?php

require BASE_PATH
    . '/app/views/layouts/footer.php';

?>
