<?= $this->extend('Modules\Admin\Views\index'); ?>
<?= $this->section('content');?>
<div class="container-fluid">
<div class="row mb-md-3">
	<div class="col">
		<h4>Edit Menu Item</h4>
		<!-- Breadcrumb !-->
		<nav aria-label="breadcrumb"><small>
			<ol class="breadcrumb mb-0">
				<li class="breadcrumb-item"><a href="<?= base_url('administrator') ?> ">Home</a></li>
                <li class="breadcrumb-item"><a href="<?= base_url('administrator/menu/group/'.$menuItem->category_alias); ?> "><?= $menuItem->menu_category ?></a></li>
				<li class="breadcrumb-item active" aria-current="page"><?= esc($bcrumb) ?> </li>
			</ol></small>
		</nav>
		<!-- End Breadcrumb !-->
	</div>
</div>
<!-- Notification messages !-->
<div class="row">
    <div class="col">
		<!-- print success message !-->
		<?php if(session()->getTempdata('success')): ?>
			<div class="alert alert-success" role="alert"><?= session()->getTempdata('success');?></div>
		<?php endif;?>
		<!-- print failure message !-->
		<?php if(session()->getTempdata('error')): ?>
			<div class="alert alert-danger" role="alert"><?= session()->getTempdata('error');?></div>
		<?php endif;?>
        <div id="showMsg"></div>
</div></div>
    <!-- Pages Detailed List !-->
    <div class="card card-body">
        <div class="mb-2"><h5>&nbsp; <span class="float-end"><a href="<?= base_url('administrator/menu/groups'); ?>" class="btn btn-sm btn-dark rounded-0 float-sm-end">Back to Groups</a> <a href="<?= base_url('administrator/menu/group/'.$menuItem->category_alias); ?>" class="btn btn-sm btn-warning rounded-0 float-sm-end">Back to <?= $menuItem->menu_category; ?></a></span></h5></div>
        <div class="alert alert-warning">
        <strong>Edit Menu</strong>:  
<small>With LT CMS, you have the flexibility to easily create/edit menus based on your preferred menu type [By <strong>Page</strong> | <strong>Post Category</strong> | <strong>Single Post</strong>]. Simply define the desired category for each menu to ensure proper categorization. Incase you did not create a category, start from here <a href="<?= base_url('administrator/menu/groups'); ?>">Create Category</a></small>
        </div>
        <?= form_open('administrator/menu/item/update'); ?>
        <div class="row g-md-5">
            <div class="col-lg-6 col-md-7">
                <div class="mb-3">
                    <label class="form-label fw-bold" for="name">Name *</label>
                    <input type="text" class="form-control " name="title" value="<?= $menuItem->title; ?>" placeholder="Add Menu Name" required/>
                </div><!-- Menu name !-->
				<div class="mb-3">
                    <label class="form-label fw-bold" for="menuType">Menu Type</label>
                    <select class="form-select" name="menu_type" id="menuType" required>
						<option value="">Select Menu Type</option>
						<option value="page" <?php if($menuItem->menu_type == "page") echo "selected" ?>>Page</option>
						<option value="category" <?php if($menuItem->menu_type == "category") echo "selected" ?>>Category</option>
						<option value="post" <?php if($menuItem->menu_type == "post") echo "selected" ?>>Post</option>
                        <option value="custom" <?php if($menuItem->menu_type == "custom") echo "selected" ?>>Custom</option>
					</select>
                </div><!-- Menu Type to help you pick right content !-->
				<p class="text-muted w-75"><small>In this section we display the different user options basing on the selected menu type</small></p>

                <?php if($menuItem->menu_type == "page"):?>
				<div>
					<div class="mb-3">
						<label class="form-label" for="pageid"><strong>Choose page to connect to Menu</strong> </label>
						<select name="page_id" class="form-select form-select-lg " id="pageid" >
							<option value="">Choose page here</option>
							<?php foreach($getPages as $page):?>
								<option value="<?= $page->id; ?>" <?php if($menuItem->post_id == $page->id) echo 'selected'; ?>><?= $page->title; ?></option>
							<?php endforeach; ?>
                        </select>
					</div>
				</div><!-- Pages  !-->
                <?php endif;?>
                <?php if($menuItem->menu_type == "category"):?>
				
				<div class="mb-3">
                    <?php function generateSelectOptions($categories, $indent = '', $menuItem = null) { ?>
                        <?php foreach ($categories as $category) { ?>
                            <option value="<?= $category['id'] ?>" <?php if ($menuItem && $category['id'] == $menuItem->post_cat) echo "selected"; ?>><?= $indent . $category['name'] ?></option>
                            <?php if (isset($category['subcategories']) && is_array($category['subcategories'])) { ?>
                                <?php generateSelectOptions($category['subcategories'], $indent . '- ', $menuItem); ?>
                            <?php } ?>
                        <?php } ?>
                    <?php } ?>
                    <label class="form-label"><strong>Category</strong></label>
                    <select name="post_category" class="form-select" required>
                        <option value="">Select Category</option>
                        <?php generateSelectOptions($categoryTree, '', $menuItem); ?>
                    </select>
                </div><!-- Post Category !-->
                <?php endif;?>
                <?php if($menuItem->menu_type == "post"):?>
				<div>
					<div class="mb-3">
						<label class="form-label" for="catid"><strong>Select Single Post for the menu</strong></label>
                        <select name="post_id" class="form-select form-select-lg" id="catid">
							<option value="">Choose Post here</option>
							<?php foreach($getPosts as $p):?>
								<option value="<?= $p->id; ?>" <?php if($menuItem->post_id == $p->id) echo 'selected'; ?>><?= $p->title; ?></option>
							<?php endforeach; ?>
                        </select>
					</div>
				</div><!-- Single Post   !-->
                <?php endif;?>
                <?php if($menuItem->menu_type == "custom"):?>
                <div >
					<div class="mb-3">
						<label class="form-label" for="customlink"><strong>Enter link to your favourite website</strong></label>
						<input type="text" name="custom" value="<?= $menuItem->link; ?>" class="form-control form-control-lg" id="customlink" placeholder="Enter full website link here">
					</div>
				</div><!-- Custom Link  !-->
                <?php endif;?>

            </div><!-- Left Col !-->
            <div class="col-lg-4 col-md-5 ">
				<div class="mb-3">
                    <label class="form-label fw-bold">Menu Category</label>
                    <select class="form-select" name="category_id" class="bg-warning bg-opacity-10" required>
						<option value="">Select Menu Category</option>
						<?php foreach($menuGroups as $group):?>
						<option value="<?= $group->id ?>" <?php if($menuItem->category_id == $group->id) echo "selected" ?> ><?= $group->name ?></option>
						<?php endforeach; ?>
					</select>
                </div><!-- Menu Categories or Menu Groups !-->
				<div class="mb-3">
                    <label class="form-label fw-bold">Parent Menu Item</label>
                    <select class="form-select" name="parent_id" >
						<option value="">Select Parent </option>
						<?php foreach($menus as $m):?>
						<option value="<?= $m->id ?>" <?php if($menuItem->parent_id == $m->id) echo "selected" ?>><?= $m->title ?></option>
						<?php endforeach; ?>
					</select>
                </div><!-- Parent Item !-->
				<div class="mb-3">
                    <label class="form-label fw-bold">Level</label>
                    <select class="form-select" name="level" >
						<option value="1" <?php if($menuItem->level == "1") echo "selected" ?>>Level 1</option>
						<option value="2" <?php if($menuItem->level == "2") echo "selected" ?>>Level 2</option>
						<option value="3" <?php if($menuItem->level == "3") echo "selected" ?>>level 3</option>
						<option value="4" <?php if($menuItem->level == "4") echo "selected" ?>>level 4</option>
					</select>
                </div><!-- Menu Level !-->
				<div class="mb-3">
                    <label class="form-label"><strong>Status</strong></label>
                    <select name="status" class="form-select bg-success bg-opacity-75 text-white" >
                        <option value="published" <?php if($menuItem->status == "published") echo "selected" ?>>Published</option>
                        <option value="unpublished" <?php if($menuItem->status == "unpublished") echo "selected" ?>>Unpublished</option>
                        <option value="archived" <?php if($menuItem->status == "archived") echo "selected" ?>>Archived</option>
                        <option value="trashed" <?php if($menuItem->status == "trashed") echo "selected" ?>>Trashed</option>
                    </select>
                  </div>
                  <div class="mb-3">
                    <label class="form-label"><strong>Access</strong></label>
                    <select name="access" class="form-select">
                        <option value="Public" <?php if($menuItem->access == "Public") echo "selected" ?>>Public</option>
                        <option value="Private" <?php if($menuItem->access == "Private") echo "selected" ?>>Private</option>
                        <option value="Users" <?php if($menuItem->access == "Users") echo "selected" ?>>Users</option>
                    </select>
                  </div>
			</div>
			<div class="col-lg-2 col-md-2 col-sm-12 d-none d-lg-block">
				<div class="alert alert-primary">
					
				</div>
			</div><!-- notice !-->
        </div><!-- row !-->
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 mb-3">
                <label class="form-label fw-bold">Menu Link</label>
                <input type="text" class="form-control form-control-lg" name="link" value="<?= $menuItem->link ?>" placeholder="Automatically generated link" readonly/>
            </div><!-- Menu Link !-->
            <div class="col-lg-12 col-md-12 col-sm-12">
                <input type="hidden" name="id" value="<?= $menuItem->id; ?>" />
                <button type="reset" class="btn btn-lg btn-secondary">Reset</button>
                <button type="submit" class="btn btn-lg btn-danger">Edit Menu Item</button>
            </div><!-- Buttons !--> 
        </div><!-- row !-->
        <?= form_close(); ?>
        
    </div>

</div><!-- Container !-->     

<!-- Modal !-->
 

<!---- JS --->  
 
<script src="<?= base_url();?>/assets/js/modules/admin-menu.js"></script>  
<?= $this->endSection();?>