<?php namespace Place\Api;
use Dever;
use Place;
use Place\Lib\Main;
class Client extends Main
{
    protected $login = true;
    protected $entry = true;

    public function __construct()
    {
        parent::__construct();
        # 查询当前源主界面是否有进货功能
        if (Place::$info['channel'] != 1) {
            Dever::error('渠道信息获取失败');
        }
    }

    # 检测是否绑定源主
    private function check($type = 1)
    {
        if ($type == 2) {
            if (Place::$user['client_id'] <= 0) {
                Dever::error('您还未绑定源主');
            }
            $user = Dever::db('user', 'sector')->find(Place::$uid);
            $mobile = $user['mobile'];
        } else {
            $mobile = Dever::input('mobile', 'is_numeric', '手机号');
            if (Place::$user['client_id'] > 0) {
                Dever::error('您已经绑定过了，请先解绑再来吧');
            }
        }
        return $mobile;
    }

    # 发送绑定短信验证码
    public function sms()
    {
        $type = Dever::input('type', 'is_numeric', '类型', 1);
        $mobile = $this->check($type);
        $data = Dever::load('template', 'msg')->send('user_code', array('mobile' => $mobile));
        return $data;
    }

    # 绑定源主
    public function bind()
    {
        $mobile = $this->check();
        $code = Dever::input('code', 'is_string', '验证码');
        Dever::load('template', 'msg')->check('user_code', $mobile, $code);
        $user = Dever::db('user', 'sector')->find(array('mobile' => $mobile, 'status' => 1));
        if (!$user) {
            Dever::error('您绑定的手机号未开通源主');
        }
        $data = array('client_id' => $user['id']);
        $member = Dever::db('member', 'place')->find($data);
        if ($member) {
            Dever::error('手机号已绑定源主，您无法绑定');
        }
        Dever::db('member', 'place')->update(Place::$uid, $data);
        Place::$user['client_id'] = $data['client_id'];
        return Place::$user;
    }

    # 解绑源主
    public function unbind()
    {
        $mobile = $this->check(2);
        $code = Dever::input('code', 'is_string', '验证码');
        Dever::load('template', 'msg')->check('user_code', $mobile, $code);
        Dever::db('member', 'place')->update(Place::$uid, array('client_id' => -1));
    }

    # 点击进货按钮
    public function submit()
    {
        if (Place::$user['client_id'] <= 0) {
            Dever::error('您不是源主');
        }
        $where = array('uid' => Place::$user['client_id']);
        if (Place::$user['client_id'] == Place::$info['uid']) {
            $where['id'] = array('!=', Place::$id);
        }
        # 获取源主的身份
        $data['place'] = Dever::db('info', 'place')->select($where, array('col' => 'id, name'));
        if (!$data['place']) {
            Dever::error('请先创建身份');
        }
        $type = Dever::input('type', 'is_numeric', '资源类型');
        $type_id = Dever::input('type_id', 'is_numeric', '资源ID');
        $resource = new \Place\Lib\Resource($type);
        $data['info'] = $resource->getInfo($type_id);
        $data['info'] = Dever::load('info', $resource->app)->submit($data['info'], 'client');
        return $data;
    }

    # 确定进货或者修改进货
    public function act_commit(){}
    public function act()
    {
        if (Place::$user['client_id'] <= 0) {
            Dever::error('您不是源主');
        }
        
        $data['uid'] = Place::$uid;
        $data['client_id'] = Place::$user['client_id'];
        $data['type'] = Dever::input('type', 'is_numeric', '资源类型');
        $data['type_id'] = Dever::input('type_id', 'is_numeric', '资源ID');

        $info = Dever::db('client_resource', 'place')->find($data);

        $data['status'] = 1;
        $data['place_id'] = Dever::input('place_id', 'is_string', '身份ID');
        $temp = explode(',', $data['place_id']);
        $where = array('id' => array('in', $data['place_id']), 'uid' => Place::$user['client_id']);
        if (Place::$user['client_id'] == Place::$info['uid']) {
            $where['id#'] = array('!=', Place::$id);
        }
        $place = Dever::db('info', 'place')->select($where);
        if (count($place) != count($temp)) {
            Dever::error('身份不存在');
        }

        $resource = new \Place\Lib\Resource($data['type']);
        $resource_info = $resource->getInfo($data['type_id']);
        $data['price'] = $resource_info['client']['value'];
        $resource_info['price'] = $resource_info['price']['value'];
        $resource_info['status'] = 2;
        unset($resource_info['cdate']);
        unset($resource_info['cdate_str']);
        unset($resource_info['client']);
        unset($resource_info['password']);

        if ($info) {
            Dever::db('client_resource', 'place')->update($info['id'], $data);
        } else {
            Dever::db('client_resource', 'place')->insert($data);
        }
        # 同时同步源主里的内容信息
        Dever::load('client', 'place')->resource($place, $data, $resource, $resource_info);
        return 'ok';
    }

    # 取消进货
    public function cancel_commit(){}
    public function cancel()
    {
        if (Place::$user['client_id'] <= 0) {
            Dever::error('您不是源主');
        }
        $data['uid'] = Place::$uid;
        $data['client_id'] = Place::$user['client_id'];
        $data['type'] = Dever::input('type', 'is_numeric', '资源类型');
        $data['type_id'] = Dever::input('type_id', 'is_numeric', '资源ID');
        $info = Dever::db('client_resource', 'place')->find($data);
        if ($info) {
            # 同时删除源主里的内容信息
            Dever::load('client', 'place')->resourceDel($info['place_id'], $data);
            Dever::db('client_resource', 'place')->update($info['id'], array('status' => 2));
        }
        return 'ok';
    }
}