<?php 
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));
require "../inc/header.php";
Session::CheckSession(); 
$msg = Session::get('msg');
Session::set("msg", NULL); 
if(!empty($_POST)){
    $error_ = 0;
    if(empty($_POST['reference_number'])){
        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_pj',' WHERE reference_number = "'.$_POST['reference_number'].'" ');
        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> Customer code already exists !</div>');
        }
    }
    if(!empty($_POST['reference_number']) and $error_ == 0){
        if($_GET['action'] == "add"){
            $commonFunctions->addData('vndr_pj'," (reference_number,vendor_id,label,visibility,start_date,end_date,lead_status,lead_probability,lead_amount,budget,description,category_id,lead_code) VALUES ('".$_POST['reference_number']."','".$_POST['vendor_id']."','".$_POST['label']."','".$_POST['visibility']."','".$_POST['start_date']."','".$_POST['end_date']."','".$_POST['lead_status']."','".$_POST['lead_probability']."','".$_POST['lead_amount']."','".$_POST['budget'].",'".$_POST['description']."','".$_POST['category_id']."','".$_POST['lead_code']."') ",'project.php?state=form');
        }
    }else{
        echo "<script>location.href='project.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">New Project</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);">New Project <?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></a></li>
                    </ol>
                </div><!--end col-->
                  
            </div><!--end row-->                                                              
        </div><!--end page-title-box-->
    </div><!--end col-->
</div><!--end row-->
<!-- end page title end breadcrumb -->
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">New Project <?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></h4>
                <?php echo !empty($msg)?$msg:''; ?>        
                <?php echo !empty($msg1)?$msg1:''; ?>        
            </div>
    
            <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="project.php?state=form&action=add" method="post" name="formsoc" autocomplete="off"> 
                                    <div class="form-row">
                                        <div class="position-relative form-group col-md-4">
                                            <label for="reference_number" class="">Ref. Number <span class="error">*</span></label>
                                            <input type="text" name="reference_number" id="reference_number" value="PJ-00<?php echo 1 + @$commonFunctions->getRowDataCount('vndr_pj','');?>" class="form-control" >
                                        </div>
                                        <div class="position-relative form-group col-md-4">
                                            <label for="label" class="">Label<span class="error">*</span></label>
                                            <input type="text" name="label" id="label" value="" class="form-control">
                                        </div>
                                        <div class="position-relative form-group col-md-4">
                                            <label for="vendor_id" class="">Vendor<span class="error">*</span></label>
                                            <?php $vendorData = $commonFunctions->getData('vndr_pj',' '); ?>
                                            <select name="vendor_id" class="form-control">
                                                <option value="0">Select</option>
                                                <?php foreach ($vendorData as $rw) { ?>
                                                    <option value="<?php echo $rw['id'];?>" ><?php echo $rw['name'];?></option>
                                                <?php } ?>
                                            </select>
                                            <b><a href="vendor.php?state=form"> Create Vendor & Supplier</a></b>
                                        </div>  
                                        
                                        <div class="position-relative form-group col-md-4">
                                            <label for="status" class="">Status</label>
                                            <br><img src="../assets/images/statut4.png" alt="" title="Open" class="inline-block"> Open
                                        </div>
                                        <div class="position-relative form-group col-md-4">
                                            <label for="visibility" class="">Visibility</label>
                                            <select name="visibility" class="form-control">
                                                <option value="0">Project Contacts</option>
                                                <option value="1">Everybody</option>
                                            </select>
                                        </div>

                                        <div class="position-relative form-group col-md-4">
                                            <label for="start_date" class="">Start Date</label>
                                            <input type="date" name="start_date" id="start_date" value="" class="form-control" >
                                        </div>
                                        <div class="position-relative form-group col-md-4">
                                            <label for="end_date" class="">End Date</label>
                                            <input type="date" name="end_date" id="end_date" value="" class="form-control" >
                                        </div>

                                        <div class="position-relative form-group col-md-4">
                                            <label for="lead_status" class="">Lead Status</label>
                                            <select class="form-control" id="lead_status" name="lead_status">
                                                <option value="-1">&nbsp;</option>
                                                <option value="1" defaultpercent="0.00" elemcode="PROSP">Prospection</option>
                                                <option value="2" defaultpercent="20.00" elemcode="QUAL">Qualification</option>
                                                <option value="3" defaultpercent="40.00" elemcode="PROPO">Proposal</option>
                                                <option value="4" defaultpercent="60.00" elemcode="NEGO">Negociation</option>
                                                <option value="6" defaultpercent="100.00" elemcode="WON">Won</option>
                                                <option value="7" defaultpercent="0.00" elemcode="LOST">Lost</option>
                                            </select>
                                        </div>

                                        <div class="position-relative form-group col-md-4">
                                            <label for="lead_probability">Lead Probability </label>
                                            <input size="5" type="text" id="lead_probability" name="lead_probability" value="" class="form-control">
                                            <input type="hidden" id="lead_code" name="lead_code" value="" class="form-control">
                                        </div>
                                        
                                        <div class="position-relative form-group col-md-4">
                                            <label for="lead_amount">Lead Amount</label>
                                            <input size="5" type="text" id="lead_amount" name="lead_amount" value="" class="form-control">
                                        </div>
                                        <div class="position-relative form-group col-md-4">
                                            <label for="budget">Budget</label>
                                            <input size="5" type="text" id="budget" name="budget" value="" class="form-control">
                                        </div>
                                        
                                        <div class="position-relative form-group col-md-4">
                                            <label for="description" class="">Description</label>
                                            <textarea class="form-control" name="description"></textarea>
                                        </div>

                                        <div class="position-relative form-group col-md-4">
                                            <label for="category_id" class=""> Cust./Prosp. Tags / Categories </label>
                                            <select id="category_id" name="category_id[]" class="flat category_id form-control">
                                                <option value="0"></option>
                                                <?php $TagData = $commonFunctions->getData('vndr_tag',''); ?>
                                                <?php foreach ($TagData as $rw) { ?>
                                                    <option value="<?php echo $rw['id'];?>" ><?php 
                                                    if($rw['parent_id'] > 0){
                                                        echo $commonFunctions->getName($rw['parent_id'],'vndr_tag','label','id')." >> ";           
                                                    }
                                                    echo $rw['label']; ?></option>     
                                                <?php } ?>
                                            </select>
                                        </div>

                                        <div class="position-relative form-group col-md-12">
                                            <button type="submit" class="btn btn-success">Create</button>
                                        </div>
                                    </div>
                                </form>
                            <?php endif; ?>
                                
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div> <!-- end col -->
</div> <!-- end row -->

<?php require "../inc/footer.php"; ?>
<script type="text/javascript">
/*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' );
        } );
    } );
} );*/

$("#lead_status").change(function(){
    var defaultpercent = $('option:selected', this).attr("defaultpercent");
    var elemcode = $('option:selected', this).attr("elemcode");
    //alert(defaultpercent);
    $("#lead_probability").val(defaultpercent);
    $("#lead_code").val(elemcode);
});
</script>