function setImg(input,id) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#'+id).attr('src', e.target.result);
        };
        reader.readAsDataURL(input.files[0]);
    }
}
function initPickUp() {
  	var options = {componentRestrictions: {country: country_flag}};
	var input = document.getElementById('pickup_location');
	var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window, 'load', initPickUp);
function initDestination() {
	var options = {componentRestrictions: {country: country_flag}};
	var input = document.getElementById('drop_location');
	var autocomplete = new google.maps.places.Autocomplete(input, options);
}
google.maps.event.addDomListener(window, 'load', initDestination);

jQuery('[name="offer_type"]').click(function(){
    var offer = jQuery(this).val();
    if(offer == 2){
        jQuery('[id="input_discount"]').html(jQuery('[id="discount_input_html"]').html());
    } else {
        jQuery('[id="input_discount"]').html('');
    }
});

function checkChild(thisObj){
    var table = thisObj.attr('table');
    if(thisObj.prop("checked") == true){
        jQuery('[id^="table_'+table+'_"]').prop("checked", true);
    } else {
        jQuery('[id^="table_'+table+'_"]').prop("checked", false);
    }
}

jQuery('[name="fieldType"]').click(function(){
    var action = jQuery(this).attr('action');
    if(action == 'show'){
        jQuery('[id="customFields"]').removeClass('hide');
    } else {
        jQuery('[id="customFields"]').addClass('hide');
    }
});

report_data = [];
jQuery('[id="rGenerate"]').click(function(){
    event.preventDefault();
    var fields = '',
        action = jQuery(this).attr('action'),
        thisObj = jQuery(this);
        fieldType = jQuery('input[name="fieldType"]:checked').val(),
        where_cond = jQuery('[id="where_cond"]').serialize();

    if(thisObj.attr('dmclick') == 1){
        return false;
    }

    thisObj.attr('dmclick','1');
    jQuery('[name^="TDLS.transport_id"],[name^="TDLS.medical_no"],[name^="TDLS.patient_name"],[name^="TDLS.age"],[name^="TDLS.phone"],[name^="APRES.reason"],[name^="TDLS.appointment_time"]').prop("checked", true);
    if(fieldType == 'custom'){
        jQuery.each(jQuery('[id="field_list"]').serialize().split('&'), function (key,field) {
            var field_arr = field.split('=');
            fields += field_arr[1]+',';
        });
    }else{
        fields = jQuery('[id="all_fields"]').attr('all_fields');
    }
    jQuery.ajax({
        url  : base_url+"Report/rGenerate", 
        type : 'POST',
        data : {'action':action,'fields':fields,'where_cond':where_cond},
        success: function(resp){
            if(resp == '' || resp == undefined || resp == 'undefined' || resp == null || resp == 'null'){
                thisObj.attr('dmclick','0');
                return false;
            }
            var resp_data = jQuery.parseJSON(resp);
            if(resp_data['status'] == '2'){
                thisObj.attr('dmclick','0');
                setErrModal('Report status','No Data Found..!');
                return false;
            }
            if(resp_data['status'] == 0 || (action == 'view' && (resp_data['report_data'] == '' || resp_data['report_data'] == undefined || resp_data['report_data'] == 'undefined' || resp_data['report_data'] == null || resp_data['report_data'] == 'null'))){
                thisObj.attr('dmclick','0');
                setErrModal('Report status','Something Went Wrong, Please Try Again..!');
                return false;
            }
            if(action == 'view'){
                var table = jQuery('[id="report_table"]').DataTable();
                table.clear().draw();

                jQuery.each(resp_data['report_data'], function(key, data) {
                    report_data[key] = data;
                    var vBtn = '<a class="btn btn-sm btn-primary" id="report_view_pop_'+key+'" key="'+key+'"><i class="fa fa-fw fa-edit"></i>View</a>';
                    table.row.add([data['Order_ID'],data['Medical_Number'],data['Patient_Name'],
                                   data['Patient_Age'],data['Patient_Phone'],data['Appointment_Reason'],
                                   data['Appointment_Time'],vBtn]).draw();
                });
                jQuery('[id="report_table_html"]').removeClass('hide');
                slideTo('report_table_html');
                thisObj.attr('dmclick','0');
            }
            if(action == 'export'){
                thisObj.attr('dmclick','0');
                window.location.replace(base_url+'Report/downloadCSV');
            }
        }
    });
});

function setErrModal(header_msg,body_msg){
    jQuery('[id="modal_body_msg"]').html(body_msg);
    jQuery('[id="modal_header_msg"]').html(header_msg);
    jQuery('[id="errModal"]').modal('show');
}

function slideTo(id){
    jQuery('html, body').animate({
        scrollTop: jQuery('[id="'+id+'"]').offset().top
    }, 800);
}

jQuery('#report_table tbody').on('click','a',function() {
    var body_html = rowHtml = '',
        thisObj = jQuery(this),
        key = thisObj.attr('key');

    var colCount = 1;
    body_html = '<div class="col-xs-12">';
    jQuery.each(report_data[key], function(field,value) {
        value = (value == null || value == 'null' || value == undefined || value == 'undefined')?'--':value;
        rowHtml += '<div class="col-xs-6"><div class="col-xs-12">'+
                     '<div class="col-xs-6">'+field.replace('_',' ')+'</div>'+
                     '<div class="col-xs-1">:</div>'+
                     '<div class="col-xs-5">'+value+'</div>'+
                   '</div></div>';
        if(colCount == 2){
            body_html += rowHtml;
            rowHtml = '';
            colCount = 1;
        }else{
            colCount++;
        }
    });
    body_html += '</div>';
    jQuery('[id="report_list_content"]').html(body_html);
    jQuery('[id="report_list_modal"]').modal('show');
});

function getCmpyPayDetls(){
    jQuery('[id="cmpyPayDetlsForm"]').submit();
}