<?php
defined('BASEPATH') OR exit('No direct script access allowed');

GLOBAL $__HAINA_VEI__;

class Index extends CI_Controller {

    public function __construct(){
        parent::__construct();
        mb_regex_encoding("UTF-8");
        bcscale(4);
        error_reporting(0);
        $GLOBALS['__HAINA_VEI__'] = 30561;
        $this->load->helper('array');
        $this->load->library('vendor');
        $this->load->model('UserAuth_model');
        $this->load->model('Group_model');
        $this->load->model('BIZ_Orders_model');
    }

    /*!
     * TODO
     * * 请求的openID和订单的VEI_SN不相等
     * * 使用供应商平台登陆账号获取接口的key
     */

    public function index($GRI_SN=0, $vendor=null, $set_pvt=0)
    {
        $auto_vendor_str = $vendor===null ? implode(",", $this->vendor->auto_vendor) : $vendor;
        return $this->push($GRI_SN, $auto_vendor_str, $set_pvt);
    }

    public function push($GRI_SN=0, $vendor_str=null, $set_pvt=0)
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d 23:59:59', strtotime("+2 months"));
        $ready_to_send = $this->Group_model->get_plan_not_received(1, $GRI_SN, $vendor_str, $start_date, $end_date);
        if (empty($ready_to_send)) {
            return $this->output->set_output("empty");
        }
        $order = $ready_to_send[0];
        if (isset($this->vendor->vendor_fun[strval($order->VAS_VEI_SN)])) {
            $vendor_class = $this->vendor->vendor_fun[strval($order->VAS_VEI_SN)];
            $this->load->library($vendor_class);
            $vendor_class_name = mb_strtolower($vendor_class);
            $call_fun = $this->$vendor_class_name->order_push($order, $set_pvt);
            log_message('error',"Call [$vendor_class>order_push] " . $order->GRI_SN . ". " . $call_fun);
            return $this->output->set_output($call_fun . ". " . $order->GRI_SN);
        }
        return $this->output->set_output("Not found vendor function. " . $order->GRI_SN);
    }

    public function verify_user()
    {
        return false;
        $open_id = $this->input->post('openId');
        $open_key = $this->input->post('key');
        $match = $this->UserAuth_model->if_user_key($open_id, $open_key, 1);
        return $match;
    }

    /**
     * 接收地接社的信息上报
     * * 信息类型代码:
     * * * 1000 已接收变更,未确认
     * * * 2000 已确认计划/变更
     * * * 3001 导游信息变更
     * * * 4001 已确认取消
     * * * 4002 订单已删除
     * * 计划发送/更新状态
     * * * 未确认:
     * * *   10 已接收计划
     * * *   11 已接收变更
     * * * 上报信息:
     * * *   20 已上报确认
     * * *   31 已录入上报导游
     * * *   32 未录入上报导游
     * @date   2018-11-14
     */
    public function uploadOperation()
    {
        $input = $this->input->post();
        log_message('error',"Call [Tulanduo>uploadOperation]: " . json_encode($input));
        $ret['status'] = 1;
        $ret['errMsg'] = "";
        return $this->output->set_content_type('application/json')->set_output(json_encode($ret));
        ;
        $ret['status'] = -1;
        $ret['errMsg'] = "未知错误";
        $user_verify = $this->verify_user();
        if ($user_verify !== TRUE) {
            $ret['errMsg'] = '用户验证失败';
            return $this->output->set_content_type('application/json')->set_output(json_encode($ret));
        }
        $vendorID = $input['openId'];
        $vps = $this->Group_model->get_sync_info_by_vendororder($input['orderId']);
        if (empty($vps)) {
            $ret['errMsg'] = "未找到相应的订单.";
            return $this->output->set_content_type('application/json')->set_output(json_encode($ret));
        }
        $vendor_order_id = $input['orderId'];
        $action_code = $input['operationTypeCode'];
        switch ($action_code) {
            case '1000':
                # code...
                break;
            case '2000':
                $ret = $this->plan_confirm($input, $vps);
                break;
            case '3001':
                $ret = $this->fill_tourguide($input, $vps);
                break;
            case '4001':
            case '4002':
                $ret['status'] = 1;
                $ret['errMsg'] = "";
                log_message('error','上报确认取消. ' . json_encode($input));
                break;

            default:
                # code...
                break;
        }
        return $this->output->set_content_type('application/json')->set_output(json_encode($ret));
    }

    public function plan_confirm($input, $vps)
    {
        return false;
        $ret['status'] = -1;
        $ret['errMsg'] = "";
        $vendor_manager = $this->Group_model->get_vendorContact($input['openId']);
        /** VendorArrangeState */
        $VAS_ConfirmInfo = $input['operationRemark'] . "\r\n======确认人: " . $input['operator'] . ", 确认时间: " . $input['operateTime'];
        $VAS_ConfirmInfo .= "\r\n" . $vps->VAS_ConfirmInfo;
        $update_vas = $this->Group_model->set_plan_confirm($vps->VAS_SN, $vendor_manager->LMI_SN, $VAS_ConfirmInfo);
        if ($update_vas === TRUE) {
            $ret['status'] = 1;
            $ret['errMsg'] = "";
            $sync_arr = array(
                "VPS_externalorderState" => 20
                ,"VPS_updateTime" => date('Y-m-d H:i:s')
            );
            /** VendorPlanSync */
            $this->Group_model->update_VendorPlanSync($vps->VPS_SN, $sync_arr);
            // 邮件外联
            $sender_name = "中华游供应商合作平台";
            $sender_mail = "info@chinahighlights.net";
            $from_name   = $vendor_manager->LMI2_Name;
            $from_mail   = $vendor_manager->LMI_ListMail;
            $to_name     = $vps->OPI_Name;
            $to_mail     = $vps->OPI_Email;
            $subject     = $vps->GRI_Name . "团已确认: " . $vendor_manager->VEI2_CompanyBN;
            $mail_body   = $vendor_manager->VEI2_CompanyBN . "对团" . $vps->GRI_Name . "的计划在" . $input['operateTime'] . "已确认。<br>";
            $mail_body  .= "确认说明：" . $input['operationRemark'] . "<br>";
            $mail_body  .= "确认人：$vendor_manager->LMI2_Name $vendor_manager->LMI_ListMail 固定电话: $vendor_manager->LMI_Telephone 移动电话:$vendor_manager->LMI_Mobile<br>";
            $mail_body  .= "变更内容: " . $vps->VAS_ChangeText . "<br>";
            $this->Group_model->save_automail($sender_name, $sender_mail, $from_name, $from_mail, $to_name, $to_mail, $subject, $mail_body, $sender_name);
        } else {
            log_message('error','图兰朵确认上报更新状态失败. POST RAW: ' . raw_json_encode($input) );
        }
        return $ret;
    }

    public function fill_tourguide($input, $vps)
    {
        return false;
        $ret['status'] = -1;
        $ret['errMsg'] = "";
        $eva = $this->Group_model->get_plan_eva($vps->VAS_SN);
        if (empty($eva)) {
            $ret['errMsg'] = "未找到相应的订单.";
            return $this->output->set_content_type('application/json')->set_output(json_encode($ret));
        }
        if (intval($eva[0]->need_tourguide)===0) {
            // 不需要设置导游
            $ret['status'] = 1;
            $ret['errMsg'] = "";
            return $ret;
        }
        // 需要填写导游, 则根据不同的供应商调用相应的方法
        if (isset($this->vendor->vendor_fun[strval($input['openId'])])) {
            $vendor_class = $this->vendor->vendor_fun[strval($input['openId'])];
            $this->load->library($vendor_class);
            $vendor_class_name = mb_strtolower($vendor_class);
            $ret = $this->$vendor_class_name->tourguide_update($input, $vps, $eva);

            $err_code = $ret['err'];
            unset($ret['err']); // 不返回给接收方
            $sync_ret = array(
               "VPS_updateTime" => date('Y-m-d H:i:s')
            );
            if ($err_code === 0) {
                // 录入成功
                $sync_ret['VPS_externalorderState'] = 31;
            } else {
                $sync_ret['VPS_externalorderState'] = 32;
            }
            $this->Group_model->update_VendorPlanSync($vps->VPS_SN, $sync_ret);
        }
        return $ret;
    }

    public function calc_key($userId, $key)
    {
        return false;
        $default = "b825e39422a54875a95752fc7ed6f5d2";
        $ret = md5(hash("sha256", $userId.$default));
        return $ret;
    }

}

/* End of file index.php */
/* Location: ./third_party/vendorPlanSync/controllers/index.php */
