<?php


namespace app\api\controller;
use think\facade\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 代金券控制器
 */
class Membervoucher extends MobileMember
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
	Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/voucher.lang.php');
    }

    /**
     * @api {POST} api/Membervoucher/voucher_list 代金卷列表
     * @apiVersion 1.0.0
     * @apiGroup MemberVoucher
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.voucher_list  代金卷列表 （返回字段参考voucher表、store表、vouchertemplate表）
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function voucher_list()
    {
        $voucher_model = model('voucher');
        $voucher_state = !empty(input('post.voucher_state')) ? input('post.voucher_state') : '';
        $voucher_list = $voucher_model->getMemberVoucherList($this->member_info['member_id'], $voucher_state, $this->pagesize);
        $result = array_merge(array('voucher_list' => $voucher_list), mobile_page($voucher_model->page_info));
        ds_json_encode(10000, '', $result);
    }


    /**
     * @api {POST} api/Membervoucher/voucher_point 代金券领取
     * @apiVersion 1.0.0
     * @apiGroup MemberVoucher
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {String} tid 代金券模板id
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function voucher_point()
    {
        $vid = intval(input('post.tid'));

        if ($vid <= 0){
            ds_json_encode(10001,lang('param_error'));
        }

        $voucher_model = model('voucher');
        //验证是否可以兑换代金券
        $data = $voucher_model->getCanChangeTemplateInfo($vid,$this->member_info['member_id'],$this->member_info['store_id']);
        if ($data['state'] == false){
            ds_json_encode(10001,$data['msg']);
        }
        //添加代金券信息
        $data = $voucher_model->exchangeVoucher($data['info'],$this->member_info['member_id'],$this->member_info['member_name']);
        if ($data['state'] == true){
            ds_json_encode(10000, $data['msg'], 1);
        } else {
            ds_json_encode(10001,$data['msg']);
        }
    }


}