<?php namespace Place\Lib;
use Dever;
use Place;
class Act
{
    private $db;
    private $type;
    public function __construct($name, $type = false, $type_id = false)
    {
        $this->type = Dever::input('type', '', '', $type);
        $this->type_id = Dever::input('type_id', '', '', $type_id);
        $this->db = Dever::db($name, 'place');
        $this->app = Dever::config('setting')['type'][$this->type];
    }

    protected function check()
    {
        if (!$this->type || !$this->type_id) {
            Dever::error('传入参数错误');
        }
    }

    # 更新互动信息
    public function up($data = array(), $msg = '')
    {
        if ($this->getInfo($data)) {
            $msg && Dever::error($msg);
        } else {
            $this->db->insert($this->data);
        }
        return 'ok';
    }

    # 删除
    public function del($data = array())
    {
        if ($info = $this->getInfo($data)) {
            $this->db->delete($info['id']);
        }
        return 'ok';
    }

    # 获取用户互动相关的信息
    public function getInfo($data = array(), $field = array())
    {
        $this->check();
        $this->data = $data;
        $this->data['type'] = $this->type;
        $this->data['type_id'] = $this->type_id;
        $this->data['uid'] = Place::$uid;
        return $this->handleInfo($this->db->find($this->data), $field);
    }

    # 获取详情页
    public function getView($field = array())
    {
        $data['id'] = Dever::input('data_id', 'is_numeric', 'ID');
        $info = $this->getInfo($data, $field);
        return Dever::load('order', $this->app)->getView($info);
    }

    # 评论列表
    public function getList()
    {
        $data['type'] = Dever::input('type', 'is_numeric', '类型');
        $data['type_id'] = Dever::input('type_id', 'is_numeric', '类型ID');
        # 每页10条
        $set['num'] = 10;
        $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'));
            }
        }
        return $data;
    }

    # 获取用户的互动列表
    public function getUserList($field = array(), $page = 10)
    {
        $where['uid'] = Place::$uid;
        if ($this->type) {
            $where['type'] = $this->type;
        }
        # 每页10条
        $set['num'] = $page;
        $data = $this->db->select($where, $set);
        $result = array();
        if ($data) {
            foreach ($data as $k => $v) {
                $info = $this->handleInfo($v, $field);
                if ($info) {
                    $result[] = $info;
                }
            }
        }
        return $result;
    }

    private function getTypeInfo($type, $type_id)
    {
        $app = Dever::config('setting')['type'][$type];
        if ($type > 0) {
            $db = Dever::db('info', $app);
            $info = $db->find($type_id, array('col' => 'id,name,pic,info'));
            $info['pic'] = explode(',', $info['pic']);
            $info['pic'] = $info['pic'][0] ?? '';
            return $info;
        }
    }

    private function handleInfo($v, $field = array())
    {
        if (!$v) {
            return array();
        }
        $info = $this->getTypeInfo($v['type'], $v['type_id']);
        if ($info) {
            #$info['status'] = $v['status'];
            #$info['status_name'] = $this->db->value('status', $v['status']);
            $info['type'] = $v['type'];
            $info['type_id'] = $v['type_id'];
            $info['data_id'] = $v['id'];
            //$info['cdate'] = $v['cdate'];
            $info['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
            if ($field) {
                foreach ($field as $v1) {
                    $info[$v1] = $v[$v1];
                }
            }
            return $info;
        }
    }
}