
<!----------------------------------------------------------------------------------------------------------------------------------------------------->
{if $type == "drugedit"}

<tr style="display: none"><td colspan="5" ><input type="hidden" id="drugItr" value="{$preItr}" name="drugItr"/></td></tr>


{assign var=itr value=1}
{foreach $PatientPresByDate as $pre}
<tr>
    <td>
        <select class="form-control xx dcat " id="dcat{$itr}" name="dcat{$itr}"  data-itr="{$itr}"  data-placeholder="Select..."  >
            <option value="" {if $pre->drugcat == ""}selected{/if}>Choose Category</option>
            {foreach $allDrugcats as $cat}
            <option value="{$cat['drugcat']}" {if $pre->drugcat == $cat['drugcat'] }selected{/if}>{$cat['drugcat']}</option>
            {/foreach}
            <option value="" {if $pre->drugcat == ""}selected{/if}>Choose Category</option>
        </select>
    </td>
    <td>
        <select class="form-control xx dname  updateElement dnameSelect" id="dname{$itr}" data-id="{$pre->id}" data-colName="drugid" data-tableName="gynadrugs" data-itr="{$itr}" data-placeholder="Select..."  >
            <option value="" {if $pre->drugid == ""}selected{/if}>Choose Drug</option>
            {foreach $drugs as $drug}
            <option value="{$drug['id']}" {if $pre->drugid == $drug['id'] }selected{/if}>{$drug['drugname']} {if $ProgramesettingData->visitsform eq 3}(left {$drug['currentbalance']}){/if}</option>
            {/foreach}
            <option class="deleteoptincompo" value="" {if $pre->drugid == ""}selected{/if}>Choose Drug</option>
        </select>

    </td>
    <td>
        <select class="form-control xx dtype  updateElement dtypeSelect" id="dtype{$itr}" data-id="{$pre->id}" data-colName="drugtype" data-tableName="gynadrugs" data-itr="{$itr}" data-placeholder="Select..."  >
            <option value="" {if $pre->drugtype == ""}selected{/if}>Choose Form</option>
            {foreach $allDrugTypes as $type}
            <option value="{$type['drugtype']}" {if $pre->drugtype == $type['drugtype'] }selected{/if}>{$type['drugtype']}</option>
            {/foreach}
            <option class="deleteoptincompo" value="" {if $pre->drugtype == ""}selected{/if}>Choose Form</option>
        </select>
    </td>
    <td>
        <select class="form-control xx  updateElement ddosSelect" id="ddos{$itr}" data-id="{$pre->id}" data-colName="drugdos" data-tableName="gynadrugs" data-itr="{$itr}" data-placeholder="Select..."  >
            <option value="" {if $pre->drugdos == ""}selected{/if}>choose dose </option>
            {foreach $allDrugDoses as $dos}
            <option value="{$dos['drugdos']}" {if $pre->drugdos == $dos['drugdos'] }selected{/if}>{$dos['drugdos']}</option>
            {/foreach}
            <option value="" class="deleteoptincompo" {if $pre->drugdos == ""}selected{/if}>choose dose </option>
        </select>
    </td>

    <td>
        <a class="btn grey-cascade btn-xs deletemsgEX" data-id="{$pre->id}" style=" float: left;" href="#" data-link="gyna" data-fn="addprescription">
            <i class="fa fa-close"></i>
        </a>
    </td>
</tr>
{capture assign=itr}{$itr+1}{/capture}
{/foreach}

<!----------------------------------------------------------------------------------------------------------------------------------------------------->
{/if}





{literal}
<script type="text/javascript" >
    var preItr = $('#drugItr').val();
    var hosturl = $('#hosturl').val();

    for (var itr = 1; itr <= preItr; itr++)
    {
        if ($().select2)
        {
            $('#dcat' + itr).select2({placeholder: "Select", allowClear: true});
            // $('#dname' + itr).select2({placeholder: "Select", allowClear: true});
            // $('#dtype' + itr).select2({placeholder: "Select", allowClear: true});
            //     $('#ddos' + itr).select2({placeholder: "Select", allowClear: true});


            $('#dname' + itr).select2({
//   width: '240px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    var thisId = $(this).attr("id");
                    // var itr = $(this).attr("data-itr");
                    return "<input class='form-control' id='newdnameSelectTerm' value='" + term + "'><a href='#' id='addNewdnameSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '#addNewdnameSelect', function () {
                /* add the new term */
                var dnameSelect = $('#newdnameSelectTerm').val();
                var thisId = $(this).attr("data-id");
                $.post(hosturl + "/controllers/patienthistory.php?ac=addRow", {value: dnameSelect, table: 'drugname'}, function (data) {

                    $('.dnameSelect > .deleteoptincompo').before('<option value="' + Number(data) + '" >' + dnameSelect + '</option>');
                    $('#' + thisId + '').select2('val', Number(data)).trigger("change"); // select the new term
                    $('.dnameSelect').select2('close'); // close the dropdown
                });
            });

            $('#dtype' + itr).select2({
//   width: '240px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    var thisId = $(this).attr("id");
                    // var itr = $(this).attr("data-itr");
                    return "<input class='form-control' id='newdtypeSelectTerm' value='" + term + "'><a href='#' id='addNewdtypeSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '#addNewdtypeSelect', function () {
                /* add the new term */
                var dtypeSelect = $('#newdtypeSelectTerm').val();
                var thisId = $(this).attr("data-id");
                $.post(hosturl + "/controllers/patienthistory.php?ac=addRow", {value: dtypeSelect, table: 'drugtype'}, function (data) {

                    $('.dtypeSelect > .deleteoptincompo').before('<option value="' + dtypeSelect + '" >' + dtypeSelect + '</option>');
                    $('#' + thisId + '').select2('val', dtypeSelect).trigger("change"); // select the new term
                    $('.dtypeSelect').select2('close'); // close the dropdown
                });
            });

            $('#ddos' + itr).select2({
//   width: '240px',
                allowClear: true,
                formatNoMatches: function (term) {
                    /* customize the no matches output */
                    var thisId = $(this).attr("id");
                    // var itr = $(this).attr("data-itr");
                    return "<input class='form-control' id='newddosSelectTerm' value='" + term + "'><a href='#' id='addNewddosSelect' data-id='" + thisId + "' class='btn blue custombtnstyle'>ADD</a>"
                }
            }).parent().find('.select2-with-searchbox').on('click', '#addNewddosSelect', function () {
                /* add the new term */
                var ddosSelect = $('#newddosSelectTerm').val();
                var thisId = $(this).attr("data-id");
                $.post(hosturl + "/controllers/patienthistory.php?ac=addRow", {value: ddosSelect, table: 'drugdos'}, function (data) {

                    $('.ddosSelect > .deleteoptincompo').before('<option value="' + ddosSelect + '" >' + ddosSelect + '</option>');
                    $('#' + thisId + '').select2('val', ddosSelect).trigger("change"); // select the new term
                    $('.ddosSelect').select2('close'); // close the dropdown
                });
            });
        }

    }
</script>
{/literal}
