<?php

/**
 * $Author: wayne $
 * $Id: affiliate.php 17217 2011-01-19 06:29:08Z wayne $
 */

define('IN_ASC', true);
require(dirname(__FILE__) . '/includes/init.php');
admin_priv('affiliate');
$config = get_affiliate();

/*------------------------------------------------------ */
//-- 分成管理頁
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    // assign_query_info();
    // if (empty($_REQUEST['is_ajax']))
    // {
    //     $smarty->assign('full_page', 1);
    // }

    // $smarty->assign('ur_here', $_LANG['affiliate']);
    // $smarty->assign('config', $config);
    $smarty->display('affiliate.htm');
}
elseif ($_REQUEST['act'] == 'get_config'){
    print_r(json_encode(['code'=>200,'data'=>$config]));
}
elseif ($_REQUEST['act'] == 'query')
{
    $smarty->assign('ur_here', $_LANG['affiliate']);
    $smarty->assign('config', $config);
    make_json_result($smarty->fetch('affiliate.htm'), '', null);
}
elseif ($_REQUEST['act'] == 'add')
{
    $request_body = file_get_contents('php://input');
    $config = json_decode($request_body, true);
    put_affiliate($config);
    exit;
    if (count($config['item']) < 5)
    {
        //下線不能超過5層
        $_POST['level_point'] = (float)$_POST['level_point'];
        $_POST['level_money'] = (float)$_POST['level_money'];
        $maxpoint = $maxmoney = 100;
        foreach ($config['item'] as $key => $val)
        {
            $maxpoint -= $val['level_point'];
            $maxmoney -= $val['level_money'];
        }
        $_POST['level_point'] > $maxpoint && $_POST['level_point'] = $maxpoint;
        $_POST['level_money'] > $maxmoney && $_POST['level_money'] = $maxmoney;
        if (!empty($_POST['level_point']) && strpos($_POST['level_point'],'%') === false)
        {
            $_POST['level_point'] .= '%';
        }
        if (!empty($_POST['level_money']) && strpos($_POST['level_money'],'%') === false)
        {
            $_POST['level_money'] .= '%';
        }
        $items = array('level_point'=>$_POST['level_point'],'level_money'=>$_POST['level_money']);
        $links[] = array('text' => $_LANG['affiliate'], 'href' => 'affiliate.php?act=list');
        $config['item'][] = $items;
        $config['on'] = 1;
        $config['config']['separate_by'] = 0;

        put_affiliate($config);
    }
    else
    {
       make_json_error($_LANG['level_error']);
    }

    ecs_header("Location: affiliate.php?act=query\n");
    exit;
}
/*------------------------------------------------------ */
//-- 修改配置
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'update')
{
    $request_body = file_get_contents('php://input');
    $_POST = json_decode($request_body, true);

    $separate_by = (intval($_POST['config']['separate_by']) == 1) ? 1 : 0;

    $_POST['config']['expire'] = (float) $_POST['config']['expire'];
    $_POST['config']['expire_unit'] = 'hour';
    $_POST['config']['level_point_all'] = (float)$_POST['config']['level_point_all'];
    $_POST['config']['level_money_all'] = (float)$_POST['config']['level_money_all'];
    $_POST['config']['level_money_all'] > 100 && $_POST['config']['level_money_all'] = 100;
    $_POST['config']['level_point_all'] > 100 && $_POST['config']['level_point_all'] = 100;

    if (!empty($_POST['config']['level_point_all']) && strpos($_POST['config']['level_point_all'],'%') === false)
    {
        $_POST['config']['level_point_all'] .= '%';
    }
    if (!empty($_POST['config']['level_money_all']) && strpos($_POST['config']['level_money_all'],'%') === false)
    {
        $_POST['config']['level_money_all'] .= '%';
    }

    $_POST['config']['level_register_all'] = intval($_POST['config']['level_register_all']);
    $_POST['config']['level_register_up'] = intval($_POST['config']['level_register_up']);

    $temp = $_POST;

    try{
        put_affiliate($temp);
        print_r(json_encode(['code'=>200,'data'=>'更新成功']));
    }catch(\Exception $e){
        print_r(json_encode(['code'=>500,'data'=>'更新失敗']));
    }

    exit;
}
/*------------------------------------------------------ */
//-- 推薦開關
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'on')
{

    $on = (intval($_POST['on']) == 1) ? 1 : 0;

    $config['on'] = $on;
    put_affiliate($config);
    $links[] = array('text' => $_LANG['affiliate'], 'href' => 'affiliate.php?act=list');
    sys_msg($_LANG['edit_ok'], 0 ,$links);
}
/*------------------------------------------------------ */
//-- Ajax修改設置
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_point')
{

    /* 取得參數 */
    $key = trim($_POST['id']) - 1;
    $val = (float)trim($_POST['val']);
    $maxpoint = 100;
    foreach ($config['item'] as $k => $v)
    {
        if ($k != $key)
        {
            $maxpoint -= $v['level_point'];
        }
    }
    $val > $maxpoint && $val = $maxpoint;
    if (!empty($val) && strpos($val,'%') === false)
    {
        $val .= '%';
    }
    $config['item'][$key]['level_point'] = $val;
    $config['on'] = 1;
    put_affiliate($config);
    make_json_result(stripcslashes($val));
}
/*------------------------------------------------------ */
//-- Ajax修改設置
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_money')
{
    $key = trim($_POST['id']) - 1;
    $val = (float)trim($_POST['val']);
    $maxmoney = 100;
    foreach ($config['item'] as $k => $v)
    {
        if ($k != $key)
        {
            $maxmoney -= $v['level_money'];
        }
    }
    $val > $maxmoney && $val = $maxmoney;
    if (!empty($val) && strpos($val,'%') === false)
    {
        $val .= '%';
    }
    $config['item'][$key]['level_money'] = $val;
    $config['on'] = 1;
    put_affiliate($config);
    make_json_result(stripcslashes($val));
}
/*------------------------------------------------------ */
//-- 刪除下線分成
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'del')
{
    $key = trim($_GET['id']) - 1;
    unset($config['item'][$key]);
    $temp = array();
    foreach ($config['item'] as $key => $val)
    {
        $temp[] = $val;
    }
    $config['item'] = $temp;
    $config['on'] = 1;
    $config['config']['separate_by'] = 0;
    put_affiliate($config);
    ecs_header("Location: affiliate.php?act=list\n");
    exit;
}

function get_affiliate()
{
    $config = json_decode($GLOBALS['_CFG']['affiliate'],true);
    empty($config) && $config = array();

    return $config;
}

function put_affiliate($config)
{
    $temp = json_encode($config);
    try{
        $sql = "UPDATE " . $GLOBALS['ecs']->table('shop_config') .
        "SET  value = '$temp'" .
        "WHERE code = 'affiliate'";
        $GLOBALS['db']->query($sql);
        clear_all_files();
    }catch(\Exception $e){
        throw new Exception('Update Db Error');
    }
}
?>