<?php namespace Place\Api;
use Dever;
use Place;
use Place\Lib\Main;
use Place\Lib\Cate;
use Place\Lib\Resource as Core;
class Resource extends Main
{
    protected $entry = true;
    protected $type;

    public function __construct()
    {
        parent::__construct();
        $type = Dever::input('type', 'is_numeric', '资源类型');
        $this->service = new Core($type);
    }

    # 首页、列表页
    public function home()
    {
        $cate = new Cate($this->service->app);
        $data = $cate->getData();
        $data['list'] = $this->service->getList($data['cate_id']);
        return Place::result($data);
    }

    # 详情
    public function view()
    {
        $id = Dever::input('type_id', 'is_numeric', '资源ID');
        $data['info'] = $this->service->getInfo($id);
        # 分类下其他内容
        $data['cate'] = $this->service->getList($data['info']['cate'], $data['info']['id'], 6);
        # 最新内容
        $data['new'] = $this->service->getList(false, $data['info']['id'], 6);
        return Place::result($data);
    }

    # 点击购买按钮
    public function submit()
    {
        if (!Place::$uid) {
            $this->showLogin();
        }
        $id = Dever::input('type_id', 'is_numeric', '资源ID');
        $info = $this->service->getInfo($id);
        $data['info'] = Dever::load('info', $this->service->app)->submit($info);
        return $data;
    }

    # 拉起支付
    public function pay()
    {
        if (!Place::$uid) {
            $this->showLogin();
        }
        $id = Dever::input('type_id', 'is_numeric', '资源ID');
        $info = $this->service->getInfo($id);
        $info = Dever::load('info', $this->service->app)->submit($info, 'price', true);
        if ($info['status'] == 2) {
            return $this->service->pay($info);
        } else {
            Dever::error('无权限');
        }
    }

    # 重新支付
    public function repay()
    {

    }

    # 资源评论列表
    public function getReviewList()
    {
        $where['type'] = Dever::input('type', 'is_numeric', '类型');
        $where['type_id'] = Dever::input('type_id', 'is_numeric', '类型ID');
        # 每页10条
        $set['num'] = Dever::input('num', 'is_numeric', '分页条数', 3);
        $data = Dever::db('review', 'place')->select($where, $set);
        if ($data) {
            foreach ($data as &$v) {
                $v['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
                $v['oper'] = 2;
                if (Place::$uid == $v['uid']) {
                    $v['oper'] = 1;
                }
                $v['user'] = Dever::db('member', 'place')->find($v['uid'], array('col' => 'mobile,name,avatar'));
            }
        }
        $result['list'] = $data;
        $result['total'] = Dever::page('total');
        return $result;
    }

    # 获取订单列表
    public function getOrderList()
    {
        $data['order'][] = array('name' => '待付款', 'status' => 1);
        $data['order'][] = array('name' => '待发货', 'status' => 2);
        $data['order'][] = array('name' => '待收货', 'status' => '3,4');
        $data['order'][] = array('name' => '已完成', 'status' => '5,6,7,9');
        $data['order'][] = array('name' => '退款', 'status' => '10');
        $data['list'] = $this->service->getOrderList();
        return Place::result($data);
    }

    # 获取订单详情
    public function getOrderView()
    {
        $data['info'] = $this->service->getOrderView();
        return Place::result($data);
    }

    # 修改订单地址
    public function upOrderAddress()
    {
        $this->service->upOrderAddress();
        return 'ok';
    }

    # 确认收货
    public function upOrderFinish()
    {
        $this->service->upOrderFinish();
        return 'ok';
    }

    # 取消订单
    public function upOrderCancel()
    {
        $this->service->upOrderCancel();
        return 'ok';
    }

    # 申请退款
    public function applyRefund()
    {
        $info = $this->service->getOrderView();
        if ($info && $info['type'] == 1 && $info['status'] > 1 && $info['status'] < 7 && !$info['refund']) {
            $data['type'] = Dever::db('order_refund', $this->service->app)->value('type');
            $data['desc_type'] = Dever::db('order_refund', $this->service->app)->value('desc_type');
            $data['cash'] = $info['scash'];
            $data['detail'] = $info['detail'];
            return $data;
        } else {
            Dever::error('无法申请退款');
        }
    }

    # 确认申请退款
    public function applyRefundAct_commit(){}
    public function applyRefundAct()
    {
        $info = $this->service->getOrderView();
        if ($info && $info['type'] == 1 && $info['status'] > 1 && $info['status'] < 7 && !$info['refund']) {
            $type = Dever::input('apply_type', 'is_numeric', '申请类型');
            $desc_type = Dever::input('desc_type', 'is_numeric', '申请原因');
            $cash = Dever::input('cash', 'is_numeric', '退款金额');
            $desc = Dever::input('desc', 'is_string', '申请说明');
            $detail = Dever::input('detail');
            if ($detail && is_string($detail)) {
                $detail = Dever::json_decode($detail);
            }
            $log['type'] = 1;
            $log['uid'] = Place::$uid;
            Dever::load('refund', $this->service->app)->up($detail, $info, $type, $desc_type, $cash, $desc, $log);
            return 'ok';
        } else {
            Dever::error('无法申请退款');
        }
    }

    # 退款单发货 获取信息
    public function express()
    {
        $info = $this->service->getOrderView();
        if ($info && $info['type'] == 1 && $info['refund'] && $info['refund']['type'] == 1 && $info['refund']['status'] == 2) {
            $data['express'] = Dever::db('express', 'sector')->select([]);
            return $data;
        }
        Dever::error('无发货权限');
    }

    # 退款单发货
    public function expressAct()
    {
        $info = $this->service->getOrderView();
        if ($info && $info['type'] == 1 && $info['refund'] && $info['refund']['type'] == 1 && $info['refund']['status'] == 2) {
            $express_id = Dever::input('express_id', 'is_string', '快递公司');
            $number = Dever::input('number', 'is_string', '快递单号');
            Dever::load('refund', $this->service->app)->express($info, $info['refund'], $express_id, $number);
            return 'ok';
        }
        Dever::error('无发货权限');
    }
}