<?php
require_once(PLUGIN_DIR . 'Dao/ContractDao.php');
require_once(PLUGIN_DIR . 'Dao/SearchDao.php');

class Contract {
    public static function init_view(){
        global $wpdb;
        $type = $_POST["type"];
        $context = array();
        $context['siteUrl'] = get_site_url();
        $context['adminUrl'] = admin_url();
        $context['city'] = SearchDao::searchCity();
        $context['managers'] = get_users( 'role=editor' );
        $context['status'] = SearchDao::searchStatusType(4);
        $context['buildProperty'] = SearchDao::searchBuildProperty();
        $context['room'] = SearchDao::searchRoom();

        $params = array(
            'business' => $_REQUEST['business'],
            'houseNumber' =>$_REQUEST['houseNumber'],
            'address' =>$_REQUEST['address'],
            'customerNumber' =>$_REQUEST['customerNumber'],
            'signedDate' =>$_REQUEST['signedDate'],
            'permitNumber' =>$_REQUEST['permitNumber'],
            'area' =>$_REQUEST['area'],
            'price' =>$_REQUEST['price'],
            'managerId' =>1,
            'paymentMethod' =>$_REQUEST['paymentMethod'],
            'codicil' =>$_REQUEST['codicil'],
            'oName' =>$_REQUEST['oName'],
            'oPhone' =>$_REQUEST['oPhone'],
            'oID' =>$_REQUEST['oID'],
            'oCommission' =>$_REQUEST['oCommission'],
            'oAddress' =>$_REQUEST['oAddress'],
            'oPayBack' =>$_REQUEST['oPayBack'],
            'oMoney' =>$_REQUEST['oMoney'],
            'cName' =>$_REQUEST['cName'],
            'cPhone' =>$_REQUEST['cPhone'],
            'cID' =>$_REQUEST['cID'],
            'cCommission' =>$_REQUEST['cCommission'],
            'cAddress' =>$_REQUEST['cAddress'],
            'cPayBack' =>$_REQUEST['cPayBack'],
            'cMoney' =>$_REQUEST['cMoney']
        );

        $wpdb->query("START TRANSACTION");
        if($type==1){
            $prefix = "新增";
            if(isset($_REQUEST["id"])){
                $result = ContractDao::update($_REQUEST["id"],$_REQUEST['']);
                $prefix = "修改";
            }else{
                $result = ContractDao::insert($params);
                if(is_numeric($result)){
                    $prefix = "CJCS";
                    if($_REQUEST['type'] > 1){
                        $prefix = "CJCZ";
                    }
                    $contractId = $prefix.str_pad($result,6,'0',STR_PAD_LEFT);
                    $result = ContractDao::setContractId($result,$contractId);
                }
            }
            if(!is_numeric($result)){
                $wpdb->query("ROLLBACK");
                $context["error"] = $result;
                echo $prefix."合同失败";
            }else{
                $wpdb->query("COMMIT");
                echo $prefix."合同成功";
            }
            exit;
        }else if(isset($_GET['edit'])){
            $id = $_REQUEST["id"];
            $context['houseId'] = $id;
            $context['result'] = ContractDao::searchContract($id);
        }
        Timber::render("contract.html",$context);
    }
} 