<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-1-25
 */

namespace app\user\controller;

use think\Page;
use think\Db;
use think\Config;
use think\Cookie;
use think\Request;

class Media extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->users_db = Db::name('users');
        $this->media_order_db = Db::name('media_order');
    }

    // 视频订单列表页
    public function index()
    {
        // 定义数组
        $list = [];
        $condition = [
            'a.lang' => $this->home_lang, // 多语言
            'a.users_id' => $this->users_id
        ];

        // 应用搜索条件
        $order_code = input('order_code/s');
        if (!empty($order_code)) $condition['a.order_code'] = ['LIKE', "%{$order_code}%"];

        // 订单状态搜索
        $order_status = input('order_status/s');
        $this->assign('order_status', $order_status);
        if (!empty($order_status)) {
            if (-1 == $order_status) $order_status = 0;
            $condition['a.order_status'] = $order_status;
        }

        // 支付类型
        $PayMethod = Config::get('global.pay_method_arr');

        // 分页
        $count = $this->media_order_db->alias('a')->where($condition)->count();
        $Page = new Page($count, config('paginate.list_rows'));
        $show = $Page->show();
        $this->assign('page', $show);

        // 数据查询
        $list = $this->media_order_db->where($condition)
            ->field('a.*, b.username')
            ->alias('a')
            ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
            ->order('a.order_id desc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        
        $array_new = get_archives_data($list, 'product_id');

        // 订单处理
        foreach ($list as $key => $value) {
            if (!empty($value['order_status']) && 1 == $value['order_status']) {
                $list[$key]['OrderStatusName'] = '已完成';
            } else {
                $list[$key]['OrderStatusName'] = '待付款';

                // 支付结束后返回的URL
                $ReturnUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('user/Media/index');
                cookie($this->users_id . '_' . $value['product_id'] . '_EyouMediaViewUrl', $ReturnUrl);

                // 付款地址处理，对ID和订单号加密，拼装url路径
                $Paydata = [
                    'type' => 8,
                    'order_id' => $value['order_id'],
                    'order_code' => $value['order_code']
                ];

                // 先 json_encode 后 md5 加密信息
                $Paystr = md5(json_encode($Paydata));

                // 清除之前的 cookie
                Cookie::delete($Paystr);

                // 存入 cookie
                cookie($Paystr, $Paydata);

                // 跳转链接
                $list[$key]['PaymentUrl'] = urldecode(url('user/Pay/pay_recharge_detail',['paystr'=>$Paystr]));
            }

            if (!empty($value['pay_name'])) {
                $list[$key]['pay_name'] = !empty($PayMethod[$value['pay_name']]) ? $PayMethod[$value['pay_name']] : '第三方支付';
            }

            $arcurl = '';
            $vars = !empty($array_new[$value['product_id']]) ? $array_new[$value['product_id']] : [];
            if (!empty($vars)) {
                $arcurl = urldecode(arcurl('home/Media/view', $vars));
            }
            $list[$key]['arcurl'] = $arcurl;
        }
        // 订单列表
        $this->assign('list', $list);

        // 订单数量查询
        $Where = [
            'users_id' => $this->users_id,
            'lang'     => $this->home_lang,
            'order_status' => 0
        ];
        $PendingPayment = $this->media_order_db->where($Where)->count();
        // 待付款
        $this->assign('PendingPayment', $PendingPayment);

        $Where['order_status'] = 1;
        $Completed = $this->media_order_db->where($Where)->count();
        // 已付款
        $this->assign('Completed', $Completed);

        // 搜索处理
        $hidden = '';
        $ey_config = config('ey_config'); // URL模式
        if (2 == $ey_config['seo_pseudo'] || (1 == $ey_config['seo_pseudo'] && 1 == $ey_config['seo_dynamic_format'])) {
            $hidden .= '<input type="hidden" name="m" value="user" />';
            $hidden .= '<input type="hidden" name="c" value="Media" />';
            $hidden .= '<input type="hidden" name="a" value="index" />';
            /*多语言*/
            $lang = Request::instance()->param('lang/s');
            !empty($lang) && $hidden .= '<input type="hidden" name="lang" value="'.$lang.'" />';
            /*--end*/
        }
        // 搜索的URL
        $searchurl = url('user/Media/index');
        $search = array(
            'action' => $searchurl,
            'hidden' => $hidden,
        );
        $this->assign('search', $search);

        return $this->fetch('users/media_index');
    }

    // 视频产品购买接口
    public function media_order_buy() {
        if (IS_AJAX_POST) {
            // 判断会员是否登录
            if (empty($this->users) || empty($this->users_id)) $this->error('请先登录！', url('user/Users/login'));

            // 提交的订单信息判断
            $post = input('post.');
            if (empty($post) || empty($post['aid'])) $this->error('操作异常，请刷新重试');

            // 查询是否已购买
            $where = [
                'order_status' => 1,
                'product_id' => $post['aid'],
                'users_id' => $this->users_id
            ];
            $MediaCount = Db::name('media_order')->where($where)->count();
            if (!empty($MediaCount)) $this->error('你已购买过，请直接观看');

            // 查询视频文档内容
            $Where = [
                'is_del' => 0,
                'status' => 1,
                'aid' => $post['aid'],
                'arcrank' => ['>', -1]
            ];
            $list = Db::name('archives')->where($Where)->find();
            if (empty($list)) $this->error('操作异常，请刷新重试');

            // 查看是否已生成过订单
            $where['order_status'] = 0;
            $MediaOrder = Db::name('media_order')->where($where)->order('order_id desc')->find();
            if (!empty($MediaOrder)) {
                $OrderID = $MediaOrder['order_id'];
                // 更新订单信息
                $time = getTime();
                $OrderData = [
                    'order_code'      => date('Y') . $time . rand(10,100),
                    'users_id'        => $this->users_id,
                    'mobile'          => !empty($this->users['mobile']) ? $this->users['mobile'] : '',
                    'order_status'    => 0,
                    'order_amount'    => $list['users_price'],
                    'product_id'      => $list['aid'],
                    'product_name'    => $list['title'],
                    'product_litpic'  => get_default_pic($list['litpic']),
                    'lang'            => $this->home_lang,
                    'add_time'        => $time,
                    'update_time'     => $time
                ];
                Db::name('media_order')->where('order_id', $OrderID)->update($OrderData);
            } else {
                // 生成订单并保存到数据库
                $time = getTime();
                $OrderData = [
                    'order_code'      => date('Y') . $time . rand(10,100),
                    'users_id'        => $this->users_id,
                    'mobile'          => !empty($this->users['mobile']) ? $this->users['mobile'] : '',
                    'order_status'    => 0,
                    'order_amount'    => $list['users_price'],
                    'pay_time'        => '',
                    'pay_name'        => '',
                    'wechat_pay_type' => '',
                    'pay_details'     => '',
                    'product_id'      => $list['aid'],
                    'product_name'    => $list['title'],
                    'product_litpic'  => get_default_pic($list['litpic']),
                    'lang'            => $this->home_lang,
                    'add_time'        => $time,
                    'update_time'     => $time
                ];
                $OrderID = Db::name('media_order')->insertGetId($OrderData);
            }

            // 保存成功
            if (!empty($OrderID)) {
                // 支付结束后返回的URL
                $ReturnUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url('user/Media/index');
                cookie($this->users_id . '_' . $post['aid'] . '_EyouMediaViewUrl', $ReturnUrl);

                // 对ID和订单号加密，拼装url路径
                $Paydata = [
                    'type' => 8,
                    'order_id' => $OrderID,
                    'order_code' => $OrderData['order_code'],
                ];

                // 先 json_encode 后 md5 加密信息
                $Paystr = md5(json_encode($Paydata));

                // 清除之前的 cookie
                Cookie::delete($Paystr);

                // 存入 cookie
                cookie($Paystr, $Paydata);

                // 跳转链接
                $PaymentUrl = urldecode(url('user/Pay/pay_recharge_detail',['paystr'=>$Paystr]));

                $this->success('订单已生成！', $PaymentUrl);
            }
        }
    }

}