<?php
/**
 * 红包管理
 *
 *
 *
 ***/
defined('InShopNC') or exit('Access Invalid!');

class bonusControl extends SystemControl
{
    public function __construct()
    {
        parent::__construct();
    }

    public function bonus_typeOp()
    {
        $bonus_type = Model('bonus_type');
        $bonus_list = $bonus_type->getAll();
        Tpl::output('bonus_list', $bonus_list);
        $this->show_menu('bonus', 'bonus_type_list');
        Tpl::showpage('bonus.bonus_type_list');
    }

    public function bonus_type_addOp()
    {
        $this->show_menu('bonus', 'bonus_type_add');
        $this->bonus_form();
    }

    // 添加随机红包
    public function bonus_type_add_randomOp()
    {
        $this->show_menu('bonus', 'bonus_type_add_random');
        $this->bonus_add_random_form();
    }

    public function bonus_type_editOp()
    {
        $id = $_GET['id'];
        $this->show_menu('type', 'bonus_type_edit');
        $this->bonus_form($id);
    }

    public function bonus_type_saveOp()
    {
        $type_id = $_POST['type_id'] ? $_POST['type_id'] : 0;
        $bonus_value = array(
            'type_name' => $_POST['hb_words'],
            'send_type' => $_POST['send_type'],
            'send_start_date' => strtotime(trim($_POST['send_start_date'])),
            'send_end_date' => strtotime(trim($_POST['send_end_date'])),
            'use_start_date' => strtotime(trim($_POST['use_start_date'])),
            'use_end_date' => strtotime(trim($_POST['use_end_date']))
        );
        $bonus_type = Model('bonus_type');
        $bonus_type->save($bonus_value, $type_id);
        if (intval($_POST['send_type']) === 1) {
            $bonus_file = $_FILES['bonus_file'];
            if ($bonus_file && !empty($bonus_file['name'])) {
                if ($bonus_file['tmp_name'] == "") {
                    $this->setError('上传失败，请联系服务器管理员。');
                    return false;
                }
                $bonus_file_content = file_get_contents($bonus_file['tmp_name']);
                $bonus = Model('user_bonus');
                $bonus->loadFile($bonus_file_content);
            }
        }
        $this->bonus_typeOp();
    }

    private function bonus_form($id = 0)
    {
        Tpl::output('id', $id);
        if ($id > 0) {
            $bonus_type = Model('bonus_type');
            $bonus_type_data = $bonus_type->get($id);
            Tpl::output('data', $bonus_type_data[0]);
        }
        Tpl::showpage('bonus.bonus_type_form');
    }

    private function bonus_add_random_form($id = 0)
    {
        Tpl::output('id', $id);
        if ($id > 0) {
            $bonus_type = Model('bonus_type');
            $bonus_type_data = $bonus_type->get($id);
            Tpl::output('data', $bonus_type_data[0]);
        }
        Tpl::showpage('bonus.bonus_type_add_random_form');
    }

    private function show_menu($menu_type, $menu_key = '')
    {
        $menu_array = array();
        switch ($menu_type) {
            case 'bonus':
                $menu_array = array(
                    1 => array('menu_key' => 'bonus_type_list', 'menu_name' => '已发红包', 'menu_url' => 'index.php?act=bonus&op=bonus_type'),
                    2 => array('menu_key' => 'bonus_type_add', 'menu_name' => '添加定向红包', 'menu_url' => 'index.php?act=bonus&op=bonus_type_add'),
                    3 => array('menu_key' => 'bonus_type_add_random', 'menu_name' => '添加随机红包', 'menu_url' => 'index.php?act=bonus&op=bonus_type_add_random'),
                );
                break;
            case 'type':
                $menu_array = array(
                    1 => array('menu_key' => 'bonus_type_edit', 'menu_name' => '编辑红包', 'menu_url' => 'index.php?act=bonus&op=bonus_type_edit')
                );
                break;
        }
        Tpl::output('menu', $menu_array);
        Tpl::output('menu_key', $menu_key);
    }
}