<?= $this->extend('admin/layout/default') ?>
<?= $this->section('content') ?>

<section class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1>Edit Shop</h1>
      </div>
      <div class="col-sm-6">
        <ol class="breadcrumb float-sm-right">
          <li class="breadcrumb-item"><a href="<?php echo url('/') ?>">Home</a></li>
          <li class="breadcrumb-item"><a href="<?php echo url('shops') ?>">Shops</a></li>
          <li class="breadcrumb-item active">Edit</li>
        </ol>
      </div>
    </div>
  </div>
</section>

<section class="content">
<?php echo form_open('shops/update/'.$shop->id, ['class' => 'form-validate', 'autocomplete' => 'off', 'enctype' => 'multipart/form-data']); ?>
  <div class="card">
    <div class="card-body">
      <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" name="name" value="<?php echo esc($shop->name) ?>" required autofocus>
      </div>
      <div class="form-row">
        <div class="form-group col-md-6">
          <label>Phone</label>
          <input type="text" class="form-control" name="phone" value="<?php echo esc($shop->phone) ?>">
        </div>
        <div class="form-group col-md-6">
          <label>Status</label>
          <select class="form-control" name="status">
            <option value="1" <?php echo $shop->status ? 'selected' : '' ?>>Active</option>
            <option value="0" <?php echo !$shop->status ? 'selected' : '' ?>>Inactive</option>
          </select>
        </div>
      </div>
      <div class="form-group">
        <label>Address</label>
        <textarea class="form-control" name="address" rows="3"><?php echo esc($shop->address) ?></textarea>
      </div>
      <div class="form-group">
        <label>City</label>
        <select name="city_id" class="form-control select2">
          <option value="">Select</option>
          <?php foreach ($cities as $c): ?>
            <?php $sel = (int) ($shop->city_id ?? 0) === (int) $c->id ? 'selected' : '' ?>
            <option value="<?php echo $c->id ?>" <?php echo $sel ?>><?php echo esc($c->label) ?></option>
          <?php endforeach ?>
        </select>
      </div>
      <div class="form-group">
        <label>Category</label>
        <select name="category_id" class="form-control select2">
          <option value="">Select</option>
          <?php foreach ($categories as $cat): ?>
            <?php $sel = (int) ($shop->category_id ?? 0) === (int) $cat->id ? 'selected' : '' ?>
            <option value="<?php echo $cat->id ?>" <?php echo $sel ?>><?php echo esc($cat->name) ?></option>
          <?php endforeach ?>
        </select>
      </div>
      <div class="form-group">
        <label>Image</label>
        <input type="file" class="form-control" name="image" accept="image/*">
        <?php if (!empty($shop->img_type)): ?>
          <div class="mt-2">
            <img src="<?php echo urlUpload('shops/'.$shop->id.'.'.$shop->img_type, true) ?>" style="max-height: 80px;">
          </div>
        <?php endif ?>
      </div>
      <div class="form-group">
        <label>Slug</label>
        <input type="text" class="form-control" value="<?php echo esc($shop->slug) ?>" readonly>
        <div class="custom-control custom-checkbox mt-2">
          <input class="custom-control-input" type="checkbox" id="regenerate_slug" name="regenerate_slug" value="1">
          <label for="regenerate_slug" class="custom-control-label">Regenerate slug from name</label>
        </div>
      </div>
      <div class="form-group">
        <label>Owner (Shopkeeper user)</label>
        <select name="owner_user_id" class="form-control select2">
          <option value="">Select</option>
          <?php foreach ($shopkeepers as $u): ?>
            <?php $sel = (int) $ownerUserId === (int) $u->id ? 'selected' : '' ?>
            <option value="<?php echo $u->id ?>" <?php echo $sel ?>><?php echo esc($u->name) ?> (<?php echo esc($u->username ?? $u->email) ?>)</option>
          <?php endforeach ?>
        </select>
      </div>
      <hr>
      <h5 class="mb-3">Subscription</h5>
      <div class="form-group">
        <label>Subscription Plan</label>
        <select name="subscription_id" class="form-control select2" required>
          <option value="">Select</option>
          <?php foreach ($subscriptions as $sub): ?>
            <?php $subSel = (int) ($shopSubscription->subscription_id ?? 0) === (int) $sub->id ? 'selected' : '' ?>
            <option value="<?php echo $sub->id ?>" <?php echo $subSel ?>><?php echo esc($sub->title) ?> (<?php echo (int) ($sub->duration_months ?? 0) ?> months - <?php echo esc($sub->price ?? 0) ?>)</option>
          <?php endforeach ?>
        </select>
      </div>
      <div class="form-row">
        <div class="form-group col-md-6">
          <label>Subscription Start at (Y-m-d H:i:s)</label>
          <input type="text" class="form-control" name="subscription_start_at" value="<?php echo esc($shopSubscription->start_at ?? date('Y-m-d H:i:s')) ?>">
        </div>
        <div class="form-group col-md-6">
          <label>Subscription Status</label>
          <?php $subStatus = (string) ($shopSubscription->status ?? 'ACTIVE') ?>
          <select name="subscription_status" class="form-control">
            <option value="ACTIVE" <?php echo $subStatus === 'ACTIVE' ? 'selected' : '' ?>>ACTIVE</option>
            <option value="EXPIRED" <?php echo $subStatus === 'EXPIRED' ? 'selected' : '' ?>>EXPIRED</option>
            <option value="CANCELLED" <?php echo $subStatus === 'CANCELLED' ? 'selected' : '' ?>>CANCELLED</option>
          </select>
        </div>
      </div>
    </div>
    <div class="card-footer">
      <div class="row">
        <div class="col"><a href="<?php echo url('shops') ?>" class="btn btn-flat btn-danger" onclick="return confirm('Leave without saving?')">Cancel</a></div>
        <div class="col text-right"><button type="submit" class="btn btn-flat btn-primary">Save</button></div>
      </div>
    </div>
  </div>
<?php echo form_close(); ?>
</section>

<?= $this->endSection() ?>
<?= $this->section('js') ?>
<script>
  $(document).ready(function() {
    $('.form-validate').validate();
    $('.select2').select2();
  });
</script>
<?= $this->endSection() ?>

