}
        }
    }
}
$i = 1;
?>
        <div id="wizard" class="tab-content-bdr-0">
            <?php 
if ((int) $show_payment_tab > 0) {
    echo "<script>var show_payment_tab=1;</script>";
} else {
    echo "<script>var show_payment_tab=0;</script>";
}
if (!empty($myvar)) {
    foreach ($myvar as $key => $value) {
        if (is_user_company_billing() && !is_user_company_admin()) {
            if (strtolower($key) == 'payments') {
                if (strtolower($key) == 'payments') {
                    if ((int) $show_payment_tab > 0) {
                        ?>
                                    <h2><?php 
                        echo $key;
                        ?>
</h2>
                                    <section>
                                    <?php 
                        echo $myvar[$key];
                        ?>
                                    </section>
                                    <?php 
                    } else {
function crm_application_callback()
{
    global $current_user, $wpdb;
    isset($_POST['length']) ? $lenght = $_POST['length'] : ($lenght = 100);
    isset($_POST['start']) ? $start = $_POST['start'] : ($start = 0);
    $where = array();
    if (isset($_POST['search']['value']) and !empty($_POST['search']['value'])) {
        $search = $_POST['search']['value'];
        $where[] = " application_name like '%{$search}%' ";
    }
    if (isset($_POST['columns'][1]['search']['value']) and !empty($_POST['columns'][1]['search']['value']) && $_POST['columns'][1]['search']['value'] != 'all') {
        $certificate_status = $_POST['columns'][1]['search']['value'];
        /* if($certificate_status == 'new_application'){
           $where[] = " (status like '%new_application%' OR status like '%modified%') ";
           } else */
        if ($certificate_status == 'approved') {
            $where[] = " (status like '%approved%' OR status like '%in_review%') ";
        } else {
            $where[] = " status like '%{$certificate_status}%' ";
        }
    } else {
        if ($_POST['columns'][1]['search']['value'] == '' && isset($_POST['type']) && $_POST['type'] != 'all') {
            $certificate_status = $_POST['type'];
            if ($certificate_status == 'approved') {
                $where[] = " (status like '%approved%' OR status like '%in_review%') ";
            } else {
                $where[] = " status like '%{$certificate_status}%' ";
            }
        }
    }
    if (isset($_POST['order'][0]['column']) and !empty($_POST['order'][0]['column'])) {
        switch ($_POST['order'][0]['column']) {
            case 0:
                $order = $wpdb->prefix . "application_data.id";
                break;
            case 1:
                $order = $wpdb->prefix . "application_data.application_name";
                break;
            case 2:
                $order = $wpdb->prefix . "application_data.certificate_name";
                break;
                //             case 3:
                //                 $order =  $wpdb->prefix . "application_data.id";
                //                 break;
                //             case 4:
                //                 $order =  $wpdb->prefix . "application_data.id";
                //                 break;
                //             case 5:
                //                 $order =  $wpdb->prefix . "application_data.id";
                break;
            case 6:
                $order = $wpdb->prefix . "application_data.status";
                break;
            default:
                $order = $wpdb->prefix . "application_data.id";
                break;
        }
    } else {
        $order = $wpdb->prefix . "application_data.id";
    }
    if (isset($_POST['order'][0]['dir']) and !empty($_POST['order'][0]['dir'])) {
        $by = $_POST['order'][0]['dir'];
    } else {
        $by = 'DESC';
    }
    if (isset($_POST['columns'][4]['search']['value']) and !empty($_POST['columns'][4]['search']['value']) && $_POST['columns'][4]['search']['value'] != 'all') {
        $search_program = $_POST['columns'][4]['search']['value'];
        $program_id = $wpdb->get_var("SELECT id FROM " . $wpdb->prefix . "programs where name='" . $search_program . "'");
        if ($search_program != '') {
            $where[] = " (wp_application_data.program_id =" . $program_id . ") ";
            if (strtolower($_POST['role']) == 'staff') {
                $where[] = '(status != "Draft")';
            }
        }
    } else {
        if ($_POST['columns'][4]['search']['value'] == '' && isset($_POST['program_name']) && $_POST['program_name'] != 'all') {
            $search_program = $_POST['program_name'];
            $program_id = $wpdb->get_var("SELECT id FROM " . $wpdb->prefix . "programs where name='" . $search_program . "'");
            if ($search_program != '') {
                $where[] = " (wp_application_data.program_id =" . $program_id . ") ";
                if (strtolower($_POST['role']) == 'staff') {
                    $where[] = '(status != "Draft")';
                }
            }
        }
    }
    if (!empty($where)) {
        $where = ' WHERE ' . implode('AND', $where);
    } else {
        if (strtolower($_POST['role']) == 'customer') {
            $where = 'WHERE status !="delete"';
        } else {
            $where = 'WHERE status != "Draft"';
        }
    }
    $role = strtolower($_POST['role']);
    $roles_join = '';
    $customer__join = '';
    $k = 1;
    $or_condition = '';
    if ($role == 'contact') {
        $roles_sql = "SELECT * FROM " . $wpdb->prefix . "application_user_roles where user_id=" . $current_user->ID . " group by roles";
        $result = $wpdb->get_results($roles_sql);
        $roles_join .= "LEFT JOIN " . $wpdb->prefix . "application_user_roles as roles on (roles.application_id = " . $wpdb->prefix . "application_data.id AND roles.user_id =" . $current_user->ID . ") ";
        $or_condition = 'OR roles.user_id =' . $current_user->ID . " ) ";
        // foreach ($result as $key => $value) {
        // 	$roles_array = unserialize($value->roles);
        // 	$or_condition='';
        // 	foreach ($roles_array[0] as $key => $value) {
        // 		if(count($result)>1)
        // 		{
        // 			if($k!=count($result))
        // 			{
        // 				$where .= ' AND roles.roles like "%'.$key.'%" OR ';
        // 			}else
        // 			{
        // 				$where .= ' roles.roles like "%'.$key.'%"';
        // 			}
        // 		}else
        // 		{
        // 			$where .= ' AND roles.roles like "%'.$key.'%"';
        // 		}
        // 		$k++;
        // 	}
        // }
        if (isset($or_condition)) {
            $where .= ' AND (' . $wpdb->prefix . 'application_data.company_id=' . $current_user->company_id;
        } else {
            $where .= ' AND ' . $wpdb->prefix . 'application_data.company_id=' . $current_user->company_id;
        }
    } else {
        if ($role != 'staff' && $role != 'assessor') {
            //$customer__join = ' Inner Join ' . $wpdb->prefix . 'users as u on u.company_id =  `' . $wpdb->prefix . 'application_data`.company_id and u.ID =' . $current_user->ID;
            //$customer__join = 'AND wp_application_data.user_id='.$current_user->ID;
            $where .= ' AND ' . $wpdb->prefix . 'application_data.company_id=' . $current_user->company_id;
            //$where .='AND wp_application_data.user_id='.$current_user->ID;
        }
    }
    // Filter application data if staff login
    $program__join = '';
    if ($role == 'staff') {
        $program__join = ' Inner join ' . $wpdb->prefix . 'program_user_association as pua on `' . $wpdb->prefix . 'application_data`.program_id = pua.program_id ';
        $where .= 'AND pua.user_id =' . $current_user->ID;
        //$where .= " GROUP BY pua.program_id";
    }
    $renewal_join = ' LEFT JOIN ' . $wpdb->prefix . 'application_renewal_notification as arn on (arn.application_id = ' . $wpdb->prefix . 'application_data.id)';
    $renewal_fields = ", arn.staff_last_notified_on, arn.customer_last_notified_on, arn.is_renewed ";
    //echo "SELECT " . $wpdb->prefix . "application_data.* " . $renewal_fields . " FROM " . $wpdb->prefix . "application_data " . $program__join . " " . $customer__join . " " . $renewal_join . " " . $where . " ORDER BY wp_application_data.id DESC LIMIT $start,$lenght ";die;
    $results = $wpdb->get_results("SELECT " . $wpdb->prefix . "application_data.* " . $renewal_fields . " FROM " . $wpdb->prefix . "application_data " . $program__join . " " . $customer__join . " " . $renewal_join . " " . " " . $roles_join . " " . $where . " " . $or_condition . " GROUP BY " . $wpdb->prefix . "application_data.id ORDER BY " . $order . " " . $by . " LIMIT {$start},{$lenght} ");
    //echo $wpdb->last_query;die;
    //echo "SELECT " . $wpdb->prefix . "application_data.* " . $renewal_fields . " FROM " . $wpdb->prefix . "application_data " . $program__join . " " . $customer__join . " " . $renewal_join . " " . $where . " ORDER BY wp_application_data.id DESC LIMIT $start,$lenght ";die;
    $return = array();
    $return["recordsTotal"] = $return["recordsFiltered"] = $wpdb->get_var("SELECT count(wp_application_data.id) FROM (SELECT count(wp_application_data.id) id FROM `" . $wpdb->prefix . "application_data` " . $program__join . " " . $customer__join . " " . $roles_join . " " . $where . " " . $or_condition . " GROUP BY " . $wpdb->prefix . "application_data.id) as wp_application_data");
    //count($results);
    $return["data"] = array();
    $status = array('new_application' => 'New', 'modified' => 'Modified', 'approved' => 'In Reveiw', 'send_to_customer' => 'Send To Customer', 'missing_item' => 'Missing Item', 'cancelled' => 'Cancelled', 'completed' => 'Completed', 'draft' => 'Draft');
    foreach ($results as $result) {
        $appData = json_decode($result->application_data, true);
        $renewal_data = json_decode($result->renewal_options);
        $company_name = '';
        //$country = get_name('country', $appData['new_application']['_linked']['new_country']['new_countryid'], 'country');
        //$state = get_name('state', $appData['new_application']['_linked']['new_state']['new_stateid'], 'state');
        if (isset($result->company_id)) {
            $company_name = get_name('company', $result->company_id, 'name');
            $country = get_name('company', $result->company_id, 'country');
            $country = get_name('country', $country, 'country');
            $state = get_name('company', $result->company_id, 'state');
            $state = get_name('state', $state, 'state');
            $city = get_name('company', $result->company_id, 'city');
        }
        $program_name = $wpdb->get_var("SELECT name FROM " . $wpdb->prefix . "programs where id=" . $result->program_id);
        $exp = $result->application_exp_date != '' ? date('d/m/Y', strtotime($result->application_exp_date)) : '';
        $opr = '<ul>';
        $third_party_array = array(2, 7, 10);
        $result->status = strtolower($result->status);
        $json = json_decode($result->application_data);
        if (strtolower($_POST['role']) == 'staff') {
            /* $opr .= '<a href="' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($result->id) . '&view=false"  title="View Application"></a>&nbsp;&nbsp;';
               $opr .= '<a href="' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($result->id) . '&view=true" title="View Application"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-eye-open"></span></a>&nbsp;&nbsp;'; */
            if (strtolower($result->status) == 'new' || strtolower($result->status) == 'modified' || strtolower($result->status) == 'draft') {
                $request_url = get_admin_url() . 'admin-post.php?action=approve-application-form&id=' . base64_encode($result->id) . '&is_ajax=true';
                $redirect_url = site_url() . '/index.php/listings?page=application-form-register&view=approved';
                if ($result->status == 'modified') {
                    $opr .= '<li><a  title="View Difference From Last Update" href="#app_diff" class="colorbox-inline" onclick="getAppDiff(\'' . $result->id . '\',\'app\', \'' . $company_name . '\', \'' . get_admin_url() . 'admin-post.php?action=approve-application-form&id=' . base64_encode($result->id) . '\', \'' . $result->certificate_name . '\', \'' . $redirect_url . '\' );getAppDocDiff(\'' . $result->id . '\', \'doc\', \'' . $company_name . '\')" ><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-random"></span></li></a>';
                }
                if (!empty($result->certificate_name) && !in_array($result->program_id, $third_party_array)) {
                    $opr .= '<li><a href="#" onclick="application_push_to_crm(\'' . $request_url . '\', \'' . $redirect_url . '\')" class="approveLink" value=""  title="Push To CRM"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-okglyphicon glyphicon-ok"></span></li></a>';
                } else {
                    $certificate_name = isset($result->certificate_name) && $result->certificate_name != '' ? $result->certificate_name : '';
                    $new_inspectionagencycert = isset($json->new_application->_linked->new_certificate->new_inspectionagencycert) ? $json->new_application->_linked->new_certificate->new_inspectionagencycert : '';
                    $opr .= '<li><a href="#app_certification_popup" id="approve-link-' . $result->id . '" onclick="return openCertificatePopUp(\'' . $result->id . '\',\'' . $result->program_id . '\',\'' . $new_inspectionagencycert . '\',\'' . $certificate_name . '\')" data-href="' . $request_url . '" redirect-href="' . $redirect_url . '" class="colorbox-inline" value=""  title="Push To CRM"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-ok"></span></li></a>';
                }
            }
            if (empty($result->certificate_url)) {
                if (strtolower($result->status) != 'completed') {
                    $opr .= '<li><a  class="icon-3" href="' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($result->id) . '&view=true" title="View Application"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-eye-open"></span></li></a>';
                }
                if (strtolower($result->status) == 'new' || strtolower($result->status) == 'modified' || strtolower($result->status) == 'draft') {
                    $opr .= '<li><a  title="Edit Application" class="icon-3" href="' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($result->id) . '&view=false"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-edit"></span></li></a>';
                }
            }
        } else {
            if ((user_can($current_user, "customer") || is_user_company_admin() || is_user_company_contact()) && empty($result->certificate_url)) {
                if (strtolower($result->status) != 'completed') {
                    $opr .= '<li><a  title="View Application" class="icon-3" href="' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($result->id) . '&view=true"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-eye-open"></span></li></a>';
                }
                if ($result->new_application_id == 0 && strtolower($result->status) != 'completed') {
                    $opr .= '<li><a  title="Edit Application" class="icon-3" href="' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($result->id) . '"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-edit"></span></li></a>';
                }
            } else {
                if (user_can($current_user, "assessor") && empty($result->certificate_url) && strtolower($result->status) != 'completed') {
                    $opr .= '<li><a  title="View Application" class="icon-3" href="' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($result->id) . '&view=true"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-eye-open"></span></li></a>';
                }
            }
        }
        if (is_user_company_admin() && empty($result->crm_id) && strtolower($result->status) != 'completed') {
            $opr .= '<li><a title="Delete Application" class="icon-3" href="' . get_admin_url() . 'admin-post.php?action=delete-application-form&id=' . base64_encode($result->id) . '" onclick="return confirm(\'Are you sure you want to delete this application ?\');"><span style="font-size: 20px;" aria-hidden="true" class="glyphicon glyphicon-trash"></span></li></a>';
        }
        if (true == is_user_company_billing() && $current_user->created_by != get_current_user_id() && ($result->gp_number == '' || $result->status == 'New' || $result->status == 'Draft' || $result->crm_id == '')) {
            $opr = '';
        }
        $renew = '';
        if ((int) $result->new_application_id == 0 || $result->is_renewed < 1) {
            $button_text = "Renew";
            if ($role == 'staff' && $result->staff_last_notified_on != '') {
                $button_text = "Submit";
                if (!empty($renewal_data)) {
                    $button_text = "Edit";
                }
                $renew = '<a title="' . $button_text . '" class="btn btn-xs btn-success margin-zero-auto" href="' . site_url() . '/index.php/listings/?page=payment-settings&app_id=' . $result->id . '&certificate_id=' . $result->certificate_crm_id . '">' . $button_text . '</a>';
            } else {
                if ($role == 'customer' && $result->customer_last_notified_on != '' && !empty($renewal_data)) {
                    $renew = '<a title="' . $button_text . '" class="btn btn-xs btn-success margin-zero-auto" href = "' . admin_url() . 'admin-post.php?action=renew_application&appid=' . base64_encode($result->id) . '" title="Renew">' . $button_text . '</a>';
                } else {
                    if ($role == 'customer' && $result->customer_last_notified_on != '' && empty($renewal_data) && $result->status != 'Completed') {
                        $renew = '<a href="#check_renewal_popup" class="btn btn-xs btn-success margin-zero-auto colorbox-inline-payment" title="Renew">Renew</a>';
                    }
                }
            }
        }
        $opr .= '</ul>';
        $certificate_name = isset($result->certificate_name) ? $result->certificate_name : 'Not Available';
        if (isset($result->certificate_url) && !preg_match("~^(?:f|ht)tps?://~i", $result->certificate_url)) {
            $result->certificate_url = "http://" . $result->certificate_url;
        }
        if (!preg_match('%^((https?://)|(www\\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $result->certificate_url)) {
            $result->certificate_url = '';
        }
        if (isset($state) && $state != '' && isset($country) && $country != '') {
            $add = $state . ', ' . $country;
        } else {
            if (isset($state) && $state != '') {
                $add = $state;
            } else {
                if (isset($country) && $country != '') {
                    $add = $country;
                } else {
                    $add = '-';
                }
            }
        }
        $certificate_url = isset($result->certificate_url) && !empty($result->certificate_url) && $certificate_name != 'Not Available' ? '<a class="pull-left certificate_url" target="_blank" style="line-height: 25px;width:150px;" download="" href="' . $result->certificate_url . '">' . $certificate_name . '<span class="btn-wizard-download-tbl">&nbsp;</span></a>' : $certificate_name;
        $application_name = isset($result->application_name) ? $result->application_name : 'Not Available';
        $return["data"][] = array($result->id, $application_name, $certificate_url, $program_name, $company_name, $add, ucwords($result->status), $opr);
    }
    echo json_encode($return);
    die;
}