<?php

# 需求接口类

namespace Res\Src;

use Dever;
use Res\lib\Info;
use Main\Lib\Core;
use Dever\Routing\Uri;

class Data extends Core
{
    protected $checkUser = true;
    # 列表页path
    private $list_path = 'main/list';//'info/data.getList';//main/list
    # 详情页path
    private $view_path = 'main/view';//'info/data.getInfo';//main/view

    private $info = false;
    public function __construct()
    {
        # 当没有top时，就是首页
        $this->top = Dever::input('top', -1);
        if ($this->top) {
            $this->info = Info::init($this->top);
        }

        # 当没有cate，但有top时就是顶级分类列表页，如果有cate，就是二级分类列表页
        $this->cate = Dever::input('cate');

        # 当有id时，就是详情页
        $this->id = Dever::input('id');
    }

    # 获取顶级分类列表，首页用到
    public function top()
    {
        $top = Dever::load('category/api')->getTop(Dever::config('base')->category);

        $result = array();
        if ($top) {
            $cates = array_keys($top);

            $child = Dever::load('category/api')->getChild($cates);
            $i = 0;
            $path = $this->list_path;
            if (Uri::$value == 'submit') {
                $path = 'submit_next';
            }
            foreach ($top as $k => $v) {
                $result[$i] = $v;
                $result[$i]['path'] = $path . '?top=' . $v['id'];
                if (isset($child[$v['id']]) && $child[$v['id']][0]['level'] != -1) {
                    $result[$i]['child'] = $child[$v['id']];
                    if (Uri::$value == 'submit') {
                        $result[$i]['path'] = 'submit_cate?top=' . $v['id'];
                    } else {
                        $result[$i]['path'] .= '&cate=' . $child[$v['id']][0]['id'];
                    }
                }

                if ($v['status'] == 1) {
                    $result[$i]['link'] = Dever::url($result[$i]['path']);
                } else {
                    $result[$i]['link'] = 'javascript:hui.toast(\'暂未开放\');';
                }
                
                $i++;
            }
        }

        return $result;
    }

    # 获取首页的分类和需求数据
    # 接口：http://192.168.33.10/info/info/?l=data.getIndex
    public function getIndex()
    {
        $top = Dever::load('res/data.top');

        if ($top) {
            $this->cate = false;
            foreach ($top as $k => $v) {
                $this->info = Info::init($v['id']);
                # 首页显示多少条需求数据，暂定10条
                $top[$k]['data'] = $this->getList('0,10', $v);
            }
        }

        return $top;
    }

    # 获取最新需求数据
    # 接口：http://192.168.33.10/info/demand/?l=data.getNew
    public function getNew()
    {
        $info = Info::init(-1);

        $data = $info->getData($this->cate, '0,10', false, false);

        # 这里需要有用户信息
        if ($data) {
            $info->setViewPath($this->view_path);
            foreach ($data as $k => $v) {
                if ($v['poster_uid'] < 0) {
                    $v['poster_uid'] = 1;
                }

                $data[$k] = $info->getInfoLink($v);
                $link = $data[$k]['view_link'];
                $data[$k]['user'] = Dever::db('passport/user')->one($v['poster_uid']);
                $data[$k]['text'] = '<span class="cuIcon-notifcation"></span><a href="'.$link.'"><span class="text-blue">'.$data[$k]['user']['username'].'刚刚发布:</span>' . Dever::cut($v['name'], 10) . '</a>';
            }
        }

        return $data;
    }

    # 获取搜索条件列表 分类列表页用到
    # 接口：http://192.168.33.10/info/demand/?l=data.getSearch&top=1&cate=3,7&json=1
    public function getSearch()
    {
        $this->checkInfo();
        return $this->info->getSearch($this->cate);
    }

    # 获取搜索条件列表 分类列表页用到 只用到了搜索条件
    # 接口：http://192.168.33.10/info/demand/?l=data.getSearchList&top=1&cate=3,7&json=1
    public function getSearchList()
    {
        $data = $this->getSearch();

        $result = array();
        if ($data && $data['cate']) {
            $result[0]['id'] = -1;
            $result[0]['name'] = '分类';
            $result[0]['option'] = $data['cate'];

            $result = array_merge($result, $data['search']);
        }

        return $result;
    }

    public function getCateInfo()
    {
        $cate = $this->cate ? $this->cate : $this->top;
        $data = Dever::db('category/info')->one($cate);

        $name = Dever::input('name');

        if ($name) {
            $data['name'] = urldecode($name);
        }

        return $data;
    }

    # 获取分类子信息
    public function getCateChild()
    {
        $data = Dever::load('category/api')->getChild($this->top);

        if ($data) {
            $data = $data[$this->top];
            foreach ($data as $k => $v) {
                $data[$k]['name'] = '我要' . $v['name'];
                $path = 'submit_next?top=' . $this->top . '&cate=' . $v['id'] . '&name=' . urlencode($data[$k]['name']);
                $data[$k]['link'] = Dever::url('main/' . $path);
            }
        }

        return $data;
    }

    # 根据顶级分类或者二级分类，获取需求列表 分类列表页用到
    # 接口：http://192.168.33.10/info/demand/?l=data.getList&top=1&cate=3,7&json=1&&attr_5=1&attr_2=9&attr_4=1
    public function getList($limit, $cate = array())
    {
        $this->checkInfo();

        $page = false;

        if (!$limit) {
            $page = true;
        }

        $where = Dever::preInput('attr_');

        $this->info->setViewPath($this->view_path);

        if ($where) {
            $data = $this->info->getDataByAttr($this->cate, $where);
        } else {
            $data = $this->info->getData($this->cate, $limit, $page);
        }

        if (Dever::input('test') == 1) {
            return $data;
        }

        if (!$cate) {
            $cate = Dever::db('category/info')->one($this->top);
        }

        $data = array('data' => $data, 'cate' => $cate, 'top' => $this->top);
        $data['show'] = false;
        if ($cate && $this->top < 0) {
            $data['show'] = true;
        }
        return Dever::render('render/list', $data, 'main');
    }

    # 获取当前用户的需求列表
    # 接口：http://192.168.33.10/info/demand/?l=data.getUserList&top=-1
    public function getUserList($limit)
    {
        $this->checkInfo();

        $data = $this->info->getDataByUid($this->data['uid']);

        return $data;
    }

    # 获取需求详情
    # 接口：http://192.168.33.10/info/demand/?l=data.getInfo&top=1&json=1&id=10000000
    public function getInfo()
    {
        if (!$this->id) {
            Dever::alert('错误的参数，请重试');
        }
        $this->checkInfo();

        $data = $this->info->getInfo($this->id);

        $data['like'] = 2;
        $data['tip'] = 2;
        # 检查当前用户是否已收藏、已举报
        if ($this->data['uid'] && $this->data['uid'] > 0) {
            $data['like'] = Dever::load('service/lib/like')->get($this->data['uid'], $this->id, 1);
        }
        

        return $data;
    }

    # 获取需求详情
    # 接口：http://192.168.33.10/info/demand/?l=data.getInfo&top=1&json=1&id=10000000
    public function getInfoPic()
    {
        $data = Dever::load('res/data.getInfo');

        if ($data['pic']) {
            return $data['pic'];
        }
        return false;
    }

    # 获取需求详情
    # 接口：http://192.168.33.10/info/demand/?l=data.getInfo&top=1&json=1&id=10000000
    public function getInfoAttr()
    {
        $data = Dever::load('res/data.getInfo');

        if ($data['attr']) {
            return $data['attr'];
        }
        return array();
    }

    # 获取更新需求时的字段
    public function getUpdate()
    {
        $this->checkInfo();

        if ($this->cate) {
            $cate = $this->cate;
            $category = $this->top . ',' . $cate;
        } else {
            $cate = $this->top;
            $category = $this->top;
        }
        $cate = $this->cate ? $this->cate : $this->top;

        $data = Dever::load('category/api')->getChild($cate);

        if ($data) {
            $data = $data[$cate];
        }
        $data = array('cate' => $data, 'category' => $category);

        # 获取当前分类的属性
        $attr = $this->info->getAttr($category);
        $data['attr'] = '';
        $data['attr_id'] = array();
        if ($attr) {
            foreach ($attr as $k => $v) {
                $data['attr_id'][] = $v['id'];
            }
            $data['attr_id'] = implode(',', $data['attr_id']);
            $data['attr'] = Dever::render('render/submit_attr', array('attr' => $attr), 'main');
        }

        return Dever::render('render/submit', $data, 'main');
    }

    # 获取分类下的属性
    public function getAttr()
    {
        $category = Dever::input('category');
        $attr_id = Dever::input('attr_id');

        $attr = $this->info->getAttr($category);

        if ($attr) {
            $attr_id = explode(',', $attr_id);
            foreach ($attr as $k => $v) {
                if (in_array($v['id'], $attr_id)) {
                    unset($attr[$k]);
                }
            }
            if ($attr) {
                return Dever::render('render/submit_attr', array('attr' => $attr), 'main');
            }
        }

        echo '';die;
    }

    # 更新或者新增需求
    # 接口：http://192.168.33.10/info/demand/?l=data.update&top=1&json=1&id=10000000&name=test&cate=1,3,9&attr_2=1
    public function update()
    {
        $this->checkInfo();

        if ($this->cate) {
            $cate = $this->top . ',' . $this->cate;
        } else {
            $cate = $this->top;
        }
        $data['name'] = Dever::input('name');
        if (!$data['name']) {
            Dever::alert('请输入标题');
        }
        $data['pic'] = Dever::input('pic');
        $data['view_price'] = Dever::input('view_price');
        $this->info->setViewPath($this->view_path);
        $data = $this->info->update($this->top, $this->cate, $data, $this->id, $this->data['uid']);

        # 提交信息之后，验证是否需要支付
        $url = $data['info']['view_link'];
        return $url;
    }

    private function checkInfo()
    {
        if (!$this->info) {
            Dever::alert('错误的参数，请重试');
        }
    }
}