<?php namespace Place_interface\Api;
use Dever;
use Place;
use Place_interface\Lib\Core;
class Source extends Core
{
    private $channel_id = 0;
    private $field = array
    (
        'info' => 'id,name,info,pic,type,spec_type',
        'cate' => 'id,parent_id,score_id,buy,name,status,`desc`',
    );

    # 首页
    public function home()
    {
        $this->channel_id = Dever::input('channel_id', 'is_numeric', '频道ID');
        # 获取频道信息
        $data['channel'] = Dever::db('channel', 'place_source')->find(array('id' => $this->channel_id, 'status' => 1), array('col' => 'id,type,name,`desc`'));
        if (!$data['channel']) {
            Dever::error('频道不存在');
        }

        # 分类信息
        $data['top'] = array();
        if ($data['channel']['type'] == 1) {
            # 显示子分类
            # 根据频道获取主分类
            $top = Dever::input('top_id');
            $data['top'] = $this->getCate(0);
            if ($top) {
                $data['cate'] = $this->getData($top);
            } elseif ($data['top']) {
                $data['cate'] = $this->getData($data['top'][0]['id']);
            }
        } else {
            # 不显示子分类
            $data['cate'] = $this->getData(0);
        }

        # 广告和推荐位信息
        $data['ad'] = array();

        return $data;
    }

    # 获取分类及其资源
    private function getData($parent_id)
    {
        $data = $this->getCate($parent_id);
        if (!$data && $parent_id > 0) {
            $data = $this->getCate(0, $parent_id);
        }
        if ($data) {
            foreach ($data as &$v) {
                $v['list'] = $this->getSource($v, 5);
            }
        }
        return $data;
    }

    # 获取分类信息
    private function getCate($parent_id = 0, $id = false)
    {
        if ($id) {
            $where['id'] = $id;
        }
        $where['channel_id'] = $this->channel_id;
        $where['parent_id'] = $parent_id;
        return Dever::db('cate', 'place_source')->select($where, array('col' => $this->field['cate']));
    }

    # 获取资源信息
    private function getSource($cate, $limit, $page = false)
    {
        $key = 'cate_parent_id';
        if ($cate['parent_id'] > 0) {
            $key = 'cate_child_id';
        }
        $where = array($key => $cate['id'], 'status' => 1);
        $search = Dever::input('search');
        if ($search) {
            $where['name'] = array('like', $search);
        }
        $set = array('col' => $this->field['info']);
        if ($limit) {
            $set['limit'] = $limit;
        }
        if ($page) {
            $set['num'] = $page;
        }
        $data = Dever::db('info', 'place_source')->select($where, $set);

        # 获取详情
        if ($data) {
            $cate['score'] = Dever::db('info', 'place_score')->find($cate['score_id']);
            foreach ($data as &$v) {
                $v = Dever::load('info', 'place_source')->get($v, $cate);
            }
        }
        return $data;
    }


    # 某个分类的列表页
    public function cate()
    {
        $cate_id = Dever::input('cate_id', 'is_numeric', '分类ID');
        $data['cate'] = Dever::db('cate', 'place_source')->find($cate_id, array('col' => $this->field['cate']));
        if (!$data['cate']) {
            Dever::error('分类不存在');
        }
        if ($data['cate']['status'] != 1) {
            Dever::error('分类不存在');
        }
        $data['list'] = $this->getSource($data['cate'], false, 10);
        return $data;
    }

    # 详情页
    public function view()
    {
        $source_id = Dever::input('source_id', 'is_numeric', '资源ID');
        $data['info'] = Dever::load('info', 'place_source')->get($source_id, false, true);
        $content = Dever::db('content', 'place_source')->find(array('info_id' => $source_id));
        # 是否展示内容
        $data['content'] = 2;
        if ($content) {
            $data['content'] = 1;
        }
        # 展示说明
        $data['desc'] = Dever::db('help', 'place_source')->select(array('type' => 1), array('col' => 'name,info,pic'));
        $data['help'] = Dever::db('help', 'place_source')->select(array('type' => 2), array('col' => 'name,info,pic'));

        # 获取评价
        $data['review'] = Dever::db('review', 'place_act')->select(array('type' => 1, 'type_id' => $source_id), array('limit' => 3));
        if ($data['review']) {
            foreach ($data['review'] 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('info', 'place_user')->find($v['uid'], array('col' => 'mobile,name,avatar'));
            }
        }

        return $data;
    }

    # 详情页查看价格
    public function view_price()
    {
        $source_id = Dever::input('source_id', 'is_numeric', '资源ID');
        $sku_id = Dever::input('sku_id', 'is_numeric', '规格ID');
        $buy_num = Dever::input('buy_num', 'is_numeric', '数量', 1);
        $data = Dever::load('info', 'place_source')->getSkuList($source_id, $sku_id, $buy_num);
        return $data;
    }

    # 详情页查看内容
    public function view_content()
    {
        $source_id = Dever::input('source_id', 'is_numeric', '资源ID');
        $data['cate'] = Dever::db('content_cate', 'place_source')->select(array('info_id' => $source_id, 'status' => 1));
        $set = array('num' => 10);
        if ($data['cate']) {
            $cate_id = Dever::input('cate_id');
            if (!$cate_id) {
                $cate_id = $data['cate'][0]['id'];
            }
            $data['content'] = Dever::db('content', 'place_source')->select(array('info_id' => $source_id, 'cate_id' => $cate_id), $set);
        } else {
            $data['content'] = Dever::db('content', 'place_source')->select(array('info_id' => $source_id), $set);
        }
        if (Place::$uid && $data['content']) {
            # 检测是否购买过
            foreach ($data['content'] as $k => $v) {

            }
        }
        return $data;
    }

    # 详情页查看内容
    public function view_content_info()
    {
        $content_id = Dever::input('content_id', 'is_numeric', '内容ID');
        $data['info'] = Dever::db('content', 'place_source')->find($content_id);
        # 检测是否购买过
        return $data;
    }

    # 拉起支付
    public function pay()
    {
        if (!Place::$uid) {
            $this->showLogin();
        }
        $info = $this->view_price();
        if ($info['sku']['buy'] == 1) {
            return Dever::load('info', 'place_source')->pay($info);
        } else {
            Dever::error('您当前的身份角色无法购买');
        }
    }

    # 重新支付
    public function repay()
    {

    }

    # 资源评论列表
    public function getReviewList()
    {
        $where['type'] = 1;
        $where['type_id'] = Dever::input('source_id', 'is_numeric', '资源ID');
        # 每页10条
        $set['num'] = Dever::input('num', 'is_numeric', '分页条数', 10);
        $data = Dever::db('review', 'place_act')->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('info', 'place_user')->find($v['uid'], array('col' => 'mobile,name,avatar'));
            }
        }
        $result['list'] = $data;
        $result['total'] = Dever::page('total');
        return $result;
    }

    # 资源订单列表
    public function getOrderList()
    {
        $where['uid'] = Place::$uid;
        $set['num'] = 10;
        $set['col'] = 'id,order_num,source_id,name,cash,num,cdate,status';
        $status = Dever::input('status');
        if ($status) {
            if ($status == 10) {
                $where['or'] = array
                (
                    'refund_status' => '1',
                    'status' => '8',
                );
                $where = ' uid = ' . $where['uid'] . ' and (refund_status = 1 or status = 8)';
            } else {
                $where['status'] = array('in', $status);
            }
        }
        $data = Dever::db('source', 'place_order')->select($where, $set);
        $result = array();
        if ($data) {
            foreach ($data as $k => $v) {
                $order = $this->getOrderInfo($v);
                if ($order) {
                    $result[] = $order;
                }
            }
        }
        return $result;
    }

    private function getOrderInfo($order, $view = false)
    {
        if ($order) {
            $info = Dever::db('info', 'place_source')->find($order['source_id'], array('col' => 'id,name,pic,info'));
            $info['pic'] = explode(',', $info['pic']);
            $order['pic'] = $info['pic'][0] ?? '';
            $order['status_name'] = Dever::db('source', 'place_order')->value('status', $order['status']);
            $order['cdate_str'] = date('Y-m-d H:i:s', $order['cdate']);
            if (isset($order['pdate']) && $order['pdate']) {
                $order['pdate_str'] = date('Y-m-d H:i:s', $order['pdate']);
            }
            if (isset($order['ddate']) && $order['ddate']) {
                $order['ddate_str'] = date('Y-m-d H:i:s', $order['ddate']);
            }
            if (isset($order['qdate']) && $order['qdate']) {
                $order['qdate_str'] = date('Y-m-d H:i:s', $order['qdate']);
            }
            if (isset($order['fdate']) && $order['fdate']) {
                $order['fdate_str'] = date('Y-m-d H:i:s', $order['fdate']);
            }
            $order = Dever::load('source/order', 'place_order')->getInfo($order, $view);
        }
        return $order;
    }
}