$(document).ready(function(){
    $('#search_value_stock_id, #search_value_sh, #search_value_wt, #search_value_col, #search_value_cla, #search_value_cut, #search_value_pol, #search_value_sym, #search_value_fl, #search_value_lab').multiselect({
      nonSelectedText: 'Select',
      enableFiltering: true,
      enableCaseInsensitiveFiltering: true,
      buttonWidth:'100px',
      maxHeight:'450',
      includeSelectAllOption: true      
     });

    $("#search_value_entry_date").datepicker({
        format: 'dd-mm-yyyy',
        todayHighlight: true,
    });
    disable_enable_background(false)
});


function get_challan_return_data(dm_id, crt_id)
{
    
    var path = base_url+"master/masterctrl/get_challan_return_data?dm_id="+dm_id+"&crt_id="+crt_id;
    ajaxCall('POST',path,'','JSON',resp => 
    {
        console.log(resp);
        
        var data = '';                      
        data += '<div class="row">';
        data += '<div class="col-md-12">'
        data += '<table class="table table-responsive">';
        data += '<tr>';
        
        data += '<th> ISSUE NO </th>';
        data += '<th> ISSUE DATE </th>';
        data += '<th> PARTY</th>';
        data += '<th> BROKER</th>';
        data += '<th> THROUGH</th>';

        data += '</tr>';

        data += '<tr>';
        
        data += '<td> '+resp[0]['crm_entry_no']+'</td>';
        data += '<td> '+resp[0]['crm_entry_date']+'</td>';
        data += '<td> '+resp[0]['customer']+'</td>';           
        data += '<td> '+resp[0]['broker']+'</td>';           
        data += '<td> '+resp[0]['crm_through']+'</td>';           

        data += '</tr>';
        data += '</table>';
        data += '</div>';
        data += '</div>';

        $(".modal-body-sm").html(data);

        var sbt_btn = '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';  
        
        $(".modal-title-sm").html("ISSUE DETAIL");
        

        $(".modal-footer-sm").html(sbt_btn);

        $("#popup_modal_sm").modal('show'); 

        
    },
    errmsg => {});
}

function get_challan_data(dm_id, ct_id)
{
    
    var path = base_url+"master/masterctrl/get_challan_data?dm_id="+dm_id+"&ct_id="+ct_id;
    ajaxCall('POST',path,'','JSON',resp => 
    {
        console.log(resp);

        let broker = resp[0]['broker'] == null ? '-' : resp[0]['broker'];
        
        var data = '';                      
        data += '<div class="row">';
        data += '<div class="col-md-12">'
        data += '<table class="table table-responsive">';
        data += '<tr>';
        
        data += '<th> ISSUE NO </th>';
        data += '<th> ISSUE DATE </th>';
        data += '<th> PARTY</th>';
        data += '<th> BROKER</th>';
        data += '<th> THROUGH</th>';

        data += '</tr>';

        data += '<tr>';
        
        data += '<td> '+resp[0]['cm_entry_no']+'</td>';
        data += '<td> '+resp[0]['cm_entry_date']+'</td>';
        data += '<td> '+resp[0]['customer']+'</td>';           
        data += '<td> '+broker +'</td>';           
        data += '<td> '+resp[0]['cm_through']+'</td>';           

        data += '</tr>';
        data += '</table>';
        data += '</div>';
        data += '</div>';

        $(".modal-body-sm").html(data);

        var sbt_btn = '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';  
        
        $(".modal-title-sm").html("ISSUE DETAIL");
        

        $(".modal-footer-sm").html(sbt_btn);

        
        $("#popup_modal_sm").modal('show'); 
    },
    errmsg => {}
    );
}

function receive_certificate(crt_id)
{
    if(confirm('Are you sure you want to proceed?'))
    {
        
        var path = base_url+"challan/challanctrl/receive_certificate/"+crt_id;
        ajaxCall('POST',path,'','JSON',resp => 
        {
            var msg = 'Successfully added';
            display_alert('succ',msg);
            $("body, html").animate({'scrollTop':0},1000);
            setInterval(function(){window.location.reload(); },2000); 
            
        },
        errmsg => {});
        
    }
    else
    {
        $("#cert_"+crt_id).prop("checked", false);
    }

    
}