<?php

namespace app\home\controller;

use think\facade\View;
use think\facade\Lang;

/**
 
 * 
 
 * 
 * ----------------------------------------------------------------------------
 * 
 
 * 控制器
 */
class Pointmallvoucher extends BasePointShop
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/voucher.lang.php');
        if (config('ds_config.voucher_allow') != 1) {
            $this->error(lang('voucher_pointunavailable'), HOME_SITE_URL);
        }
    }

    public function index()
    {
        $this->pointmallvoucher();
        return View::fetch($this->template_dir . 'pointmallvoucher');
    }
    /**
     * 平台代金券列表
     */
    public function pointmallvoucher()
    {
        //查询会员及其附属信息
        parent::pointshopMInfo();

        $mallvouchertemplate_model = model('mallvouchertemplate');


        //查询会员信息
        $member_info = model('member')->getMemberInfoByID(session('member_id'));

        //查询代金券列表
        $where = array();
        $where[] = array('mallvouchertemplate_startdate', '<', TIMESTAMP);
        $where[] = array('mallvouchertemplate_enddate', '>', TIMESTAMP);
        $gc_id = intval(input('gc_id'));

        if ($gc_id > 0) {
            $gc_idarr = array();
            $goodsclasslist = model('goodsclass')->getChildClass($gc_id);
            foreach ($goodsclasslist as $k => $v) {
                $gc_idarr[] = $v['gc_id'];
            }
            $gccondition = implode(',', $gc_idarr);
            $where[] = array('mallvouchertemplate_gcid', 'in', $gccondition);
        }
        if (intval(input('price')) > 0) {
            $where[] = array('mallvouchertemplate_price', '=', intval(input('price')));
        }



        //查询仅我能兑换和所需积分
        $points_filter = array();
        if (intval(input('isable')) == 1) {
            $points_filter['isable'] = $member_info['member_points'];
        }
        if (intval(input('points_min')) > 0) {
            $points_filter['min'] = intval(input('points_min'));
        }
        if (intval(input('points_max')) > 0) {
            $points_filter['max'] = intval(input('points_max'));
        }
        if (count($points_filter) > 0) {
            asort($points_filter);
            if (count($points_filter) > 1) {
                $points_filter = array_values($points_filter);
                $where[] = array('mallvouchertemplate_points', 'between', array($points_filter[0], $points_filter[1]));
            } else {
                if (isset($points_filter['min'])) {
                    $where[] = array('mallvouchertemplate_points', '>=', $points_filter['min']);
                } elseif (isset($points_filter['max'])) {
                    $where[] = array('mallvouchertemplate_points', '<=', $points_filter['max']);
                } elseif (isset($points_filter['isable'])) {
                    $where[] = array('mallvouchertemplate_points', '<=', $points_filter['isable']);
                }
            }
        }
        //排序
        switch (input('orderby')) {
            case 'pointsdesc':
                $orderby = 'mallvouchertemplate_points desc,';
                break;
            case 'pointsasc':
                $orderby = 'mallvouchertemplate_points asc,';
                break;
            default:
                $orderby = '';
        }
        $orderby .= 'mallvouchertemplate_id desc';
        $mallvoucherlist = $mallvouchertemplate_model->getMallvouchertemplateList($where, 10,  $orderby);
        View::assign('mallvoucherlist', $mallvoucherlist);
        View::assign('show_page', $mallvouchertemplate_model->page_info->render());

        //查询平台分类
        $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
        View::assign('gc_list', $gc_list);

        //分类导航
        $nav_link = array(
            0 => array('title' => lang('homepage'), 'link' => HOME_SITE_URL),
            1 => array('title' => lang('integral_center'), 'link' => (string)url('Pointshop/index')),
            2 => array('title' => lang('mall_voucher'))
        );
        View::assign('nav_link_list', $nav_link);
    }
    /**
     * 兑换代金券
     */
    public function mallvoucherexchange()
    {
        $vid = intval(input('param.vid'));

        $result = true;
        $message = "";
        if ($vid <= 0) {
            $result = false;
        }
        if ($result) {
            //查询可兑换代金券模板信息
            $template_info = model('mallvouchertemplate')->getCanChangeTemplateInfo($vid, intval(session('member_id')));
            if ($template_info['state'] == false) {
                $result = false;
                $message = $template_info['msg'];
            } else {
                //查询会员信息
                $member_info = model('member')->getMemberInfoByID(session('member_id'));
                View::assign('member_info', $member_info);
                View::assign('template_info', $template_info['info']);
            }
        }
        View::assign('message', $message);
        View::assign('result', $result);
        echo View::fetch($this->template_dir . 'exchange');
        exit;
    }
    /**
     * 兑换代金券保存信息
     *
     */
    public function mallvoucherexchange_save()
    {
        if (session('is_login') != '1') {
            ds_json_encode(10001, lang('param_error'));
        }
        $vid = intval(input('post.vid'));
        if ($vid <= 0) {
            ds_json_encode(10001, lang('param_error'));
        }
        $mallvouchertemplate_model = model('mallvouchertemplate');
        //验证是否可以兑换代金券
        $data = $mallvouchertemplate_model->getCanChangeTemplateInfo($vid, intval(session('member_id')));
        if ($data['state'] == false) {
            ds_json_encode(10001, $data['msg']);
        }
        //添加代金券信息
        $data = $mallvouchertemplate_model->exchangeMallvoucher($data['info'], session('member_id'), session('member_name'));
        if ($data['state'] == true) {
            ds_json_encode(10000, $data['msg']);
        } else {
            ds_json_encode(10001, $data['msg']);
        }
    }
}
