<?php
    $aStatus      = true;
    $eStatus      = true;
    $tpID           = 'id';
    $tpTitle        = 'title';
    $pageTitle      = $rModule['title'];
    $titleFieldName = 'Product Title';


    $pLink=$pUrl;
    $units=$db->selectAll('unit','where isActive=1 order by title asc','id,title');
    $general->arrayIndexChange($units,'id');

    $use_product_production = $db->get_company_settings('use_product_production');
    $use_product_category = $db->get_company_settings('use_product_category');
    //$general->printArray($use_product_production);
    if($use_product_production==1){
        $types=$general->get_all_product_type();
    }

    $subCategorys = [];
    $categoryData = [];
    
?>
<script type=""> <?php echo 'var categoryData='.json_encode($categoryData).';';?></script>
<?php 
    if(isset($_GET['add'])){
        $data = [$pUrl=>$pageTitle,'1'=>'Add'];
        $general->pageHeader("Add $pageTitle",$data);

        if(isset($_POST['add'])){
            $pTitle     = $_POST[$tpTitle];
            $code     = $_POST['code'];
            $unID       = intval($_POST['unID']);
            $pSalePrice = floatval($_POST['sale_price']);
            
            
            if(empty($pTitle)){setMessage(36,$titleFieldName);$error=fl();}
            elseif(empty($code)){setMessage(36,'Code');$error=fl();}
            elseif(!array_key_exists($unID,$units)){$error=fl();setMessage(63,'Unit');}
            elseif($pSalePrice<=0&&$type==PRODUCT_TYPE_FINISHED){$error=fl();setMessage(63,'TP');}
            
            if(!isset($error)){
                $data = [
                    'title'         => $pTitle,
                    'code'          => $code,
                    'unit_id'       => $unID,
                    'sale_price'    => $pSalePrice
                ];
                $db->arrayUserInfoAdd($data);
                $db->transactionStart();
                $product_id=$db->insert('products',$data,true);
                if($product_id==false){
                    $error=fl();setMessage(66);
                }
                $product_price_log = $db->product_price_log($product_id,['sale_price'=>$data['sale_price'],'unit_cost'=>0]);
                if($product_price_log==false){$error=fl();setMessage(66);}
                $ac=false;
                if(!isset($error)){
                    $ac=true;
                }
                $db->transactionStop($ac);
                if(!isset($error)){
                    $general->redirect($pUrl,29,$pageTitle);
                }
            }
        }
    ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="white-box border-box">
                <div class="row">
                    <div class="col-lg-12"><?php show_msg();?></div>
                    <div class="col-xs-12 col-sm-12 col-md-12">
                        <form method="post" action="">
                            <div class="col-xs-6 col-sm-4 col-md-4">
                                <?php
                                    $general->inputBoxText($tpTitle,$titleFieldName,@$_POST[$tpTitle],'y');
                                    $general->inputBoxText('code','Code',@$_POST['code'],'y');
                                    $general->inputBoxSelect($units,'Unit','unID','id','title',@$_POST['unID']);
                                    $general->inputBoxText('sale_price','TP',@$_POST['sale_price']);
                                ?>
                                
                                
                                <div class="form-group m-b-0">
                                    <div class="pull-right">
                                        <input type="submit" name="add" value="Add" class="btn btn-lg btn-info waves-effect waves-light">
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    }
    elseif(isset($_GET['edit'])){
        $edit = intval($_GET['edit']);
        $u = $db->get_rowData('products',$tpID,$edit);
        $general->arrayContentShow($u);
        $general->arrayIndexChange($categorys,'id');
        if(empty($u)){$general->redirect($pUrl,array(37,$pageTitle));}
        $product_data = $general->getJsonFromString($u['data']);
        $get_one_free = intval(@$product_data['get_one_free']);
        if($use_product_category==1){
            $category= $categorys[$u['category_id']]['parent'];
        }
        $statuss=[
            ['id'=>1,'title'=>'Active'],
            ['id'=>0,'title'=>'In Active'],
            ['id'=>3,'title'=>'Archive']
        ];
        if(isset($_POST['edit'])){
            $pTitle     = $_POST[$tpTitle];
            $code     = $_POST['code'];
            $unID       = intval($_POST['unID']);

            $pSalePrice = floatval($_POST['sale_price']);
            
            $type = intval($_POST['type']);
            $status = intval($_POST['status']);
            
            if(empty($pTitle)){setMessage(36,$titleFieldName);$error=fl();}
            elseif(empty($code)){setMessage(36,'Code');$error=fl();}
            elseif(!array_key_exists($unID,$units)){$error=fl();setMessage(63,'Unit');}
            elseif($pSalePrice<=0&&$type==0){$error=fl();setMessage(63,'TP');}
            if(!isset($error)){
                $data = [
                    'title'         => $pTitle,
                    'code'          => $code,
                    'unit_id'       => $unID,
                    'sale_price'    => $pSalePrice,
                    'isActive'      => $status
                ];
                $data['type']=$type;
                $where=[$tpID=>$edit];
                $db->arrayUserInfoEdit($data);
                $db->transactionStart();
                $product_id=$db->update('products',$data,$where);
                if($product_id==false){
                    $error=fl();setMessage(66);  

                }
                if($u['sale_price']!=$data['sale_price']){
                    $product_price_log = $db->product_price_log($edit,['sale_price'=>$data['sale_price'],'unit_cost'=>$u['unit_cost']]);
                    if($product_price_log==false){$error=fl();setMessage(66);}
                }
                if(!isset($error)){
                    $ac=true;
                }
                else{
                    $ac=false;
                }
                $db->transactionStop($ac);
                if(!isset($error)){
                    $general->redirect($pUrl,29,$pageTitle);
                }
            }
        }
        $box_unit_quantity=0;
        if(isset($product_data['box_unit_quantity'])){
            $box_unit_quantity=intval($product_data['box_unit_quantity']);
        }
        $data = array($pUrl=>$pageTitle,'javascript:void()'=>$u[$tpTitle],'1'=>'Edit');
        $general->pageHeader('Edit '.$rModule['title'],$data);
    ?>
    <div class="row">
        <div class="col-lg-12">
            <div class="white-box border-box">
                <div class="row">
                    <div class="col-lg-12"><?php show_msg();?></div>
                    <div class="col-xs-12 col-sm-12 col-md-12">
                        <form method="post" action="">
                            <div class="col-xs-6 col-sm-4 col-md-4">
                                <?php $general->inputBoxText($tpTitle,$titleFieldName,$u[$tpTitle]);?>
                                <?php $general->inputBoxText('code','Code',@$u['code'],'y');?>
                                <?php $general->inputBoxSelect($units,'Unit','unID','id','title',$u['unit_id']);?>
                                <?php $general->inputBoxText('sale_price','TP',$u['sale_price']);?>
                                <?php $general->inputBoxSelect($statuss,'Status','status','id','title',$u['isActive'],haveSelect:'n');  ?>
                            </div>


                            <div class="clearfix visible-xs"></div>
                            <div class="col-xs-6 col-sm-4 col-md-4">

                                <div class="form-group m-b-0">
                                    <div class="pull-right">
                                        <input type="submit" name="edit" value="Edit" class="btn btn-info waves-effect waves-light">
                                    </div>
                                </div>

                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    }
    else if(isset($_GET['archive'])){
        $data = array($pUrl=>$pageTitle,'1'=>'Archive');
        $general->pageHeader('Archive '.$rModule['title'],$data);
    ?>
    <div class="row">
        <div class="col-sm-12">
            <div class="white-box">
                <form method="get">
                    <input type="hidden" name="mdl" value="products">
                    <?php
                        show_msg();
                        $q=['isActive=3'];
                        if($use_product_production==1){
                            $general->inputBoxSelectForReport($types,'Type','type','id','title',@$_GET['type']); 
                        ?> 
                        <div class="col-md-2">
                            <h5 class="box-title">Search</h5>
                            <input class="btn btn-success" type="submit" value="Search"/>

                        </div>

                        <?php 
                            if(isset($_GET['type']) && $_GET['type']>-1){
                                $q[]='type='.intval($_GET['type']);
                            }
                        }
                        $products=$db->selectAll('products','where '.implode(' and ',$q));

                    ?>
                </form>
                <div class="col-md-12" id="reportArea">
                    <table class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>

                                <th>SN</th>
                                <th>ID</th>
                                <th>Title</th>
                                <th>Stock</th>
                                <th>Unit</th>
                                <th>Type</th>
                                <th class="amount_td">TP</th>
                                <th>Edit</th>
                            </tr>
                        </thead> 
                        <tbody>
                            <?php
                                $total=1;
                                foreach($products as $u){
                                ?>
                                <tr>

                                    <td><?=$total++?></td>
                                    <td ><?=$u['code']?></td>
                                    <td ><?=$u[$tpTitle]?></td>
                                    <td class="amount_td"><?=(float)$u['stock']?></td>
                                    <td ><?=$units[$u['unit_id']]['title']?></td>  
                                    <td><?=$types[$u['type']]['title']?></td>
                                    <td class="amount_td"><?php echo $general->numberFormat($u['sale_price']);?></td>
                                    <td><a href="<?=$pUrl?>&edit=<?=$u[$tpID]?>" class="btn btn-info">Edit</a>
                                    </td>
                                </tr>
                                <?php
                                }
                            ?>
                        </tbody>

                    </table>
                </div>
            </div>
        </div>
    </div>
    <?php 
    }
    else{
        $general->arrayIndexChange($categorys,'id');
        $data = array($pUrl=>$pageTitle);
        //$sortLink='<a style="font-size: 20px; color: #228AE6;margin-left: 20px;" href="'.$pUrl.'&sort=1"><i class="fa fa-arrows-v"></i></a>';
        $archived = '<a href="' . $pUrl . '&archive" class="btn btn-info">Archive</a>';
        $general->pageHeader($rModule['title'],$data,$general->addBtnHtml($pUrl).$archived);

    ?>
    <div class="row">
        <div class="col-sm-12">
            <div class="white-box">
                <form method="get">
                    <input type="hidden" name="mdl" value="products">
                    <?php
                        show_msg();
                        $q=['isActive in(1,0)'];
                        if($use_product_production==1){
                            $general->inputBoxSelectForReport($types,'Type','type','id','title',@$_GET['type']); 
                        ?> 
                        <div class="col-md-2">
                            <h5 class="box-title">Search</h5>
                            <input class="btn btn-success" type="submit" value="Search"/>

                        </div>

                        <?php 
                            if(isset($_GET['type']) && $_GET['type']>-1){
                                $q[]='type='.intval($_GET['type']);
                            }
                        }
                        $products=$db->selectAll('products','where '.implode(' and ',$q));
                        $total=1;

                    ?>
                </form>
                <div class="col-md-12" >
                    <table class="table table-striped table-bordered table-hover" id="reportArea">
                        <thead>
                            <tr>

                                <th>SN</th>
                                <th>ID</th>
                                <th>Title</th>
                                <th>Stock</th>
                                <th>Unit</th>
                                <th>Unit cost</th>
                                <th class="amount_td">TP</th>
                                <th>Edit</th>
                            </tr>
                        </thead> 
                        <tbody>
                            <?php
                                foreach($products as $u){
                                ?>
                                <tr>
                                    <td><?=$total++?></td>
                                    <td ><?=$u['code']?></td>
                                    <td ><?=$u[$tpTitle]?></td>
                                    <td class="amount_td"><?=(float)$u['stock']?></td>
                                    <td ><?=$units[$u['unit_id']]['title']?></td> 
                                    <td><?=$u['unit_cost']?></td>
                                    <td class="amount_td"><?php echo $general->numberFormat($u['sale_price']);?></td>
                                    <td><a href="<?=$pUrl?>&edit=<?=$u[$tpID]?>" class="btn btn-info">Edit</a>
                                    </td>
                                </tr>
                                <?php
                                }
                            ?>
                        </tbody>

                    </table>
                    <script type="">
                    $(document).ready(function () {
                            $('#reportArea thead th').each(function () {
                                var title = $(this).text();
                            });
                            var table = $('#reportArea').DataTable({
                                "columnDefs": [
                                    {
                                        className: "amount_td",
                                        targets: [4]
                                    }
                                ],
                                paging: false,
                                initComplete: function (){
                                    this.api()
                                    .columns()
                                    .every(function () {
                                        var that = this;
                                        $('input', this.header()).on('keyup change clear', function () {
                                            if (that.search() !== this.value) {
                                                that.search(this.value).draw();
                                            }
                                        });
                                    });
                                },
                            });
                        });
                
                    </script>
                </div>
            </div>
        </div>
    </div>
    <?php
        $general->onclickChangeJavaScript('products',$tpID);
    }



?>

