<?php

namespace Journal\Lib;

use Dever;

class Manage
{
    public function showOrderUser($id)
    {
        $info = Dever::db('journal/order')->one($id);

        $table = array();

        $user = Dever::db('passport/user')->one($info['uid']);

        $table['用户名'] = $user['username'] . '('.$info['uid'].')';
        $table['手机号'] = $user['mobile'];
        $table['产品名'] = $info['name'];
        if ($info['type'] == 1) {
            $table['支付方式'] = '购买小刊';
            $table['支付金额'] = $info['cash'];
        } elseif ($info['type'] == 3) {
            $table['支付方式'] = '购买小刊兑换码';
            $table['兑换码'] = $info['code'];
            $table['支付金额'] = $info['cash'];
        } elseif ($info['type'] == 2) {
            $table['支付方式'] = '兑换小刊';
            $table['兑换码'] = $info['code'];
        } elseif ($info['type'] == 4) {
            $table['支付方式'] = '使用通用兑换码';
            $table['兑换码'] = $info['code'];
        }
        //$table['购买数量'] = $info['num'];
        
        if ($info['source_uid'] && $info['source_uid'] != $info['uid']) {
            $user = Dever::db('passport/user')->one($info['source_uid']);
            $table['邀请人'] = $user['username'];
        }

        return Dever::table($table);
    }

    public function showOrderUserV1($id)
    {
        $info = Dever::db('act/order')->one($id);

        $table = array();

        $user = Dever::db('passport/user')->one($info['uid']);

        $table['用户名'] = $user['username'] . '('.$info['uid'].')';
        $table['手机号'] = $user['mobile'];
        $table['产品名'] = $info['name'];
        if ($info['type'] == 1) {
            $table['支付方式'] = '购买小刊';
            $table['支付金额'] = $info['cash'];
        } elseif ($info['type'] == 3) {
            $table['支付方式'] = '购买小刊兑换码';
            $table['兑换码'] = $info['code'];
            $table['支付金额'] = $info['cash'];
        } elseif ($info['type'] == 2) {
            $table['支付方式'] = '兑换小刊';
            $table['兑换码'] = $info['code'];
        } elseif ($info['type'] == 4) {
            $table['支付方式'] = '使用通用兑换码';
            $table['兑换码'] = $info['code'];
        }
        //$table['购买数量'] = $info['num'];
        
        if ($info['source_uid'] && $info['source_uid'] != $info['uid']) {
            $user = Dever::db('passport/user')->one($info['source_uid']);
            $table['邀请人'] = $user['username'];
        }

        return Dever::table($table);
    }

    # 获取订阅本数
    public function ding($id)
    {
        $data = Dever::db('journal/info')->one($id);
        $data = Dever::load('journal/api')->ding($data);

        $num_ding = $data['num_ding'];
        $num_add_ding = $data['num_add_ding'];
        $num_ratio_ding = $data['num_ratio_ding'];

        $num_view = $data['num_view'];
        $num_add_view = $data['num_add_view'];
        $num_comment = $data['num_comment'];

        $buy_num = $data['buy_num'];

        //$html = '订阅本数：('.$buy_num.'+'.$num_add_ding.')*'.$num_ratio_ding.'='.$num_ding.'<br />阅读数：'.$num_view.'+'.$num_add_view.'<br />评论数：'.$num_comment.'<br />';

        $cate = Dever::db('journal/cate')->one($data['cate_id']);
        $html = '《' . $cate['name'] . '》<br />';

        if ($data['buy'] == 1) {
            $html .= '<font style="color:red">收费电子刊</font><br />';
            $html .= '订阅本数：('.$buy_num.'+'.$num_add_ding.')*'.$num_ratio_ding.'='.$num_ding;
        } else {
            $html .= '<font style="color:blue">免费电子刊</font><br />';
            $html .= '订阅数：('.$buy_num.'+'.$num_add_ding.')*'.$num_ratio_ding.'='.$num_ding;
        }
        
        return $html;
    }

    public function showOrderStatus($id)
    {
        $info = Dever::db('journal/order')->one($id);
        if ($info) {
            if ($info['status'] == 1) {
                #return '*已支付';
                return '待支付';
            } elseif ($info['status'] == 2) {
                return '已支付';
            } elseif ($info['status'] == 3) {
                return '支付失败';
            } elseif ($info['status'] == 5) {
                $table['状态'] = '已退款';
                $manage = Dever::db('manage/admin')->one($info['tk_admin']);
                $table['备注'] = $info['tk_desc'];
                $table['截图'] = '<a><img src="'.$info['tk_pic'].'" style="width:150px" /></a>';
                //$table['退款时间'] = date('Y-m-d H:i:s', $info['tk_time']);
                $table['操作人'] = $manage['username'];
                return Dever::table($table);
            }
        }
    }

    public function showOrderStatusV1($id)
    {
        $info = Dever::db('act/order')->one($id);
        if ($info) {
            if ($info['status'] == 1) {
                #return '*已支付';
                return '待支付';
            } elseif ($info['status'] == 2) {
                return '已支付';
            } elseif ($info['status'] == 3) {
                return '支付失败';
            } elseif ($info['status'] == 5) {
                $table['状态'] = '已退款';
                $manage = Dever::db('manage/admin')->one($info['tk_admin']);
                $table['备注'] = $info['tk_desc'];
                $table['截图'] = '<a><img src="'.$info['tk_pic'].'" style="width:150px" /></a>';
                //$table['退款时间'] = date('Y-m-d H:i:s', $info['tk_time']);
                $table['操作人'] = $manage['username'];
                return Dever::table($table);
            }
        }
    }

	private function search($table = 'content/article')
	{
		$keyword = Dever::input('keyword');

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

        $where = array();

        $cate = Dever::input('cate');
        if ($cate) {
            $where['cate_id'] = $cate;
        }

        if ($yes) {
            $yes = Dever::db($table)->search(array('ids' => $yes));
        }
        if (!$keyword) {
            $where['limit'] = '0,50';
            $data = Dever::db($table)->search($where);
        } else {
            $where['name'] = $keyword;
            $data = Dever::db($table)->search($where);
        }

        $result = array();
        if ($yes) {
            foreach ($yes as $k => $v) {
                if (isset($data[$k])) {
                    unset($data[$k]);
                }
                $yes[$k]['selected'] = 'selected';
            }
            $data = $yes + $data;

            $data = array_merge($data, array());
        } else {
            $data = array_merge($data, array());
        }

        if (!$data) {
            Dever::alert('暂无数据');
        }

        return $data;
	}

	# 搜索文章
    public function search_article_api()
    {
        return $this->search('content/article');
    }

    # 搜索视频
    public function search_vod_api()
    {
        return $this->search('video/vod');
    }

    # 搜索直播
    public function search_live_api()
    {
        return $this->search('video/live');
    }

    # 搜索小刊
    public function search_journal_api()
    {
        return $this->search('journal/info');
    }

    # 搜索专题
    public function search_feature_api()
    {
        return $this->search('feature/info');
    }

    # 搜索小程序
    public function search_applet_api()
    {
        return $this->search('content/applet');
    }

    # 按照分类搜索小刊
    public function search_cate_journal_api()
    {
        $level_num = Dever::input('level_num');
        $level_id = Dever::input('level_id');
        
        $auth = Dever::tops();
        $where = array();
        if ($level_num == 1) {
            $default = array
            (
                'value' => -1,
                'name' => '选择分类',
            );
            if ($auth) {
                $where['ids'] = $auth;
            }

            $data = Dever::db('journal/cate')->getIds($where);
        } elseif ($level_num == 2 && $level_id > 0) {
            $default = array
            (
                'value' => -1,
                'name' => '选择小刊',
            );
            if ($level_id > 0) {
                $where['cate_id'] = $level_id;
            } elseif ($auth) {
                $where['cate_id'] = $auth;
            }
            
            $data = Dever::db('journal/info')->getIds($where);
        } else {
            return array();
        }

        
        array_unshift($data, $default);
        $result['level_total'] = 2;
        $result['list'] = $data;
        return $result;
    }

    # 显示杂志内页
    public function journalContent()
    {
        $where['info_id'] = Dever::input('search_option_info_id');
        $where['state'] = Dever::input('search_option_state');
        $data = Dever::db('journal/content')->all($where);

        $database = Dever::load('manage/database');
        $type = Dever::db('journal/content')->config['gettype'];
        
        if ($data) {
            foreach ($data as $k => $v) {

                $data[$k]['height'] = 'height: 154px;';
                $data[$k]['info_height'] = 'height: auto;';
                if ($v['type'] > 2) {
                    $data[$k]['height'] = 'height: 104px;';
                    $data[$k]['info_height'] = 'height: 47px;';
                }
                if ($v['type'] <= 2) {
                    $data[$k]['name'] = '';
                } elseif ($v['type'] == 3) {
                    $data[$k]['pic'] = $v['focus'];
                }

                if ($v['type'] == 4 || $v['type'] == 5 || $v['type'] == 6) {
                    $data[$k]['name'] = '';
                    $data[$k]['pic'] = $v['cover'];
                }

                if ($v['type'] == 11) {
                    # 图文
                    $info = Dever::load('content/lib/article')->get($v['article_id']);
                    if ($info) {
                        $data[$k]['pic'] = $info['pic_cover'];
                        $data[$k]['name'] = $info['name'];
                    }
                } elseif ($v['type'] == 12) {
                    # 视频
                    $info = Dever::load('video/lib/vod')->get($v['vod_id']);
                    if ($info) {
                        $data[$k]['pic'] = $info['pic_cover'];
                        $data[$k]['name'] = $info['name'];
                    }
                } elseif ($v['type'] == 13) {
                    # 直播
                    $info = Dever::load('video/lib/live')->get($v['live_id']);
                    if ($info) {
                        $data[$k]['pic'] = $info['pic_cover'];
                        $data[$k]['name'] = $info['name'];
                    }
                }

                if ($v['type'] == 10) {
                    $palaces = Dever::array_decode($v['palaces']);
                    if (isset($palaces[0])) {
                        $data[$k]['pic'] = $palaces[0]['pic'];
                    }
                }

                if ($v['type'] == 21) {
                    if ($data[$k]['talk_pic']) {
                        $data[$k]['pic'] = $data[$k]['talk_pic'];
                    } else {
                        $talk = Dever::array_decode($v['talk']);
                        if ($talk) {
                            foreach ($talk as $tk => $tv) {
                                if ($tv['pic']) {
                                    $data[$k]['pic'] = $tv['pic'];
                                    break;
                                }
                            }
                        }
                    }
                }

                $data[$k]['typename'] = $type[$v['type']];

                /*
                if ($v['type'] == 1) {
                    $data[$k]['text'] = $this->text($database, $v['id'], $where['info_id']);
                } else {
                    $data[$k]['text'] = '';
                }
                */
                $data[$k]['text'] = '';

                $link = $database->url('edit', $v['id']) . '&search_option_info_id=' . $where['info_id'];

                $data[$k]['edit'] = "fastEdit($(this), '".$link."', '编辑内页', '', 1)";

                $data[$k]['editurl'] = $link;

                $data[$k]['delete'] = $database->url('delete', $v['id']);
            }
        }

        return $data;
    }

    private function text_name($link, $content_id, $info_id, $location, $name)
    {
        $where['info_id'] = $info_id;
        $where['content_id'] = $content_id;
        $where['location'] = $location;
        $info = Dever::db('journal/content_text')->one($where);

        $link .= '&search_option_info_id=' . $info_id. '&search_option_content_id=' . $content_id.'&table=content_text&search_option_location='.$location;

        if ($info) {
            $link .= '&where_id=' . $info['id'];
        }

        $html = '<a style="cursor:pointer;" href="javascript:;" onclick="fastEdit($(this), \''.$link.'\', \'编辑'.$name.'文字\', \'\')">'.$name.'</a>';

        return $html;
    }

    public function text($database, $content_id, $info_id)
    {
        $link = $database->url('edit');

        $location = Dever::db('journal/content_text')->config['location'];

        $html = '<table class="layui-table"><tbody><tr>';
        $i = 0;
        foreach ($location as $k => $v) {
            $name = $this->text_name($link, $content_id, $info_id, $k, $v);
            $html .= '<td>'.$name.'</td>';

            if ($i == 2 || $i == 5) {
                $html .= '</tr><tr>';
            }

            $i++;
        }

        $html .= '</tr></tbody></table>';

        return $html;
    }

    public function updateJournalContent_api()
    {
        $data = Dever::input('data');

        if ($data) {
            $data = explode(',', str_replace('data-', '', $data));
            foreach ($data as $k => $v) {
                $k = $k + 1;
                $where['where_id'] = $v;
                $where['reorder'] = $k;
                Dever::db('journal/content')->update($where);
            }

        }

        return $data;
    }

    public function addContent($id, $name, $data)
    {
        # 获取最新的reorder
        $info_id = Dever::param('info_id', $data);
        $data = Dever::db('journal/content')->getAllByReorder(array('info_id' => $info_id));

        if ($data) {
            $update['where_id'] = $id;
            $update['reorder'] = $data['reorder'] + 1;
            Dever::db('journal/content')->update($update);
        }
    }

    public function getName($id)
    {
        $info = Dever::db('journal/buy_num')->one($id);
        if ($info && $info['name']) {
            return $info['name'];
        } else {
            return '购买' . $info['num'] . '本';
        }
    }

    public function getPrice($id)
    {
        $info = Dever::db('journal/buy_num')->one($id);

        $journal = Dever::db('journal/info')->one($info['info_id']);
        if ($info && $info['price']) {
            return $info['price'];
        } else {
            return $info['num'] * $journal['cash'] . '元';
        }
    }

    public function mobileTemplate_api()
    {
        $host = 'http://' . $_SERVER['HTTP_HOST'] . '/wonderful/';
        $file = $host . 'doc/mobile.xlsx';

        Dever::location($file);
    }

    public function setJournalCache($id, $name, $param)
    {
        Dever::load('act/order')->setCache($id, 1);
    }

    public function setBuyCache($id, $name, $param)
    {
        Dever::load('act/order')->setCache($id, 2);
    }

    public function setActiveCache($id, $name, $param)
    {
        Dever::load('act/order')->setCache($id, 3);
    }

    /**
     * 导入用户
     *
     * @return mixed
     */
    public function mobileImport($id, $name, $param)
    {
        $file = Dever::param('file', $param);
        if ($file) {
            $file = str_replace('wonderful', 'upload', Dever::local($file));
            $file = str_replace('https://api.huoxingkandiqiu.com/', '/www/', $file);
            //$file = '/www/upload/data/upload/4/2019/04/11/f730437f0b5764790baed3c96c5264b2.xlsx';
            $data = Dever::excelImport($file);
            if ($data) {
                foreach ($data as $k => $v) {
                    if ($k > 1) {
                        if (isset($v['A']) && isset($v['B'])) {
                            if ($v['A']) {
                                $name = $v['A'];
                            }
                            
                            $journal = Dever::db('journal/info')->one(array('name' => $name));
                            if ($journal) {
                                $info = Dever::db('journal/mobile')->one(array('product_id' => $journal['id'], 'mobile' => $v['B']));

                                if (!$info) {
                                    Dever::db('journal/mobile')->insert(array('product_id' => $journal['id'], 'mobile' => $v['B']));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}