<?php

namespace app\admin\controller;
use think\facade\View;
use think\facade\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Mallvouchertemplate extends AdminControl {

    private $quotastate_arr;
    private $templatestate_arr;

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/mallvouchertemplate.lang.php');
        if (config('ds_config.voucher_allow') != 1 || config('ds_config.points_isuse') != 1) {
            $this->error(lang('admin_voucher_unavailable'), 'operation/setting');
        }
    }
    /*
     * 代金券面额列表
     */

    public function mallvouchertemplatelist() {
        //获得代金券金额列表
        $mallvouchertemplate_model = model('mallvouchertemplate');
        $condition = array();
        $mallvouchertemplate_list = $mallvouchertemplate_model->getMallvouchertemplateList($condition,10);
        View::assign('mallvouchertemplate_list', $mallvouchertemplate_list);
        View::assign('show_page', $mallvouchertemplate_model->page_info->render());
        $this->setAdminCurItem('mallvouchertemplatelist');
        return View::fetch();
    }
    

    /*
     * 添加代金券页面
     */

    public function mallvouchertemplateadd() {
        if (request()->isPost()) {
            $mallvouchertemplate_model = model('mallvouchertemplate');
            $goodsclass_model = model('goodsclass');
            
            //获取代金券所属分类ID集
            $gc_id = intval(input('post.mallvouchertemplate_gcid'));
            if($gc_id == 0){
                $this->error(lang('admin_mallvouchertemplate_gc_error'));
            }
            
            $gc_ids = ',';
            $goodsclasslist = $goodsclass_model->getChildClass($gc_id);
            foreach($goodsclasslist as $key => $val){
               $gc_ids .= $val['gc_id'].',';
            } 
            
            
            $limit = intval(input('post.mallvouchertemplate_limit')) > 0 ? intval(input('post.mallvouchertemplate_limit')) : 0;
            $price = intval(input('post.mallvouchertemplate_price')) > 0 ? intval(input('post.mallvouchertemplate_price')) : 0;

            $data = [
                'mallvouchertemplate_price' => $price,
                'mallvouchertemplate_title' => input('post.mallvouchertemplate_title'),
                'mallvouchertemplate_gcid' => $gc_id,
                'mallvouchertemplate_gcname' => input('post.mallvouchertemplate_gcname'),
                'mallvouchertemplate_limit' => $limit,
                'mallvouchertemplate_startdate' => strtotime(input('param.mallvouchertemplate_startdate')),
                'mallvouchertemplate_enddate' => strtotime(input('param.mallvouchertemplate_enddate')),
                'mallvouchertemplate_gcidarr' => $gc_ids,
                'mallvouchertemplate_points' => input('post.mallvouchertemplate_points'),
                'mallvouchertemplate_quantity' => intval(input('post.mallvouchertemplate_quantity')),
                'mallvouchertemplate_eachlimit' => intval(input('post.mallvouchertemplate_eachlimit')),
            ];
            if ($price >= $limit) {
                $this->error(lang('admin_mallvouchertemplate_limit_error'));
            }
                $rs = $mallvouchertemplate_model->addmallvouchertemplate($data);
                if ($rs) {
                    $this->log(lang('ds_add') . lang('admin_mallvouchertemplate_add') . '[' . $data['mallvouchertemplate_title'] . ']');
                    $this->success(lang('ds_common_save_succ'), (string) url('mallvouchertemplate/mallvouchertemplatelist'));
                } else {
                    $this->error(lang('ds_common_save_fail'), 'mallvouchertemplate/mallvouchertemplatelist');
                }
            }
         else {
            $mallvouchertemplate_info = array(
                'mallvouchertemplate_startdate' => TIMESTAMP,
                'mallvouchertemplate_enddate' => TIMESTAMP+3600*24*7,
                'mallvouchertemplate_gcid' => '',
                'mallvouchertemplate_quantity' => '',
                'mallvouchertemplate_eachlimit'=>''
            ); 
            $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
            View::assign('gc_list', $gc_list);
            View::assign('info', $mallvouchertemplate_info);
            View::assign('action', 'add');
            $this->setAdminCurItem('mallvouchertemplateadd');
            return View::fetch();
        }
    }
    
    /*
     * 添加代金券页面
     */
    
    public function mallvouchertemplateedit() {
        $id = intval(input('param.mallvouchertemplate_id'));
        if ($id <= 0) {
            $this->error(lang('param_error'), 'Mallvouchertemplate/mallvouchertemplatelist');
        }
        
        if (request()->isPost()) {
            $mallvouchertemplate_model = model('mallvouchertemplate');
            $goodsclass_model = model('goodsclass');
            
            //获取代金券所属分类ID集
            $gc_id = intval(input('post.mallvouchertemplate_gcid'));
            if($gc_id == 0){
                $this->error(lang('admin_mallvouchertemplate_gc_error'));
            }
            
            
            $gc_ids = ',';
            
            if($gc_id > 0){
               $goodsclasslist = $goodsclass_model->getChildClass($gc_id);
               foreach($goodsclasslist as $key => $val){
                   $gc_ids .= $val['gc_id'].',';
               } 
            }
            
            $limit = intval(input('post.mallvouchertemplate_limit')) > 0 ? intval(input('post.mallvouchertemplate_limit')) : 0;
            $price = intval(input('post.mallvouchertemplate_price')) > 0 ? intval(input('post.mallvouchertemplate_price')) : 0;
    
            $updata = [
                'mallvouchertemplate_price' => $price,
                'mallvouchertemplate_title' => input('post.mallvouchertemplate_title'),
                'mallvouchertemplate_gcid' => $gc_id,
                'mallvouchertemplate_gcname' => input('post.mallvouchertemplate_gcname'),
                'mallvouchertemplate_limit' => $limit,
                'mallvouchertemplate_startdate' => strtotime(input('param.mallvouchertemplate_startdate')),
                'mallvouchertemplate_enddate' => strtotime(input('param.mallvouchertemplate_enddate')),
                'mallvouchertemplate_gcidarr' => $gc_ids,
                'mallvouchertemplate_points' => input('post.mallvouchertemplate_points'),
                'mallvouchertemplate_quantity' => intval(input('post.mallvouchertemplate_quantity')),
                'mallvouchertemplate_eachlimit' => intval(input('post.mallvouchertemplate_eachlimit')),
            ];
            if ($price >= $limit) {
                $this->error(lang('admin_mallvouchertemplate_limit_error'));
            }
            
            $condition   = array();
            $condition[] = array('mallvouchertemplate_id','=',$id);
            $rs = $mallvouchertemplate_model->editMallvouchertemplate($condition,$updata);
            
            if ($rs) {
                $this->log(lang('ds_edit') . lang('admin_mallvouchertemplate_edit') . '[' . $updata['mallvouchertemplate_title'] . ']');
                $this->success(lang('ds_common_save_succ'), (string) url('mallvouchertemplate/mallvouchertemplatelist'));
            } else {
                $this->error(lang('ds_common_save_fail'), 'mallvouchertemplate/mallvouchertemplatelist');
            }
        }else {
             
            $mallvouchertemplate_model = model('mallvouchertemplate');
            $mallvouchertemplate_info = $mallvouchertemplate_model->getOneMallvouchertemplate(array('mallvouchertemplate_id' => $id));
            if (empty($mallvouchertemplate_info)) {
                $this->error(lang('param_error'), 'Mallvouchertemplate/mallvouchertemplatelist');
            }
            
            $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
            View::assign('gc_list', $gc_list);
            View::assign('info', $mallvouchertemplate_info);
            View::assign('action', 'edit');
            $this->setAdminCurItem('mallvouchertemplateedit');
            return View::fetch();
        }
    }
    

    /*
     * 查看代金券面额
     */

    public function mallvouchertemplateview() {
        $id = intval(input('param.mallvouchertemplate_id'));
        if ($id <= 0) {
            $this->error(lang('param_error'), 'Mallvouchertemplate/mallvouchertemplatelist');
        }
        $mallvouchertemplate_model = model('mallvouchertemplate');
        $mallvouchertemplate_info = $mallvouchertemplate_model->getOneMallvouchertemplate(array('mallvouchertemplate_id' => $id));
        if (empty($mallvouchertemplate_info)) {
            $this->error(lang('param_error'), 'Mallvouchertemplate/mallvouchertemplatelist');
        }
        
        $gc_list = model('goodsclass')->getGoodsclassListByParentId(0);
        View::assign('gc_list', $gc_list);
        View::assign('info', $mallvouchertemplate_info);
        View::assign('action', 'view');
        $this->setAdminCurItem('mallvouchertemplateview');
        return View::fetch('mallvouchertemplateview');
    }

    /*
     * 删除代金券面额
     */

    public function drop() {
        $mallvouchertemplate_id = trim(input('param.mallvouchertemplate_id'));
        if (empty($mallvouchertemplate_id)) {
            $this->error(lang('param_error'), 'Mallvouchertemplate/mallvouchertemplatelist');
        }
        $mallvouchertemplate_model = model('mallvouchertemplate');
        $condition = array();
        $condition[] = array('mallvouchertemplate_id','in', $mallvouchertemplate_id);
        $rs = $mallvouchertemplate_model->delMallvouchertemplate($condition);
        if ($rs) {
            $this->log(lang('ds_del') . lang('admin_mallvouchertemplate_drop') . '[ID:' . $mallvouchertemplate_id . ']');
            ds_json_encode(10000, lang('ds_common_del_succ'));
        } else {
            ds_json_encode(10001, lang('ds_common_del_fail'));
        }
    }
 

  

    /**
     * 页面内导航菜单
     * @param string $menu_key 当前导航的menu_key
     * @param array $array 附加菜单
     * @return
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'mallvouchertemplatelist',
                'text' => lang('admin_mallvouchertemplate_manage'),
                'url' => (string)url('Mallvouchertemplate/mallvouchertemplatelist')
            )
        );
        if (request()->action() == 'mallvouchertemplateadd' || request()->action() == 'mallvouchertemplatelist') {
            $menu_array[] = array(
                'name' => 'mallvouchertemplateadd',
                'text' => lang('admin_mallvouchertemplate_add'),
                'url' => (string) url('Mallvouchertemplate/mallvouchertemplateadd')
            );
        }
        if (request()->action() == 'mallvouchertemplateview') {
            $menu_array[] = array(
                'name' => 'mallvouchertemplateview',
                'text' => lang('admin_mallvouchertemplate_view'),
                'url' => ''
            );
        }
        if (request()->action() == 'mallvouchertemplateedit') {
            $menu_array[] = array(
                'name' => 'mallvouchertemplateedit',
                'text' => lang('admin_mallvouchertemplate_edit'),
                'url' => ''
            );
        }
        return $menu_array;
    }

}
