<?php
require_once(PLUGIN_DIR . 'Dao/ContractDao.php');
require_once(PLUGIN_DIR . 'Dao/SearchDao.php');
require_once(PLUGIN_DIR . 'Dao/HouseDao.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(
            'signedDate' =>$_REQUEST['signedDate'],
            'status' =>$_REQUEST['status'],
            'area' =>$_REQUEST['area'],
            'permitNumber' =>$_REQUEST['permitNumber'],
            'managerId' =>$_REQUEST['managerId'],
            'paymentMethod' =>$_REQUEST['paymentMethod'],
            'codicil' =>$_REQUEST['codicil'],
            'oName' =>$_REQUEST['oName'],
            'oID' =>$_REQUEST['oID'],
            'oBroker' =>$_REQUEST['oBroker'],
            'oAddress' =>$_REQUEST['oAddress'],
            'oMoney' =>$_REQUEST['oMoney'],
            'oPayBack' =>$_REQUEST['oPayBack'],
            'cName' =>$_REQUEST['cName'],
            'cID' =>$_REQUEST['cID'],
            'cBroker' =>$_REQUEST['cBroker'],
            'cAddress' =>$_REQUEST['cAddress'],
            'cMoney' =>$_REQUEST['cMoney'],
            'cPayBack' =>$_REQUEST['cPayBack']
        );

        $wpdb->query("START TRANSACTION");
        if($type==1){
            $prefix = "新增";
            if(isset($_REQUEST["id"])){
                $result = ContractDao::update($_REQUEST["id"],$params);
                $prefix = "修改";
                if(is_numeric($result)){
                    $status = 0;
                    if($params['status'] == 4) {
                        $status = 0;
                    }else if($params['status'] == 3){
                        $status = 2;
                    }
                    $result = HouseDao::updateStatus($status,$_REQUEST['id']);
                }
            }else{
                $params['business'] = $_REQUEST['businessId'];
                $params['houseNumber'] = $_REQUEST['houseNumber'];
                $params['price'] = $_REQUEST['price'];
                $params['houseId'] = $_REQUEST['houseId'];
                $params['customerNumber'] = $_REQUEST['customerNumber'];
                $params['oPhone'] = $_REQUEST['oPhone'];
                $params['oCommission'] = $_REQUEST['oCommission'];
                $params['cPhone'] = $_REQUEST['cPhone'];
                $params['cCommission'] = $_REQUEST['cCommission'];
                $params['consultantId'] = get_current_user_id();

                $result = ContractDao::insert($params);
                $pre = "CJCS";
                if($_REQUEST['type'] > 1){
                    $pre = "CJCZ";
                }
                $contractId = $pre.str_pad($result,6,'0',STR_PAD_LEFT);
                $result = ContractDao::setContractId($result,$contractId);
                //二手房、租房创建合同时修改房源的状态
                if(intval($_REQUEST['businessId']) >=1 ){
                    $result = HouseDao::updateStatus(-1,$params['houseId']);
                }
            }
            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);
    }
} 