<?php
/**
 * 后台外卖活动控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <xiaol@sina.cn>
 */

namespace Admin\Controller;
class TakeoutPromoteController extends BaseController
{
    public function index(){
        $id=I('id');
        if(empty($id)){
            $id=1;
        }
        $where=array();$where['id']=$id;
        $promote=M(CONTROLLER_NAME)->where($where)->find();
        $config_info=unserialize($promote['config']);
        $this->assign('config_info',$config_info);
        if($promote['shop_id']){
            $shopids = array_filter(explode(',', $promote['shop_id']));
            foreach($shopids as $k=>$v){
                $promote['shop_arr'][$v] = idtoname($v,'Shops','name');
            }
            $promote['shop_check'] = implode(',',array_keys($promote['shop_arr']));
        }
        $this->assign($promote);
        $shopsData = getShops();
		$this->assign('shopsData',$shopsData);
        $this->display();
    }
    public function mj_edit(){
        $id=I('id');
        if(empty($id)){
            $id=2;
        }
        $where=array();$where['id']=$id;
        $promote=M(CONTROLLER_NAME)->where($where)->find();
        $config_info=unserialize($promote['config']);
        $this->assign('config_info',$config_info);
        if($promote['shop_id']){
            $shopids = array_filter(explode(',', $promote['shop_id']));
            foreach($shopids as $k=>$v){
                $promote['shop_arr'][$v] = idtoname($v,'Shops','name');
            }
            $promote['shop_check'] = implode(',',array_keys($promote['shop_arr']));
        }
        $this->assign($promote);
        $shopsData = getShops();
		$this->assign('shopsData',$shopsData);
        $this->display();
    }
    public function update(){
        $id=I('id');$where=array();$where['id']=$id;
        $data=I('post.');$config_info=I('config');$config_data=array();
        foreach($config_info['discount_limit'] as $k=>$v){
            $discount_amount=$config_info['discount_amount'][$k];
            if(empty($v) || empty($discount_amount)){
                unset($config_info['discount_limit'][$k],$config_info['discount_amount'][$k]);
            }else{
                $config_data[$v]=$config_info['discount_amount'][$k];
            }
        }
        $config_data=$config_data?$config_data:$config_info;
        $data['config']=serialize($config_data);
        if($data['shop_ids'])$data['shop_id'] = ','.implode(',',$data['shop_ids']).',';
        if($id==1){
            $jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME . '/index');
        }else if($id==2){
            $jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME . '/mj_edit');
        }
        
        $msg = array_merge(array('success' => L('edit_ok'), 'error' => L('edit_error'), 'url' => $jumpUrl, 'ajax' => IS_AJAX), (array)$msg);
        if(M(CONTROLLER_NAME)->where($where)->save($data)!==false){
            $this->success($msg['success'], $msg['url'], $msg['ajax']);
        }else{
            $this->error($msg['error'], $msg['url'], $msg['ajax']);
        }
    }
	
	public function insert(){
		$id=I('id');
        $data=I('post.');$config_info=I('config');$config_data=array();
		
		$data['id']=$id;
        foreach($config_info['discount_limit'] as $k=>$v){
            $discount_amount=$config_info['discount_amount'][$k];
            if(empty($v) || empty($discount_amount)){
                unset($config_info['discount_limit'][$k],$config_info['discount_amount'][$k]);
            }else{
                $config_data[$v]=$config_info['discount_amount'][$k];
            }
        }
        $config_data=$config_data?$config_data:$config_info;
        $data['config']=serialize($config_data);
		
        if($data['shop_ids'])$data['shop_id'] = ','.implode(',',$data['shop_ids']).',';
		
		$id = M(CONTROLLER_NAME)->add($data);
        
        if($id==1){
            $jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME . '/new_edit');
        }else if($id==2){
            $jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME . '/mj_edit');
        }
        
        $msg = array_merge(array('success' => L('edit_ok'), 'error' => L('edit_error'), 'url' => $jumpUrl, 'ajax' => IS_AJAX), (array)$msg);
        if($id){
            $this->success('添加成功', $msg['url'], $msg['ajax']);
        }else{
            $this->error('添加失败', $msg['url'], $msg['ajax']);
        }
	}

    public function _before_delete()
    {
        $id = I('id');
        if (isset($id) && is_array($id)) {
            foreach ($id as $k => $v) {
                $result = check_delete($v, 8);
                if (!$result['status']) unset($id[$k]);
            }
            if (empty($id)) {
                $this->error($result['info']);
                exit;
            }
        } else {
            $result = check_delete($id, 8);
            if (!$result['status']) {
                $this->error($result['info']);
                exit;
            }
        }
        $this->id = $id;
    }
}
?>