<!-- BEGIN: MAIN -->
<form action="#" mathod="post" name="pricelist_form">
<input type="hidden" name="uid" value="{PL_USER_ID}" />
<table class="table-style pricelist-table">
  <thead>
    <tr>
      <td class="w40p pl20" colspan="2">
        Услуга
      </td>
      <!-- BEGIN: PL_TABLE_HEADER -->
      <td>{PL_ROW_TYPE_TITLE}</td>
      <!-- END: PL_TABLE_HEADER -->
      <td class="w20p pl20">
        Описание
      </td>
    </tr>
  </thead>
  <tbody>
    <!-- BEGIN: PL_CATS_ROW -->
      <tr class="m-clearfix">
        <td colspan="2">
          <img class="pricelist_cat_icon" src="themes/itbirja/images/cat_icons/{CAT_ROW_CODE}.png" height="37px" />
          <span data-editcattitle="{CAT_ROW_CODE}" data-editcurtitle="{CAT_ROW_TITLE}" role="button">
            <input type="hidden" name="pricelist_names[{CAT_ROW_CODE}]" value="{CAT_ROW_TITLE}" />
            {CAT_ROW_TITLE} &nbsp; <i class="fa fa-pencil"></i>
          </span>
        </td>
        <!-- BEGIN: CAT_PL_TYPES -->
          <td data-minmaxtype="{PL_ROW_TYPE_ID}" data-minmaxcat="{CAT_ROW_CODE}">
            <span data-catmin="">{PL_ROW_TYPE_MINMAX.min}</span><span data-catmax=""><!-- IF {PL_ROW_TYPE_MINMAX.max} > 0 AND {{PL_ROW_TYPE_MINMAX.min} != {PL_ROW_TYPE_MINMAX.max} --> - {PL_ROW_TYPE_MINMAX.max}<!-- ENDIF --></span>
          </td>
        <!-- END: CAT_PL_TYPES -->
        <td data-pldesc="{CAT_ROW_CODE}">
          <input type="text" name="pricelist_descs[{CAT_ROW_CODE}]" value="{CAT_ROW_DESC}" maxlen="1020"/>
        </td>
      </tr>
      <!-- BEGIN: SUBCATS_ROW -->
        <tr class="m-clearfix pricelist_subcat" data-catparent="{CAT_ROW_ID}">
          <td class="pl_td_fix"></td>
          <td>
            <img class="pricelist_subcat_icon" src="themes/itbirja/images/cat_icons/{SUBCAT_ROW_CODE}.png" height="15px" />
            <span data-editcattitle="{SUBCAT_ROW_CODE}" data-editcurtitle="{SUBCAT_ROW_TITLE}" role="button">
              <input type="hidden" name="pricelist_names[{SUBCAT_ROW_CODE}]" value="{SUBCAT_ROW_TITLE}" />
              {SUBCAT_ROW_TITLE} &nbsp; <i class="fa fa-pencil"></i>
            </span>
          </td>
          <!-- BEGIN: SUB_PL_TYPES -->
            <td>
              <!-- IF {PL_ROW_TYPE_FILED_MAX} -->
                <div class="pricelist_filds">
                  <input type="text" name="pricelist[{SUBCAT_ROW_CODE}][{PL_ROW_TYPE_ID}][min]" value="{PL_ROW_VALUES_MIN}" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}" />
                  <span>-</span>
                  <input type="text" name="pricelist[{SUBCAT_ROW_CODE}][{PL_ROW_TYPE_ID}][max]" value="{PL_ROW_VALUES_MAX}" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}"<!-- IF !{PL_ROW_VALUES_MIN} --> disabled="disabled"<!-- ENDIF -->/>
                </div>
              <!-- ELSE -->
                <div class="pricelist_filds_one">
                  <input type="text" name="pricelist[{SUBCAT_ROW_CODE}][{PL_ROW_TYPE_ID}][min]" value="{PL_ROW_VALUES_MIN}" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}" />
                </div>
              <!-- ENDIF -->
              <span class="rouble">Р</span>
            </td>
          <!-- END: SUB_PL_TYPES -->
          <td data-pldesc="{SUBCAT_ROW_CODE}">
            <input type="text" name="pricelist_descs[{SUBCAT_ROW_CODE}]" value="{SUBCAT_ROW_DESC}" maxlen="1020"/>
          </td>
        </tr>
      <!-- END: SUBCATS_ROW -->
      <!-- BEGIN: PL_EXTRA_ROW -->
        <tr class="m-clearfix pricelist_subcat" data-catparent="{CAT_ROW_ID}">
          <td class="pl_td_fix"></td>
          <td>
            <input type="text" name="pricelist[{CAT_ROW_CODE}][name][]" value="{PL_ROW_EXT_NAME}" placeholder="Название услуги" style="display: inline;width: calc(100% - 75px);" />
             - <a role="button" onclick="var tmptr = $(this).closest('tr'); tmptr.find('input').val('').change(); tmptr.remove();">удалить</a>
          </td>
          <!-- BEGIN: EXT_PL_TYPES_ADD -->
            <td>
              <!-- IF {PL_ROW_TYPE_FILED_MAX} -->
                <div class="pricelist_filds">
                  <input type="text" name="pricelist[{CAT_ROW_CODE}][{PL_ROW_TYPE_ID}][min][]" value="{PL_ROW_VALUES_MIN}" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}" />
                  <span>-</span>
                  <input type="text" name="pricelist[{CAT_ROW_CODE}][{PL_ROW_TYPE_ID}][max][]" value="{PL_ROW_VALUES_MAX}" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}"<!-- IF !{PL_ROW_VALUES_MIN} --> disabled="disabled"<!-- ENDIF -->/>
                </div>
              <!-- ELSE -->
                <div class="pricelist_filds_one">
                  <input type="text" name="pricelist[{CAT_ROW_CODE}][{PL_ROW_TYPE_ID}][min][]" value="{PL_ROW_VALUES_MIN}" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}" />
                </div>
              <!-- ENDIF -->
              <span class="rouble">Р</span>
            </td>
          <!-- END: EXT_PL_TYPES_ADD -->
          <td data-pldesc="{CAT_ROW_CODE}">
            <input type="text" name="pricelist[{CAT_ROW_CODE}][desc][]" value="{PL_ROW_EXT_DESC}" maxlen="1020"/>
          </td>
        </tr>
      <!-- END: PL_EXTRA_ROW -->
        <tr class="m-clearfix pricelist_subcat" data-catparent="{CAT_ROW_ID}" data-addcustom="{CAT_ROW_CODE}">
          <td class="pl_td_fix"></td>
          <td>
            <a role="button" class="addcustom_pricelist"> + добавить свою услугу</a>
          </td>
          <!-- BEGIN: CAT_PL_TYPES_ADD -->
            <td>
              <!-- IF {PL_ROW_TYPE_FILED_MAX} -->
                <div class="pricelist_filds hidden">
                  <input type="text" name="pricelist[pricelist_template][{PL_ROW_TYPE_ID}][min][]" value="" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}" />
                  <span>-</span>
                  <input type="text" name="pricelist[pricelist_template][{PL_ROW_TYPE_ID}][max][]" value="" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}" disabled="disabled"/>
                </div>
              <!-- ELSE -->
                <div class="pricelist_filds_one hidden">
                  <input type="text" name="pricelist[pricelist_template][{PL_ROW_TYPE_ID}][min][]" value="" data-format="val" data-parcat="{CAT_ROW_CODE}" data-pltype="{PL_ROW_TYPE_ID}" />
                </div>
              <!-- ENDIF -->
              <span class="rouble hidden">Р</span>
            </td>
          <!-- END: CAT_PL_TYPES_ADD -->
          <td data-pldesc="{CAT_ROW_CODE}">
            <input type="text" name="pricelist[pricelist_template][desc][]" value="{PL_ROW_EXT_DESC}" maxlen="1020" class="hidden"/>
          </td>
        </tr>
    <!-- END: PL_CATS_ROW -->
  </tbody>
  <tfoot>
    <tr>
      <td colspan="2">
        <button type="submit" class="btn green-btn">Сохранить</button>
      </td>
      <td colspan="{PL_CNT_TYPES}"></td>
    </tr>
  </tfoot>
</table>
</form>

<script>
  $('form[name="pricelist_form"]').submit(function(e) {
    e.stopPropagation();
    e.preventDefault();
    var thisform = $(this),
        senddata = thisform.serialize();
    if(thisform.find('div.alert-pricelist').length) {
      thisform.find('div.alert-pricelist').remove();
    }
    $.post('/api?method=pricelist', senddata, function(data) {
      if(data) {
        data = JSON.parse(data);
        if(data.success == 1) {
          thisform.prepend('<div class="alert alert-success alert-pricelist mb-0">Цены успешно сохранены</div>');
          location.href='{PL_USER_LINK}';
        } else {
          thisform.prepend('<div class="alert alert-warning alert-pricelist mb-0">Произошла ошибка при сохранении</div>');
        }
      }
    });
  });
  $('tr[data-addcustom] .addcustom_pricelist').click(function(e) {
    e.preventDefault();
    var thistr = $(this).closest('tr'),
        addcustom = thistr.attr('data-addcustom');
    var newtr = thistr.clone();

    newtr.removeAttr('data-addcustom');
    newtr.find('.addcustom_pricelist').replaceWith('<input type="text" name="pricelist[' + addcustom + '][name][]" value="" placeholder="Название услуги" style="display: inline;width: calc(100% - 75px);" /> - <a role="button" onclick="var tmptr = $(this).closest(\'tr\'); tmptr.find(\'input\').val(\'\').change(); tmptr.remove();">удалить</a>');
    newtr.find('[name^="pricelist[pricelist_template]"]').each(function() {
      $(this).attr('name', $(this).attr('name').replace('pricelist_template', addcustom));
    });
    newtr.find('.hidden').removeClass('hidden');
    thistr.before(newtr);
  });

  $('.pricelist-table').on('input', 'input[data-format="val"][name^="pricelist"]', function(e) {
    var thisinp = $(this),
        ch = thisinp.val().replace(/[^\d\-.]/g, ''),
        ch = (ch > 0 ? ch : '');
    $(this).val(ch);
  });
  $('.pricelist-table').on('change', 'input[data-format="val"][name^="pricelist"]', function(e) {
    var thisinp = $(this),
        thisinp_val = thisinp.val() - 0;
    var tdata = thisinp.attr('name').replace('pricelist[', '').replace(']', '').replace(']', '').replace(']', '').split('['),
        parcat = thisinp.attr('data-parcat'),
        minmaxtr = $('td[data-minmaxtype="' + tdata[1] + '"][data-minmaxcat="' + parcat + '"]');

    if(tdata[2] == 'min') {
      var maxfld = thisinp.parent().find('input[name^="pricelist[' + tdata[0] + '][' + tdata[1] + '][max]"]'),
          maxfld_val = maxfld.val() - 0;
      if(maxfld.length > 0) {
        if(thisinp_val > 0) {
          maxfld.removeAttr('disabled');
          if(thisinp_val >= maxfld_val) {
            maxfld.val('');
          }
        } else {
          maxfld.val('').attr('disabled', 'disabled');
        }
      }
    } else if(tdata[2] == 'max') {
      var minfld = thisinp.parent().find('input[name^="pricelist[' + tdata[0] + '][' + tdata[1] + '][min]"]'),
          minfld_val = minfld.val() - 0;
      if(minfld.length > 0) {
        if(minfld.val() - 0 >= thisinp_val) {
          thisinp.val('');
        }
      }
    }

    if(minmaxtr.length > 0) {
      var tmin = 0, tmax = 0;
      $('input[name^="pricelist"][data-parcat="' + parcat + '"][data-pltype="' + tdata[1] + '"]').each(function() {
        var chtmp = $(this).val().replace(/[^\d\-.]/g, '') - 0,
            chtmp = (chtmp > 0 ? chtmp : 0);
        if(chtmp > 0) {
          if(chtmp < tmin || tmin == 0) tmin = chtmp;
          if(chtmp > tmax) tmax = chtmp;
        }
      });
      if(tmin > 0) {
        minmaxtr.find('[data-catmin]').html(tmin);
        minmaxtr.find('[data-catmax]').html(((tmax > 0 && tmax > tmin) ? ' - ' + tmax : ''));
      } else {
        minmaxtr.find('[data-catmin], [data-catmax]').html('');
      }
    }
  });

  $('.pricelist-table').on('click', 'span[data-editcattitle][data-editcurtitle]._editable .fa.fa-check', function(e) {
    var thispar = $(this).parent(),
        thisinp = thispar.find('input'),
        thisval = thisinp.val().trim();
    thispar.attr('data-editcurtitle', thisval);
    thispar.removeClass('_editable').html('<input type="hidden" name="pricelist_names[' + thispar.attr('data-editcattitle') + ']" value="' + thisval + '" />' + thisval + ' &nbsp; <i class="fa fa-pencil"></i>');
  });
  $('.pricelist-table').on('click', 'span[data-editcattitle][data-editcurtitle]:not(._editable)', function(e) {
    $(this).addClass('_editable').html('<input type="text" name="pricelist_names[' + $(this).attr('data-editcattitle') + ']" value="' + $(this).attr('data-editcurtitle') + '" /> &nbsp; <i class="fa fa-check"></i>');
  });
</script>
<!-- END: MAIN -->