<?php
// require_once('basic-function.php');
class package extends basicFunction 
{
    use table_names;

    public function create($name,$investment,$min,$max,$total_percent,$no_of_days)
    {
        $vals = [$name,$investment,$min,$max,$total_percent,$no_of_days];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        $name_exist = $this->selectWhereDlt($this->package_tb,'name',$name);
        if(count($name_exist) > 0){
            return $this->errorMsgs(16);
        }

        if($max <= $min){
            return ['error'=>1,'msg'=>'Error! Max value is less than Min Value!.'];
        }

        $daily_percentage = ($total_percent + 100) / $no_of_days;
        
        // 365 days in a year
        // 7 days a week
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->package_tb VALUES (null,
        '".mysqli_real_escape_string($this->connectDb(),$unique_id)."',
        '".mysqli_real_escape_string($this->connectDb(),$investment)."',
        '".mysqli_real_escape_string($this->connectDb(),$name)."',
        '".mysqli_real_escape_string($this->connectDb(),$min)."',
        '".mysqli_real_escape_string($this->connectDb(),$max)."',
        '".mysqli_real_escape_string($this->connectDb(),$total_percent)."',
        '".mysqli_real_escape_string($this->connectDb(),$daily_percentage)."',
        '".mysqli_real_escape_string($this->connectDb(),$no_of_days)."',
        '".mysqli_real_escape_string($this->connectDb(),0)."',
        '".mysqli_real_escape_string($this->connectDb(),$date_created)."',
        '".mysqli_real_escape_string($this->connectDb(),$date_created)."') ";
        mysqli_close($this->connectDb());
        if($this->sqlQuery($query)){
            return $this->successMsg('Package');
        }else{
            return $this->errorMsgs(14);
        }

    }
    
    public function edit($id,$name,$investment,$min,$max,$total_percent,$no_of_days)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $vals = [$name,$name,$investment,$min,$max,$total_percent,$no_of_days];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        $name_exist = $this->selectWhereDlt($this->package_tb,'name',$name);
        if(count($name_exist) > 0 && $name_exist[0]['unique_id'] !== $id){
            return $this->errorMsgs(16);
        }
        
        if($max <= $min){
            return ['error'=>1,'msg'=>'Error! Max value is less than Min Value!.'];
        }

        $daily_percentage = ($total_percent + 100) / $no_of_days;

        $fields = ['name','investment_id','min_amt','max_amt','total_percentage','daily_percentage','no_of_days'];
        $values = [$name,$investment,$min,$max,$total_percent,$daily_percentage,$no_of_days];

        $update_package = $this->update_where( $this->package_tb,$fields,$values,'unique_id',$id);
        if($update_package['error'] == 1){
            return $update_package;
        }
        return $this->successMsg2('Package Edit');
    }
    public function delete($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_deleted'];
        $values = ['1'];
        $package_update = $this->update_tbs( $this->package_tb,$fields,$values,$id);
        if($package_update['error'] == 1){
            return $package_update;
        }
        return $this->dltSuccessMsg();
    }
    
}
$package = new package();