<?php
require_once(PLUGIN_DIR . 'Dao/CommissionDao.php');
class CommissionManage{
    public static function commissionManage_html(){
        global $wpdb;
        $context = array();
        $type = $_POST["type"];
        if($type == 4){
            $commissionParams = array(
                "intent" => $_POST["intent"],
                "accounts" => $_POST["accounts"],
                "type" => $_POST["businessType"],
                "contractId" => 1
            );
            $wpdb->query("START TRANSACTION");
            if(isset($_POST['contractId'])){
                $res = CommissionManage::commission_update($_POST['contractId']);
                if(is_numeric($res)){
                    $wpdb->query("COMMIT");
                    echo "收佣管理修改成功";
                }else{
                    $wpdb->query("ROLLBACK");
                    print_r($res);
                    echo "收佣管理修改失败";
                }
            }else{
                $res = CommissionManage::commission_insert($commissionParams);
                if(is_numeric($res)){
                    $wpdb->query("COMMIT");
                    echo "收佣管理录入成功";
                }else{
                    $wpdb->query("ROLLBACK");
                    print_r($res);
                    echo "收佣管理录入失败";
                }
            }

            exit;
        }else if(isset($_GET["id"])){
            $context["id"] = $_GET["id"];
            $context["consultant"] = CommissionDao::search_commission_consultant($_GET["id"]);
            $context["commission"] = CommissionDao::search_tospur_commission($_GET["id"]);
            $context["commissionLog"] = CommissionDao::search_commission_log($context["commission"]->id);
        }
        $context["city"] = SearchDao::searchCity();
        Timber::render("commissionManage.html",$context);
    }

    public static function commission_insert($comParams){
        $data = $_POST["data"];
        $log = $_POST["log"];
        $comParams["consultantId"] = $data["recConsultant"];
        $comRes = CommissionDao::insert_touspur_commission($comParams);
        if(is_numeric($comRes)){
            if(isset( $_POST["log"])) {
                foreach ($log as $value) {
                    $logParams = array(
                        "commissionId" => $comRes,
                        "time" => $value["time"],
                        "paid" => $value["paid"]
                    );
                    $logRes =  CommissionDao::insert_commission_log($logParams);
                }
                return $logRes;
            }
        }else{
            return $comRes;
        }
    }

    public static function commission_update($contractId){
        global $wpdb;
        $data = $_POST["data"];
        $log = $_POST["log"];
        $updateParam = array(
            "consultanId" => $data["recConsultant"],
            "type" => $_POST["businessType"]
        );
        $updateRes = $wpdb->update(Config::TOSPUR_COMMISSION,$updateParam,array("contractId" => $contractId));
        $commission  = CommissionDao::search_tospur_commission($contractId);
        if(is_numeric($updateRes)){
            if(isset( $_POST["log"])) {
                foreach ($log as $value) {
                    $logParams = array(
                        "commissionId" => $commission->id,
                        "time" => $value["time"],
                        "paid" => $value["paid"]
                    );
                    $logRes =  CommissionDao::insert_commission_log($logParams);
                }
                return $logRes;
            }
        }else{
            $updateRes = $wpdb->last_error;
            return $updateRes;
        }
    }
}