<?php 
    include("../config.php");
    include("../Datatable.php");

    $where = '';
    $type  = $_GET['type'];
    
    if($type != '' && $type != 'null' && !empty($type)){
        if($type == 'V'){
            $where .= ' AND  type = "'.$type.'"';
        }else if($type == 'A'){
            $where .= ' AND  type = "'.$type.'"';
        }
    }
   

    $obj = new Datatable($conn);

    $obj->aColumns      = array('code', 'name', 'mobile', 'email', 'pan', 'gstin', 'type', 'created_id', 'file_img');
    $obj->eColumns      = array('id');
    $obj->sIndexColumn  = "id";
    $obj->sTable        = " vendor_master";
    $obj->myWhere       = ' WHERE 1=1 ' . $where;
    $obj->datatable_process();
    
    $cou = 0;
    if(isset($_GET['iDisplayStart'])) {
        $cou = $_GET['iDisplayStart'];
    }
    $master_user = combine(getAllRecords('users'), 'id', 'name');
    while ($aRow = mysqli_fetch_array($obj->rResult)) {

        $act_mst = "SELECT * FROM `act_master` WHERE `party_code` = '".$aRow['code']."'";
        $act_res = mysqli_query($conn,$act_mst);
        $act_row = mysqli_fetch_array($act_res);
        
        if($aRow['type'] != 'V'){
            $types = 'AGENT';
        }else{
            $types = 'VENDOR';
        }


        $temp_arr = array();

        $temp_arr[] = ++$cou;
        $temp_arr[] = $aRow['code'];
        $temp_arr[] = $aRow['name'];
        $temp_arr[] = $aRow['mobile'];
        $temp_arr[] = $aRow['pan'];
        $temp_arr[] = $aRow['gstin'];
        $temp_arr[] = $types;
        $temp_arr[] = @$master_user[$aRow['created_id']];
        $str = '';

        if($aRow['file_img'] != ''){
             $str .= '<a href="assets/upload/vendor/'.$aRow['file_img'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="IMAGE" target="_blank"><em class="fa fa-picture-o"></em></a>&nbsp;';
        }
        if( hasPermission($_SESSION['user_detail']['role_id'], 'vendor_edit') ) {
        $str .= '<a href="index.php?page=vendor/edit.php&eid='.$aRow['id'].'" name="edit" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="EDIT VENDOR...!!"><em class="fa fa-pencil"></em></a>&nbsp;';
        }

        if( hasPermission($_SESSION['user_detail']['role_id'], 'vendor_delete') ) {
        $str .= '<button type="button" class="btn btn-danger delete_data" id="'.$aRow["id"].'" data-toggle="tooltip" data-placement="top" title="DELETE VENDOR..!!" onclick="confirmDelete(this);"><em class="fa fa-trash"></em></button>';
        }
        $temp_arr[] = $str;

        $obj->output['aaData'][] = $temp_arr;
    }

    echo json_encode($obj->output);
    exit();
?>