<?php namespace Collect\Api;
use Dever;
use Collect\Lib\Api;
class Notify extends Api
{
    public function common()
    {
        $input = Dever::input();
        if (!isset($input['s'])) {
            $this->error('error');
        }
        $s = \Dever\Helper\Str::decode($input['s']);
        if ($s) {
            list($api_id, $api_service_id, $order_id) = explode('|', $s);
        } elseif (isset($input['order_num'])) {
            $order_id = Dever::db('order', 'seller')->find(array('order_num' => $input['order_num']));
            if (!$order_id) {
                $this->error('error');
            }
            $channel = Dever::db('info', 'channel')->find($order_id['channel_id']);
            if (!$channel) {
                $this->error('order error');
            }
            $connect_id = $channel['connect_id'];
            $api = Dever::db('api', 'connect')->find(array('connect_id' => $connect_id, 'type' => 1));
            if (!$api) {
                $this->error('order error');
            }
            $api_id = $api['id'];

        } else {
            $this->error('error');
        }
        
        unset($input['s']);
        unset($input['l']);
        if (!$input) {
            $input = file_get_contents("php://input");
            if ($input) {
                $input = json_decode($input, true);
                if (is_array($input)) {
                    if (isset($input['s'])) {
                        unset($input['s']);
                    }
                    if (isset($input['l'])) {
                        unset($input['l']);
                    }
                } else {
                    $this->error('error');
                }
            }
        }
        if (!$input) {
            $this->error('error');
        }
        Dever::log($input, 'notify');
        $this->load($input, $connect_id, $api_id, $order_id);
    }

    private function load($input, $connect_id, $api_id, $order_id)
    {
        $this->connect = Dever::db('info', 'connect')->find($connect_id);
        $this->info = Dever::db('api', 'connect')->find($api_id);
        if (is_array($order_id)) {
            $order = $order_id;
        } else {
            $order = Dever::db('order', 'seller')->find($order_id);
        }
        if (!$order || $order['status'] >= 10) {
            $this->error('order error');
        }
        $channel = Dever::db('info', 'channel')->find($order['channel_id']);
        if (!$channel) {
            $this->error('channel error');
        }
        $this->connect['appsecret'] = $channel['appsecret'];

        $code = Dever::db('api_notify_code', 'connect')->select(array('api_id' => $api_id));

        $status = 0;
        if ($code) {
            foreach ($code as $k => $v) {
                if (isset($input[$v['key']]) && $input[$v['key']] == $v['value']) {
                    $status = $v['type'];
                }
            }
        }
        $data = $input;
        $data['status'] = $status;
        $data = $this->service($data);

        $msg = '';
        if ($status == 1) {
            $msg = 'ok';
        } elseif ($status == 2) {
            $msg = 'error';
        }
        if ($this->info['notify_sign_col']) {
            $this->info['sign_col'] = $this->info['notify_sign_col'];
        }
        if ($sign = Dever::issets($input, $this->connect['sign_name'])) {
            unset($input[$this->connect['sign_name']]);
            unset($input['l']);
            if ($sign != $this->sign($input)) {
                //$this->error('sign error');
            }
        }
        if ($msg) {
            $update = array();
            $update['official_msg'] = $data['official_msg'] ?? '';
            $update['official_order_num'] = $data['official_order_num'] ?? '';
            $update['channel_callback'] = json_encode($input, JSON_UNESCAPED_UNICODE);
            $update['channel_callback_date'] = time();
            Dever::load('order', 'seller')->notify($order, $msg, $update);
            echo $this->info['notify_success'];die;
        }
        $this->error('error');
    }

    private function error($msg)
    {
        if ($this->info && $this->info['notify_error']) {
            if ($this->info['notify_error'] == 500) {
                header("HTTP/1.1 500 Internal Server Error");
                header("Status: 500 Internal Server Error");
            } else {
                echo $this->info['notify_error'];die;
            }
        }
        echo $msg;die;
    }

    # 生成回调
    public function callback()
    {
        $input = Dever::input();
        if (!isset($input['t'])) {
            $this->error('error');
        }
        $temp = explode('-', $input['t']);
        $connect_id = $temp[0];
        $api_id = $temp[1];
        $order_key = $temp[2];
        if (!isset($input[$order_key])) {
            $this->error('error');
        }
        $order_id = $input[$order_key];
        unset($input['t']);
        $order = Dever::db('order', 'seller')->find(array('order_num' => $order_id));
        if (!$order || $order['status'] >= 10) {
            $this->error('order error');
        }
        $this->load($input, $connect_id, $api_id, $order);
    }
}