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

class customer extends Tospur_House
{
    //二手房信息
    public static function customer_html()
    {
        global $wpdb;
        $context = array();
        $context['siteUrl'] = get_site_url();
        $insert_tospur_house_array = array(
            //customerInfo（4）
            'name' => $_POST['name'],
            'customer_type' => $_POST['customer_type'],
            'phone' => $_POST['phone'],
            'customer_remark' => $_POST['customer_remark'],
            //customerDemand（14）
            'new_house' => $_POST['new_house'],
            'secondHand_house' => $_POST['secondHand_house'],
            'rent_house' => $_POST['rent_house'],
            'character' => $_POST['character'],
            'origin' => $_POST['origin'],
            'payment_mode' => $_POST['payment_mode'],
            'min_totalPrice' => $_POST['min_totalPrice'],
            'max_totalPrice' => $_POST['max_totalPrice'],
            'min_unitPrice' => $_POST['min_unitPrice'],
            'max_unitPrice' => $_POST['max_unitPrice'],
            'min_rent' => $_POST['min_rent'],
            'max_rent' => $_POST['max_rent'],
            'rent' => $_POST['rent'],
            'tenancy' => $_POST['tenancy'],
            //customerDetail（name 19）
            'city_id' => $_POST['baseCity'],
            'district_id' => $_POST['baseAreaId'],
            'plate_id' => $_POST['basePlateId'],
            'community_name' => $_POST['community_name'],
            'room_id' => $_POST['baseRoom'],
            'check_in_time' => $_POST['check_in_time'],
            'min_coveredArea' => $_POST['min_coveredArea'],
            'max_coveredArea' => $_POST['max_coveredArea'],
            'min_floor' => $_POST['min_floor'],
            'max_floor' => $_POST['max_floor'],
            'propertyRight' => $_POST['propertyRight'],
            'decoration' => $_POST['decoration'],
            'age' => $_POST['age'],
            'faceto' => $_POST['faceto'],
            'bedroom' => $_POST['bedroom'],
            'hall' => $_POST['hall'],
            'bathroom' => $_POST['bathroom'],
            'origin_remark' => $_POST['origin_remark'],
            'consultant_id' => $_POST['consultant_id'],
            //（1）
            'status' => $_POST['status']
        );
        if ($_POST) {
            $wpdb->query("START TRANSACTION");
            if (isset($_POST['houseId'])) {
                $result = $wpdb->update(Config::TOSPUR_CUSTOMER_TABLE,
                    $insert_tospur_house_array,
                    array('id' => $_POST['houseId'])
                );
                if ($result) {
                    $wpdb->query("COMMIT");
                    echo "修改客源成功";
                } else {
                    $wpdb->query("ROLLBACK");
                    echo "修改客源失败";
                }
            } else {
                $id = CustomerDao::insert_customer($insert_tospur_house_array);
                if ($id) {
                    $wpdb->query("COMMIT");
                    echo "新增客源成功";
                } else {
                    $wpdb->query("ROLLBACK");
                    echo "新增客源失败";
                }
            }
            exit;
        } else if (isset($_GET['edit'])) {
            $context['houseId'] = $_GET['id'];
            $context = array_merge($context, CustomerDao::searchCustomer($_GET['id']));
            $context["district"] = SearchDao::searchCity($context['result']->city_id);
            $context["plate"] = SearchDao::searchCity($context['result']->city_id, $context['result']->district_id);
        }

        $current_user = wp_get_current_user();
        $context['role'] = $current_user->roles[0];
        if ($context['role'] == 'author') {
            $context['consultant_id'] = $current_user->ID;
        }
        $context['city'] = SearchDao::searchCity();
        $context['buildProperty'] = SearchDao::searchBuildProperty();
        $context['room'] = SearchDao::searchRoom();
        $context['status'] = searchDao::searchStatusType(5);
        $context['customer_type'] = searchDao::searchStatusType(6);
        for ($i = 1970; $i <= 2020; $i++) {
            $context['age'][] = $i;
        }
        Timber::render('customer.html', $context);
    }
}

