<script type="text/javascript" src="/libraries/jquery/sortable/sortable.js"></script>
<?
global $db;
ob_start();
?>
<div id="settings-tabs">
  <ul class="resp-tabs-list settings-tabs">
    <li><i class="fa fa-wrench"></i>Ustawienia</li>
  </ul>
  <div class="resp-tabs-container settings-tabs">
    <div>
      <?=
      \Html::input( [
          'label' => 'Nazwa',
          'name'  => 'name',
          'id'    => 'name',
          'value' => $this -> promotion['name']
      ] );
      ?>
      <?=
      \Html::input_switch( [
          'label'   => 'Aktywna',
          'name'    => 'status',
          'checked' => $this -> promotion['status'] == 1 ? true : false
      ] );
      ?>
      <?=
      \Html::input_switch( [
          'label'   => 'Łącz z kuponami rabatowymi',
          'name'    => 'include_coupon',
          'checked' => $this -> promotion['include_coupon'] == 1 ? true : false
      ] );
      ?>
      <?=
      \Html::input_switch( [
          'label'   => 'Uwzględnij produkty przecenione',
          'name'    => 'include_product_promo',
          'checked' => $this -> promotion['include_product_promo'] == 1 ? true : false
      ] );
      ?>
      <?=
        \Html::select( [
            'label'  => 'Warunki promocji',
            'id' => 'condition_type',
            'name'   => 'condition_type',
            'values' => \shop\Promotion::$condition_type,
            'value'  => $this -> promotion['condition_type'],
        ] );
      ?>
      <?=
        \Html::select( [
            'label'  => 'Typ rabatu',
            'id' => 'discount_type',
            'name'   => 'discount_type',
            'values' => \shop\Promotion::$discount_type,
            'value'  => $this -> promotion['discount_type'],
        ] );
      ?>
      <?= \Html::input( [
            'label' => 'Min. ilość produktów z danej kategorii',
            'class' => 'int-format',
            'name'  => 'min_product_count',
            'id' => 'min_product_count',
            'value' => $this -> promotion['min_product_count']
      ] );?>
      <?= \Html::input( [
          'label' => 'Cena najtańszego produktu',
          'class' => 'number-format',
          'name' => 'price_cheapest_product',
          'id' => 'price_cheapest_product',
          'value' => $this -> promotion['price_cheapest_product']
      ] );
      ?>
      <?=
      \Html::input( [
          'label' => 'Wartość',
          'class' => 'number-format',
          'name'  => 'amount',
          'id'    => 'amount',
          'value' => $this -> promotion['amount']
      ] );
      ?>
      <?=
      \Html::input( [
          'label' => 'Data do',
          'class' => 'date',
          'name'  => 'date_to',
          'id'    => 'date_to',
          'value' => $this -> promotion['date_to']
      ] );
      ?>
      <div class="form-group row">
        <label class="col-lg-4 control-label">Kategorie grupa I:</label>
        <div class="col-lg-8">
          <div class="menu_sortable">
            <ol class="sortable" id="sortable">
              <?
              if ( is_array( $this -> categories ) )
              {
                foreach ( $this -> categories as $category )
                {
                  ?>
                  <li id="list_<?= $category[ 'id' ];?>" class="category_<?= $category[ 'id' ];?>" category="<?= $category[ 'id' ];?>">
                    <div class="context_0 content content_menu">
                      <span class="disclose"><span></span></span>
                      <? if ( !$category[ 'status' ] ) echo '<i class="fa fa-ban fa-lg text-danger" title="Kategoria nieaktywna"></i>';?>
                      <input type="checkbox" class="g-checkbox" name="categories[]" value="<?= $category[ 'id' ];?>" <? if ( is_array( json_decode( $this -> promotion['categories'] ) ) and in_array( $category[ 'id' ], json_decode( $this -> promotion['categories'] ) ) ):?>checked="checked"<? endif;?> />
                      <b><?= $category[ 'languages' ][ $this -> dlang ][ 'title' ];?></b>
                    </div>
                    <?=
                    \Tpl::view( 'shop-product/subcategories-list', [
                        'categories' => \admin\factory\ShopCategory::subcategories( $category[ 'id' ] ),
                        'product_categories' => json_decode( $this -> promotion['categories'] ),
                        'dlang' => $this -> dlang
                    ] );
                    ?>
                  </li>
                  <?
                }
              }
              ?>
            </ol>
          </div>
        </div>
        <div class="clear"></div>
      </div>
      <div class="form-group row">
        <label class="col-lg-4 control-label">Kategoria grupa II:</label>
        <div class="col-lg-8">
          <div class="menu_sortable">
            <ol class="sortable" id="sortable2">
              <?
              if ( is_array( $this -> categories ) )
              {
                foreach ( $this -> categories as $category )
                {
                  ?>
                  <li id="list_<?= $category[ 'id' ];?>" class="category_<?= $category[ 'id' ];?>" category="<?= $category[ 'id' ];?>">
                    <div class="context_0 content content_menu">
                      <span class="disclose"><span></span></span>
                      <? if ( !$category[ 'status' ] ) echo '<i class="fa fa-ban fa-lg text-danger" title="Kategoria nieaktywna"></i>';?>
                      <input type="checkbox" class="g-checkbox" name="condition_categories[]" value="<?= $category[ 'id' ];?>" <? if ( is_array( json_decode( $this -> promotion['condition_categories'] ) ) and in_array( $category[ 'id' ], json_decode( $this -> promotion['condition_categories'] ) ) ):?>checked="checked"<? endif;?> />
                      <b><?= $category[ 'languages' ][ $this -> dlang ][ 'title' ];?></b>
                    </div>
                    <?=
                    \Tpl::view( 'shop-product/subcategories-list', [
                        'categories' => \admin\factory\ShopCategory::subcategories( $category[ 'id' ] ),
                        'product_categories' => json_decode( $this -> promotion['condition_categories'] ),
                        'dlang' => $this -> dlang,
                        'name' => 'condition_categories[]'
                    ] );
                    ?>
                  </li>
                  <?
                }
              }
              ?>
            </ol>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<?
$out = ob_get_clean();

$grid = new \gridEdit;
$grid -> id = 'promotion-edit';
$grid -> gdb_opt = $gdb;
$grid -> include_plugins = true;
$grid -> title = $this -> promotion['id'] ? 'Edycja promocji: <u>' . $this -> promotion['name'] . '</u>' : 'Nowa promocja';
$grid -> fields = [
    [
        'db'    => 'id',
        'type'  => 'hidden',
        'value' => $this -> promotion['id']
    ]
];
$grid -> actions = [
    'save'   => [ 'url' => '/admin/shop_promotion/save/', 'back_url' => '/admin/shop_promotion/view_list/' ],
    'cancel' => [ 'url' => '/admin/shop_promotion/view_list/' ]
];
$grid -> external_code = $out;
$grid -> persist_edit = true;
$grid -> id_param = 'id';

echo $grid -> draw();
?>

<link type="text/css" rel="stylesheet" href="/libraries/selectize/css/selectize.css">
<link type="text/css" rel="stylesheet" href="/libraries/selectize/css/selectize.default.css">
<script type="text/javascript" src="/libraries/plupload/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript" src="/libraries/jquery-nested-sortable/jquery.mjs.nestedSortable.js"></script>
<script type="text/javascript" src="/libraries/selectize/js/standalone/selectize.js"></script>
<script type="text/javascript">
  function condition_type_change()
  {
    if ( $( '#condition_type' ).val() == 1 || $( '#condition_type' ).val() == 2 )
    {
      $( '#discount_type' ).closest( '.row' ).show();
      $( '#amount' ).closest( '.row' ).show();
      $( '#sortable2' ).closest( '.row' ).show();
      $( '#min_product_count' ).closest( '.row' ).hide();
      $( '#price_cheapest_product' ).closest( '.row' ).hide();
    }

    if ( $( '#condition_type' ).val() == 3 )
    {
      $( '#discount_type' ).closest( '.row' ).hide();
      $( '#amount' ).closest( '.row' ).hide();
      $( '#sortable2' ).closest( '.row' ).hide();
      $( '#min_product_count' ).closest( '.row' ).show();
      $( '#price_cheapest_product' ).closest( '.row' ).show();
    }
  }

  $(function ()
  {
    condition_type_change();

    $( 'body' ).on( 'change', '#condition_type', function()
    {
      condition_type_change();
    });

    disable_menu();
    $('#settings-tabs').easyResponsiveTabs({
      width: 'auto',
      fit: true,
      tabidentify: 'settings-tabs',
      type: 'vertical'
    });

    $('ol.sortable').nestedSortable({
      forcePlaceholderSize: true,
      handle: 'div',
      helper: 'clone',
      items: 'li',
      opacity: .9,
      placeholder: 'placeholder',
      revert: 250,
      tabSize: 45,
      tolerance: 'pointer',
      toleranceElement: '> div',
      maxLevels: 2,
      isTree: true,
      expandOnHover: 700,
      protectRoot: false
    });

    $('.disclose').on('click', function ()
    {
      $(this).closest('li').toggleClass('sort-collapsed').toggleClass('sort-expanded');
    });
  });
</script>