<?php 
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call"){
    require "../inc/ajax_header.php";
    $country_id = $_POST["country_id"];
    $getData = $commonFunctions->getData('sys_erp_adm_countries_states'," WHERE country_id = '".$country_id."' and active = 1 ");
    $html = "";
    foreach ($getData as $rw) {
        $html .= "<option value='".$rw['rowid']."'>".$rw['label']."</option>";
    }
    echo json_encode(array("option"=>$html));
    exit();
}
if(!empty($_GET["action"]) and $_GET["action"] == "ajax_call1"){
    require "../inc/ajax_header.php";
    $country_id = $_GET["country_id"];
    $state_id = $_GET["state_id"];
    $keyword = $_GET["keyword"]; 
    $getData = $commonFunctions->getData('sys_erp_adm_countries_states_cities'," WHERE country_id = '".$country_id."' and state_id = '".$state_id."' and name LIKE '%".$keyword."%' ");
    $html = "";
    $skillData = array(); 
    foreach ($getData as $rw) {
        $data['id'] = $rw['name']; 
        $data['value'] = $rw['name']; 
        array_push($skillData, $data); 
    } 
    echo json_encode($skillData);
    exit();
}
require "../inc/header.php";
Session::CheckSession(); 
$msg = Session::get('msg');
Session::set("msg", NULL); 
if(!empty($_POST)){
    $error_ = 0;
    if(empty($_POST['vendor_code'])){
        Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Please enter code !</div>');
    }else{
        $cntA = $commonFunctions->getRowData('vndr_details',' WHERE vendor_code = "'.$_POST['vendor_code'].'" and id != "'.$_POST['vendor_id'].'" ');
        if(@count($cntA) > 1){
           $error_ = 1;
           Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Vendor & Suppliers code already exists !</div>');
        }
    }

    if(!empty($_POST['vendor_code']) and $error_ == 0){
        if($_POST['action'] == "add"){
            $column = $fileName = "";
            if(!empty($_FILES["logo"]["name"])){
                $target_dir = "../uploads/supplier-vendor/";
                $target_file = $target_dir . basename($_FILES["logo"]["name"]);
                $uploadOk = 1;
                $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                @getimagesize($_FILES["logo"]["tmp_name"]);
                if(move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)){
                    $column = " ,logo";
                    $fileName = " , '".$_FILES["logo"]["name"]."'";
                }
            }
            $custcats = @implode(',', $_POST['custcats']);

            $last_id = $commonFunctions->addData('vndr_details'," (name,alias_name,client_type,customer_code,vendor_type,vendor_code,status,barcode,address,postcode,city,country,state,email,web,phone,fax,professional_id_1,professional_id_2,professional_id_3,professional_id_4,professional_id_5,professional_id_6,sales_tax_use,sales_tax_id,type_of_company,staff,entity_type,capital,incoterm_id,customer_cat,currency,assigned_to_sales,org_sm_page_linkedin,org_sm_page_twitter,org_sm_page_facebook,org_sm_page_instagram,org_id_msngr_whatsapp".$column.") VALUES ('".$_POST['name']."','".$_POST['alias_name']."','".$_POST['client']."','".$_POST['customer_code']."','".$_POST['fournisseur']."','".$_POST['vendor_code']."','".$_POST['status']."','".$_POST['barcode']."','".@str_replace("'","",$_POST['address'])."','".$_POST['postcode']."','".$_POST['city']."','".$_POST['country']."','".$_POST['state']."','".$_POST['email']."','".$_POST['web']."','".$_POST['phone']."','".$_POST['fax']."','".$_POST['professional_id_1']."','".$_POST['professional_id_2']."','".$_POST['professional_id_3']."','".$_POST['professional_id_4']."','".$_POST['professional_id_5']."','".$_POST['professional_id_6']."','".$_POST['sales_tax_use']."','".$_POST['sales_tax_id']."','".$_POST['type_of_company']."','".$_POST['staff']."','".$_POST['entity_type']."','".$_POST['capital']."','".$_POST['incoterm_id']."','".$custcats."','".$_POST['currency']."','".$_POST['assigned_to_sales']."','".$_POST['erp_sys_org_sm_page_linkedin']."','".$_POST['erp_sys_org_sm_page_twitter']."','".$_POST['erp_sys_org_sm_page_facebook']."','".$_POST['erp_sys_org_sm_page_instagram']."','".$_POST['erp_sys_org_id_msngr_whatsapp']."'".$fileName." ) ",'');
            
            $rand = rand(0,999999999999999);
            if($_POST['customer_status'] == 0){
                $commonFunctions->addData('global_organaization'," (global_org_name,global_org_address,global_org_city,global_org_province,global_org_country,global_org_zip_code,global_org_phone_work,global_org_fax,global_email_work,global_org_tax_id_1,vndr_id,vndr_ref_id,glo_org_rand_num,global_org_sm_page_linkedin,global_org_sm_page_twitter,global_org_sm_page_facebook,global_org_sm_page_instagram,global_org_id_msngr_whatsapp) VALUES ('".$_POST['name']."','".@str_replace("'","",$_POST['address'])."','".$_POST['city']."','".$_POST['state']."','".$_POST['country']."','".$_POST['postcode']."','".$_POST['phone']."','".$_POST['fax']."','".$_POST['email']."','".$_POST['sales_tax_id']."','".$last_id."','".$_POST['customer_code']."','".$rand."','".$_POST['erp_sys_org_sm_page_linkedin']."','".$_POST['erp_sys_org_sm_page_twitter']."','".$_POST['erp_sys_org_sm_page_facebook']."','".$_POST['erp_sys_org_sm_page_instagram']."','".$_POST['erp_sys_org_id_msngr_whatsapp']."' ) ",'');
            }else{
                $commonFunctions->addData('global_contacts'," (cont_random_num,global_user_name_first,global_user_email_personal,global_user_mobile1,global_org_name,global_user_job_title,global_org_fax,global_user_email_work,global_user_id_sm_linkedin,global_user_id_sm_twitter,global_user_id_sm_facebook,global_user_id_sm_instagram,global_user_id_msngr_whatsapp) VALUES ('".$rand."','".$_POST['name']."','".$_POST['email']."','".$_POST['phone']."','".$_POST['name']."','Vendor','".$_POST['fax']."','".$_POST['email']."','".$_POST['fax']."','".$_POST['email']."','".$_POST['erp_sys_org_sm_page_linkedin']."','".$_POST['erp_sys_org_sm_page_twitter']."','".$_POST['erp_sys_org_sm_page_facebook']."','".$_POST['erp_sys_org_sm_page_instagram']."','".$_POST['erp_sys_org_id_msngr_whatsapp']."' ) ",'');
            }
            echo "<script> window.location.href='vendor.php?state=form'; </script>";
        }
        if($_POST['action'] == "edit"){
            $column = $fileName = "";
            if(!empty($_FILES["logo"]["name"])){
                $target_dir = "../uploads/supplier-vendor/";
                $target_file = $target_dir . basename($_FILES["logo"]["name"]);
                $uploadOk = 1;
                $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                @getimagesize($_FILES["logo"]["tmp_name"]);
                if(move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)){
                    $column = " ,logo";
                    $fileName = " ,logo = '".$_FILES["logo"]["tmp_name"]."' ";
                }
            }
            $commonFunctions->updateData('vndr_details'," WHERE id = '".$_POST["vendor_id"]."' ", " alias_name='".$_POST['alias_name']."',name = '".$_POST['name']."', client_type='".$_POST['client']."' , customer_code='".$_POST['customer_code']."' , vendor_type='".$_POST['fournisseur']."' , vendor_code='".$_POST['vendor_code']."' , status='".$_POST['status']."' , barcode='".$_POST['barcode']."', address='".@str_replace("'","",$_POST['address'])."', postcode='".$_POST['postcode']."', city='".$_POST['city']."', country='".$_POST['country']."', state='".$_POST['state']."', email='".$_POST['email']."', web='".$_POST['web']."', phone='".$_POST['phone']."', fax='".$_POST['fax']."', professional_id_1='".$_POST['professional_id_1']."', professional_id_2='".$_POST['professional_id_2']."', professional_id_3='".$_POST['professional_id_3']."', professional_id_4='".$_POST['professional_id_4']."', professional_id_5='".$_POST['professional_id_5']."', professional_id_6='".$_POST['professional_id_6']."', sales_tax_use='".$_POST['sales_tax_use']."', sales_tax_id='".$_POST['sales_tax_id']."', type_of_company='".$_POST['type_of_company']."', staff='".$_POST['staff']."', entity_type='".$_POST['entity_type']."', capital='".$_POST['capital']."', incoterm_id='".$_POST['incoterm_id']."', currency='".$_POST['currency']."', assigned_to_sales='".$_POST['assigned_to_sales']."'".$fileName,'vendor.php?state=list');
        }
    }else{
        echo "<script>location.href='vendor.php?state=form';</script>";
    }
}
?>    

<!-- Page-Title -->
<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box">
            <div class="row">
                <div class="col">
                    <h4 class="page-title">Vendor & Suppliers</h4>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Vendor & Suppliers</a></li>
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Vendor & Suppliers <?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></a></li>
                    </ol>
                </div><!--end col-->
                <!-- <div class="col-auto align-self-center">
                    <a href="#" class="btn btn-sm btn-outline-primary" id="Dash_Date">
                        <span class="day-name" id="Day_Name">Today:</span>&nbsp;
                        <span class="" id="Select_date">Jan 11</span>
                        <i data-feather="calendar" class="align-self-center icon-xs ml-1"></i>
                    </a>
                    <a href="#" class="btn btn-sm btn-outline-primary">
                        <i data-feather="download" class="align-self-center icon-xs"></i>
                    </a>
                </div> --><!--end col-->  
            </div><!--end row-->                                                              
        </div><!--end page-title-box-->
    </div><!--end col-->
</div><!--end row-->
<!-- end page title end breadcrumb -->

<?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="row justify-content-center">
                <div class="col-md-6 col-lg-3">
                    <div class="card report-card">
                        <div class="card-body">
                            <div class="row d-flex justify-content-center">
                                <div class="col">
                                    <p class="text-dark mb-0 font-weight-semibold">Total Vendor</p>
                                    <h3 class="m-0"><?php echo $commonFunctions->getRowDataCount('vndr_details',''); ?></h3>
                                    <!--p class="mb-0 text-truncate text-muted">In Inventory</p-->
                                </div>
                                <div class="col-auto align-self-center">
                                    <div class="report-main-icon bg-light-alt">
                                        <i data-feather="users" class="align-self-center text-muted icon-sm"></i>  
                                    </div>
                                </div>
                            </div>
                        </div><!--end card-body--> 
                    </div><!--end card--> 
                </div> <!--end col--> 
                <div class="col-md-6 col-lg-3">
                    <div class="card report-card">
                        <div class="card-body">
                            <div class="row d-flex justify-content-center">                                                
                                <div class="col">
                                    <p class="text-dark mb-0 font-weight-semibold">My Vendor</p>                                                    
                                    <!--p class="mb-0 text-truncate text-muted">Barwa Shop 4</p-->
                                    <h3 class="m-0"><?php echo $commonFunctions->getRowDataCount('vndr_details',' WHERE staff = "'.$_SESSION['id'].'"'); ?></h3>
                                </div>
                                <div class="col-auto align-self-center">
                                    <div class="report-main-icon bg-light-alt">
                                        <i data-feather="clock" class="align-self-center text-muted icon-sm"></i>  
                                    </div>
                                </div> 
                            </div>
                        </div><!--end card-body--> 
                    </div><!--end card--> 
                </div> <!--end col--> 
                <div class="col-md-6 col-lg-3">
                    <div class="card report-card">
                        <div class="card-body">
                            <div class="row d-flex justify-content-center">                                                
                                <div class="col">
                                    <p class="text-dark mb-0 font-weight-semibold">Active Vendor</p>
                                    <h3 class="m-0"><?php echo $commonFunctions->getRowDataCount('vndr_details',' WHERE status = "Open"'); ?> </h3>
                                    <!--p class="mb-0 text-truncate text-muted">Listed Products</p-->
                                </div>
                                <div class="col-auto align-self-center">
                                    <div class="report-main-icon bg-light-alt">
                                        <i data-feather="clock" class="align-self-center text-muted icon-sm"></i>  
                                    </div>
                                </div> 
                            </div>
                        </div><!--end card-body--> 
                    </div><!--end card--> 
                </div> <!--end col--> 
                <div class="col-md-6 col-lg-3">
                    <div class="card report-card">
                        <div class="card-body">
                            <div class="row d-flex justify-content-center">                                                
                                <div class="col">
                                    <p class="text-dark mb-0 font-weight-semibold">In-Active Vendor</p>
                                    <h3 class="m-0"><?php echo $commonFunctions->getRowDataCount('vndr_details',' WHERE status = "Closed"'); ?></h3>
                                    <!--p class="mb-0 text-truncate text-muted">Products Below Desired Stock</p-->
                                </div>
                                <div class="col-auto align-self-center">
                                    <div class="report-main-icon bg-light-alt">
                                        <i data-feather="clock" class="align-self-center text-muted icon-sm"></i>  
                                    </div>
                                </div> 
                            </div>
                        </div><!--end card-body--> 
                    </div><!--end card--> 
                </div> <!--end col-->                        
            </div><!--end row-->
        </div>
    </div>
<?php endif; ?>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">
                    <div class="form-row">
                        <div class="form-group searchForm col-md-6">
                            <a href="vendor.php?state=form"><button type="button" class="btn btn-outline-primary">Add Vendor &amp; Supplier</button></a>
                        </div>
                        <div class="form-group searchForm col-md-6">
                            <!-- <h4 class="card-title">Customer<?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></h4> -->
                            <?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): ?>
                                <form action="" id="searchForm" method="GET">
                                    <div class="form-row">
                                        <div class="form-group searchForm col-md-8">
                                            <input type="text" name="search" id="search" placeholder="Search" class="form-control" value="<?php echo @$_GET['search']; ?>">
                                            <input type="hidden" name="state" value="<?php echo @$_GET['state']; ?>">
                                            <input type="hidden" name="page" value="<?php echo @$_GET['page']; ?>">
                                        </div>
                                        <div class="form-group searchForm col-md-4">
                                            <button type="submit" id="searchbtn" class="btn btn-primary"> <i class="fa fa-search"></i> Search</button>
                                            <button type="button" id="clearbtn" class="btn btn-danger"> <i class="fa fa-trash"></i> Clear</button>
                                        </div>
                                    </div>
                                </form>
                            <?php endif; ?>
                        </div>
                    </div>
                </h4>
                <?php echo !empty($msg)?$msg:''; ?>        
                <?php echo !empty($msg1)?$msg1:''; ?>        
            </div>
            <?php 
            if(!empty($_GET['vendor_id'])){
                $vendorData = $commonFunctions->getRowData('vndr_details',' WHERE id = "'.$_GET['vendor_id'].'" ');
            }
            ?>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        <div class="main-card mb-3 ">
                            <!-- <div class="card-body"> -->
                                <?php if(!empty($_GET['state']) and $_GET['state'] == "form"): ?>
                                    <form enctype="multipart/form-data" action="vendor.php?state=form&action=add" method="post" name="formsoc" autocomplete="off">      
                                        <div class="form-row">
                                            <input type="hidden" name="action" value="<?php echo !empty($_GET['vendor_id'])?'edit':'add';?>">
                                                <input type="hidden" name="vendor_id" value="<?php echo @$_GET['vendor_id']; ?>">

                                            <div class="position-relative form-group col-md-4">
                                                <label for="code" class="">Vendor & Suppliers Name <span class="error">*</span></label>
                                                <input type="text" maxlength="128" name="name" id="name" value="<?php echo @$vendorData['name'];?>" autofocus="autofocus" class="form-control">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Alias Name (Commercial, Trademark, ...) <span class="error">*</span></label>
                                                <input type="text" class="form-control" name="alias_name" value="<?php echo @$vendorData['alias_name'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Prospect / Vendor & Suppliers <span class="error">*</span></label>
                                                <select class="flat form-control" name="client" id="customerprospect">
                                                    <option value="1" <?php echo (!empty($vendorData['client']) and $vendorData['client'] == 1)?"selected":"";?> >Vendor & Suppliers</option>
                                                    <option value="3" <?php echo (!empty($vendorData['client']) and $vendorData['client'] == 3)?"selected":"";?> >Prospect / Vendor & Suppliers</option>
                                                    <option value="2" <?php echo (!empty($vendorData['client']) and $vendorData['client'] == 2)?"selected":"";?> >Prospect</option>
                                                    <option value="0" <?php echo (@$vendorData['client'] == "0")?"selected":"";?> >Not Prospect, or Vendor & Suppliers</option>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Vendor & Suppliers Status <span class="error">*</span></label>
                                                <select class="flat form-control" id="customer_status" name="customer_status">
                                                    <option value="1" <?php echo !empty($vendorData['customer_status'])?"selected":"";?> >Individual</option>
                                                    <option value="0" <?php echo empty($vendorData['customer_status'])?"selected":"";?> >Organization</option>
                                                </select>
                                            </div>
                                            <!-- <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Vendor Code <span class="error">*</span></label>
                                                <input type="text" class="form-control" name="customer_code" value="CUST-<?php echo @$commonFunctions->getRowDataCount('vndr_details','');?>">
                                            </div> -->
                                            <input type="hidden" name="fournisseur" value="1">
                                            <input type="hidden" name="customer_code" value="0">
                                            <!-- <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Vendor <span class="error">*</span></label>
                                                <select class="flat form-control" id="fournisseur" name="fournisseur">
                                                    <option value="1">Yes</option>
                                                    <option value="0" selected="">No</option>
                                                </select>
                                            </div> -->
                                            <!-- <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Vendor & Suppliers Code <span class="error">*</span></label>
                                            </div> -->
                                                    <?php $seriNo2 = $commonFunctions->getRowDataCount('vndr_details',''); 
                                                        $seriNo3 = $commonFunctions->random_num(10 - strlen($seriNo2));
                                                        if(!empty($vendorData['vendor_id'])){
                                                    ?>
                                                        <input type="hidden" class="form-control" name="vendor_code" value="<?php echo @$vendorData['vendor_code'];?>">
                                                        <input type="hidden" class="form-control" name="barcode" value="<?php echo @$vendorData['vendor_code'];?>">
                                                    <?php }else{ ?>
                                                        <input type="hidden" class="form-control" name="vendor_code" value="VNDR-<?php echo @$seriNo3.$seriNo2;?>">
                                                        <input type="hidden" class="form-control" name="barcode" value="SU-<?php echo @$seriNo3.$seriNo2;?>">
                                                    <?php } ?>
                                            
                                            <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Status <span class="error">*</span></label>
                                                <select id="status" class="flat status form-control" name="status" >
                                                    <option value="Open" <?php echo (!empty($vendorData['status']) and @$vendorData['status'] == "Open" )?"selected":"";?> >Open</option>
                                                    <option value="Closed" <?php echo (!empty($vendorData['status']) and @$vendorData['status'] == "Closed" )?"selected":"";?>>Closed</option>
                                                </select>
                                            </div>
                                            <!-- <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Barcode <span class="error">*</span></label>
                                            </div> -->
                                            

                                            <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Address <span class="error">*</span></label>
                                                <textarea name="address" class="form-control"><?php echo @$vendorData['address'];?></textarea>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="code" class=""> Postcode <span class="error">*</span></label>
                                                <input type="text" class="form-control" name="postcode" value="<?php echo @$vendorData['postcode'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="city" class=""> Country <span class="error">*</span></label>
                                                <?php $countryData = $commonFunctions->getData('sys_erp_adm_countries',' WHERE active = 1 '); ?>
                                                <select name="country" id="country" class="select2 form-control" >
                                                    <option value="0">Select Country</option>
                                                    <?php foreach ($countryData as $rw) { ?>
                                                        <option <?php echo (!empty($vendorData['country_id']) and $vendorData['country_id'] == $rw['rowid'] )?"selected":"";?> value="<?php echo $rw['rowid'];?>" ><?php echo $rw['label']."(".$rw['code'].")"; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="state" class=""> State / Province <span class="error">*</span></label>
                                                <?php $stateData = $commonFunctions->getData('sys_erp_adm_countries_states',' WHERE rowid = "'.@$vendorData['state_id'].'" '); ?>
                                                <select name="state" id="state" class="select2 form-control" >
                                                    <option value="0">Select State / Province</option>
                                                    <?php foreach ($stateData as $rw) { ?>
                                                        <option selected="" value="<?php echo $rw['rowid'];?>" ><?php echo $rw['label']."(".$rw['region_code'].")"; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="city" class=""> City <span class="error">*</span></label>
                                                <input type="text" class="form-control" name="city" id="city" value="<?php echo @$vendorData['city'];?>">
                                            </div>

                                            <div class="position-relative form-group col-md-4">
                                                <label for="email" class=""> Email <span class="error">*</span></label>
                                                <input type="email" class="form-control" name="email" value="<?php echo @$vendorData['email'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="web" class=""> Web </label>
                                                <input type="text" class="form-control" name="web" value="<?php echo @$vendorData['web'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="phone" class=""> Phone <span class="error">*</span></label>
                                                <input type="text" class="form-control" name="phone" value="<?php echo @$vendorData['phone'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="fax" class=""> Fax <span class="error">*</span></label>
                                                <input type="text" class="form-control" name="fax" value="<?php echo @$vendorData['fax'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="professional_id_1" class=""> Professional ID 1   </label>
                                                <input type="text" class="form-control" name="professional_id_1" value="<?php echo @$vendorData['professional_id_1'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="professional_id_2" class=""> Professional ID 2   </label>
                                                <input type="text" class="form-control" name="professional_id_2" value="<?php echo @$vendorData['professional_id_2'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="professional_id_3" class=""> Professional ID 3   </label>
                                                <input type="text" class="form-control" name="professional_id_3" value="<?php echo @$vendorData['professional_id_3'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="professional_id_4" class=""> Professional ID 4   </label>
                                                <input type="text" class="form-control" name="professional_id_4" value="<?php echo @$vendorData['professional_id_4'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="professional_id_5" class=""> Professional ID 5   </label>
                                                <input type="text" class="form-control" name="professional_id_5" value="<?php echo @$vendorData['professional_id_5'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="professional_id_6" class=""> Professional ID 6   </label>
                                                <input type="text" class="form-control" name="professional_id_6" value="<?php echo @$vendorData['professional_id_6'];?>">
                                            </div>

                                            <div class="position-relative form-group col-md-4">
                                                <label for="sales_tax_use" class=""> Sales TAX Used <span class="error">*</span></label>
                                                <select class="flat form-control" id="fournisseur" name="sales_tax_use">
                                                    <option value="1" <?php echo !empty($vendorData['sales_tax_use'])?"selected":"";?>>Yes</option>
                                                    <option value="0" <?php echo empty($vendorData['sales_tax_use'])?"selected":"";?>>No</option>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="sales_tax_id" class=""> Sales Tax/VAT ID <span class="error">*</span></label>
                                                
                                                <input type="text" class="form-control" name="sales_tax_id" value="<?php echo @$vendorData['sales_tax_id'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <?php $companyData = $commonFunctions->getData('vndr_type_of_company',' '); ?>
                                                <label for="type_of_company" class=""> Type of Company <span class="error">*</span></label>
                                                <select id="type_of_company" class="flat type_of_company form-control" name="type_of_company">
                                                    <option value="0">Select Type of Company</option>
                                                    <?php foreach ($companyData as $rw) { ?>
                                                        <option <?php echo (!empty($vendorData['type_of_company']) and $vendorData['type_of_company'] == $rw['id'] )?"selected":"";?> value="<?php echo $rw['id'];?>" ><?php echo $rw['label'];?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="staff" class=""> Staff <span class="error">*</span></label>.
                                                <?php $staffData = $commonFunctions->getData('tbl_staff',' WHERE status = 0 '); ?>
                                                <select id="staff" class="flat staff form-control" name="staff">
                                                    <option value="0">Select Staff</option>
                                                    <?php foreach ($staffData as $rw) { ?>
                                                        <option <?php echo (!empty($vendorData['staff']) and $vendorData['staff'] == $rw['id'] )?"selected":"";?> value="<?php echo $rw['id'];?>" ><?php echo $rw['label']."(".$rw['code'].")"; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="entity_type" class=""> Legal Entity Type </label>
                                                <select id="entity_type" class="flat entity_type form-control" name="entity_type">
                                                   <option value="0">Select Legal Entity Type</option>
                                                   
                                                </select>
                                            </div>

                                            <div class="position-relative form-group col-md-4">
                                                <label for="entity_type" class=""> Capital <span class="error">*</span></label>
                                                <input type="text" class="form-control" name="capital" value="<?php echo @$vendorData['capital'];?>">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="incoterm_id" class=""> Incoterms </label>
                                                <?php $IncotermsData = $commonFunctions->getData('tbl_incoterms',' WHERE status = 0 '); ?>
                                                <select id="incoterm_id" class="flat incoterm_id form-control" name="incoterm_id">
                                                    <option value="0">Incoterm</option>
                                                    <?php foreach ($IncotermsData as $rw) { ?>
                                                        <option <?php echo (!empty($vendorData['incoterm_id']) and $vendorData['incoterm_id'] == $rw['id'] )?"selected":"";?> value="<?php echo $rw['id'];?>" ><?php echo $rw['label']."(".$rw['code'].")"; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="custcats" class=""> Cust./Prosp. Tags / Categories </label>
                                                <select id="custcats" name="custcats[]" class="flat custcats form-control">
                                                    <option value="0"></option>
                                                    <?php $TagData = $commonFunctions->getData('crm_cust_tag',''); ?>
                                                    <?php foreach ($TagData as $rw) { ?>
                                                        <option value="<?php echo $rw['id'];?>" ><?php 
                                                        if($rw['parent_id'] > 0){
                                                            echo $commonFunctions->getName($rw['parent_id'],'crm_cust_tag','label','id')." >> ";           
                                                        }
                                                        echo $rw['label']; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="currency" class=""> Currency </label>
                                                <?php $currencyData = $commonFunctions->getData('fin_accnt_currencies',' WHERE active = 1 '); ?>
                                                <select id="currency" name="currency" class="flat currency form-control">
                                                    <option value="0">Select</option>
                                                    <?php foreach ($currencyData as $rw) { ?>
                                                        <option <?php echo (!empty($vendorData['currency']) and $vendorData['currency'] == $rw['code_iso'] )?"selected":"";?> value="<?php echo $rw['code_iso'];?>" ><?php echo $rw['label']."(".$rw['code_iso'].")"; ?></option>     
                                                    <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="assigned_to_sales" class=""> Assigned to Sales Representative</label>
                                                <select id="assigned_to_sales" name="assigned_to_sales" class="flat assigned_to_sales form-control">
                                                    <option value="0"></option>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="assigned_to_sales" class=""> Logo </label>
                                                <input type="file" name="logo" >
                                            </div>
                                            
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_sm_page_linkedin" class="">linkedIn</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_sm_page_linkedin" id="erp_sys_org_sm_page_linkedin" value="<?php echo @$vendorData['erp_sys_org_sm_page_linkedin'];?>" autofocus="autofocus" class="form-control" placeholder="linkedIn">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_sm_page_twitter" class="">Twitter</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_sm_page_twitter" id="erp_sys_org_sm_page_twitter" value="<?php echo @$vendorData['erp_sys_org_sm_page_twitter'];?>" autofocus="autofocus" class="form-control" placeholder="Twitter">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_sm_page_facebook" class="">Facebook</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_sm_page_facebook" id="erp_sys_org_sm_page_facebook" value="<?php echo @$vendorData['erp_sys_org_sm_page_facebook'];?>" autofocus="autofocus" class="form-control" placeholder="Facebook">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_sm_page_instagram" class="">Instagram</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_sm_page_instagram" id="erp_sys_org_sm_page_instagram" value="<?php echo @$vendorData['erp_sys_org_sm_page_instagram'];?>" autofocus="autofocus" class="form-control" placeholder="Facebook">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_id_msngr_whatsapp" class="">Business Whatsapp</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_id_msngr_whatsapp" id="erp_sys_org_id_msngr_whatsapp" value="<?php echo @$vendorData['erp_sys_org_id_msngr_whatsapp'];?>" autofocus="autofocus" class="form-control" placeholder="Business Whatsapp">
                                            </div>
                                            <div class="position-relative form-group col-md-12">
                                                <button type="submit" class="btn btn-success">Create</button>
                                                <a href="vendor.php?state=list"><button type="button" class="btn btn-danger">Back to Vendor & Suppliers List</button></a>
                                            </div>
                                        </div>
                                    </form>
                                <?php endif; ?>

                                <?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): 

                                    $targetpage = "vendor.php?state=list";

                                    $limit = 50;
                                    $total_pages = $commonFunctions->getData('vndr_details','');
                                    $total_pages = count($total_pages);
                                    $stages = 3;
                                    $page = @$_GET['page'];
                                    if($page){
                                        $start = ($page - 1) * $limit; 
                                    }else{
                                        $start = 0; 
                                    } 
                                    
                                    $search_con = "";
                                    if(!empty($_GET['search'])){
                                        $countryArr = $commonFunctions->getIDs('sys_erp_adm_countries','rowid'," WHERE label LIKE '%".$_GET['search']."%' ");
                                        $sys_erp_adm_countries_statesArr = $commonFunctions->getIDs('sys_erp_adm_countries_states','rowid'," WHERE label LIKE '%".$_GET['search']."%' ");
                                        $country_ids = @$countryArr["ids"];
                                        $state_ids = @$sys_erp_adm_countries_statesArr["ids"];
                                        $search_con = " AND vendor_code LIKE '%".$_GET['search']."%' OR name LIKE '%".$_GET['search']."%' OR barcode LIKE '%".$_GET['search']."%' OR alias_name LIKE '%".$_GET['search']."%' OR vendor_code LIKE '%".$_GET['search']."%' OR email LIKE '%".$_GET['search']."%' OR phone LIKE '%".$_GET['search']."%' OR postcode LIKE '%".$_GET['search']."%' ";
                                        
                                        if(!empty($country_ids)){ $search_con .= "  OR country IN(".$country_ids.") "; }
                                        if(!empty($state_ids)){ $search_con .= "  OR state IN(".$state_ids.") "; }
                                    }
                                    $where = " WHERE id ".$search_con." ORDER BY id DESC LIMIT $start, $limit ";

                                    // Initial page num setup
                                    if ($page == 0){ $page = 1; }
                                    $prev = $page - 1;  
                                    $next = $page + 1;  
                                    $lastpage = ceil($total_pages/$limit);  
                                    $LastPagem1 = $lastpage - 1;    
                                    $paginate = '';
                                    if($lastpage > 1)
                                    {       
                                        $paginate .= "<div class='paginate'>";
                                        // Previous

                                        if ($page > 1){
                                            $paginate.= "<a href='$targetpage&page=$prev'>Previous</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Previous</span>";   }
                                        // Pages    
                                        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
                                        {   
                                            for ($counter = 1; $counter <= $lastpage; $counter++)
                                            {
                                                if ($counter == $page){
                                                    $paginate.= "<span class='current'>$counter</span>";
                                                }else{
                                                    $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                            }
                                        }
                                        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
                                        {
                                            // Beginning only hide later pages
                                            if($page < 1 + ($stages * 2))   
                                            {
                                                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // Middle hide some front and some back
                                            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // End only hide early pages
                                            else
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}
                                                }
                                            }       
                                        }       
                                                // Next
                                        if ($page < $counter - 1){ 
                                            $paginate.= "<a href='$targetpage&page=$next'>Next</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Next</span>";
                                            }
                                        $paginate.= "</div>";   
                                    }
                                    $resultData = $commonFunctions->getData('vndr_details',$where);
                                    ?>
                                    <div class="">
                                        <table style="width: 100% !important;" class="table datatable_t table-hover table-striped table-bordered">
                                            <thead>
                                                <tr>
                                                    <th  class="text-center">Vendor Name</th>
                                                    <th  class="text-center">Vendor ID#</th>
                                                    <th  class="text-center">Phone</th>
                                                    <th  class="text-center">Email</th>
                                                    <th  class="text-center">City</th>
                                                    <th  class="text-center">Country</th>
                                                    <th  class="text-center">State</th>
                                                    <th  class="text-center">Status</th>
                                                </tr>

                                            </thead>
                                            <tbody>
                                                <?php foreach ($resultData as $rw) { ?>
                                                    <tr onclick="redirect_('vendor-card.php?vendor_id=<?php echo $rw['id'];?>')" >
                                                        <td class="text-primary"><?php echo $rw['name'];?></td>
                                                        <td class="text-primary"><?php echo $rw['vendor_code'];?></td>
                                                        
                                                        <td><?php echo $rw['phone'];?></td>
                                                        <td><?php echo $rw['email'];?></td>
                                                        <td><?php echo $rw['city'];?></td>
                                                        <td><?php echo $commonFunctions->getName($rw['country'],"sys_erp_adm_countries","label","rowid");?>
                                                            <?php echo $rw['country'];?></td>
                                                        <td><?php echo $commonFunctions->getName($rw['state'],"sys_erp_adm_countries_states","label","rowid");?></td>

                                                        <td>
                                                            <?php if($rw['status'] == "Open"){ ?>
                                                                <span class="badge badge-soft-success">Open</span>
                                                            <?php }else{ ?>
                                                                <span class="badge badge-soft-danger">Closed</span>
                                                            <?php } ?>
                                                        </td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <?php echo "<center>".$paginate."</center>";?>
                                <?php endif; ?>
                            <!-- </div> -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div> <!-- end col -->
</div> <!-- end row -->

<?php require "../inc/footer.php"; ?>
<script type="text/javascript">
$(document).ready(function() {
    $('.datatable_t1').DataTable({
        "scrollX": true,
        "paging":   false,
        "info":     false
    });
});
/*function format ( d ) {
    return 'Full name: '+d.first_name+' '+d.last_name+'<br>'+
        'Salary: '+d.salary+'<br>'+
        'The child row can contain any data you wish, including links, images, inner tables etc.';
}
 
$(document).ready(function() {
    var dt = $('.datatable_t').DataTable( {
        "processing": false,
        "serverSide": false,
        "columns": [ 
            {
                "class":          "details-control",
                "orderable":      false,
                "data":           null,
                "defaultContent": ""
            },
        ],
        "order": [[1, 'asc']]
    } );
 
    // Array to track the ids of the details displayed rows
    var detailRows = [];
 
    $('.datatable_t tbody').on( 'click', 'tr td.details-control', function () {
        var tr = $(this).closest('tr');
        var row = dt.row( tr );
        var idx = $.inArray( tr.attr('id'), detailRows );
 
        if ( row.child.isShown() ) {
            tr.removeClass( 'details' );
            row.child.hide();
 
            // Remove from the 'open' array
            detailRows.splice( idx, 1 );
        }
        else {
            tr.addClass( 'details' );
            row.child( format( row.data() ) ).show();
 
            // Add to the 'open' array
            if ( idx === -1 ) {
                detailRows.push( tr.attr('id') );
            }
        }
    } );
 
    // On each draw, loop over the `detailRows` array and show any child rows
    dt.on( 'draw', function () {
        $.each( detailRows, function ( i, id ) {
            $('#'+id+' td.details-control').trigger( 'click' );
        } );
    } );
} );*/
$("#country").change(function(){
    var country = $("#country option:selected").val();
    $.ajax({
        type: 'POST',
        url: "",
        data: {"country_id":country,"action":"ajax_call"},
        success: function(response) { 
            obj = JSON.parse(response);
            $("#state").html("<option value=''>Select State / Province</option>"+obj.option);
        }
    });
})
$(function() {
    $("#city").autocomplete({
        source: function(request, response) {
            $.getJSON("", { country_id: $('#country option:selected').val(),state_id: $('#state option:selected').val(),keyword: request.term,action:"ajax_call1" }, response);
        },
        minLength: 2,
    });
});
function redirect_(uri){
    window.location.href = uri;
}
</script>