<?php

namespace Act\Lib;

use Dever;

# 批量设置商品
class Goods_set
{
	public function __construct()
    {
        Dever::load('manage/auth.init');
    }

    # 获取商品列表
    public function goods_api()
    {
        $result = array();
        $id = Dever::input('discount_id', 1);
        $discount = Dever::db('act/discount')->find($id);
        if (!$discount) {
            return Dever::outDiy($result);
        }
        $where['project_id'] = 1;
        if ($discount['category']) {
            $where['ids'] = $discount['category'];
        }
        $data = Dever::db('category/info')->getTop($where);
        if ($data) {
            $i = 0;
            foreach ($data as $k => $v) {

                $result[$i]['id'] = $v['id'];
                $result[$i]['name'] = $v['name'];
                $result[$i]['select'] = 2;
                $result[$i]['del'] = 1;
                $result[$i]['children'] = array();

                $w['top_category_id'] = $v['id'];
                $goods = Dever::db('goods/info')->select($w);
                if ($goods) {

                    foreach ($goods as $v1) {
                        $children = array
                        (
                            'id' => $v['id'] . '-' . $v1['id'],
                            'name' => $v1['name'],
                            'class_name' => $v['name'],
                            'del' => 1,
                            'select' => 2,
                            'end' => true,
                        );

                        $bind = Dever::db('act/discount_goods')->find(array('discount_id' => $id, 'goods_id' => $v1['id']));

                        if ($bind) {
                            $children['select'] = 1;
                        }
                        $result[$i]['children'][] = $children;
                    }

                    $i++;
                } else {
                    unset($result[$i]);
                }
            }
        }

        return Dever::outDiy($result);
    }

    # 设置商品
    public function home_api()
    {
        $data = array();
        $data['link'] = Dever::decode(Dever::input('refer'));
        $data['discount_id'] = Dever::input('id', 1);
        Dever::setInput('discount_id', $data['discount_id']);
        $data['host'] = Dever::url('lib/goods_set.home?id=' . $data['discount_id'], 'act');
        $data['url'] = Dever::url('lib/goods_set.goods?json=1&discount_id=' . $data['discount_id'], 'act');
        $data['submit'] = Dever::url('lib/goods_set.action_commit?json=1', 'act');

        return Dever::render('setGoods', $data);
    }

    # 设置
    public function action_commit_api()
    {
        $goods = Dever::input('goods');
        if (!$goods) {
            Dever::alert('请传入商品');
        }
        $goods = Dever::json_decode($goods);
        $discount_id = Dever::input('discount_id');

        $where['option_discount_id'] = $discount_id;
        $where['set_state'] = 2;
        Dever::db('act/discount_goods')->updates($where);

        foreach ($goods as $k => $v) {
            $temp = explode('-', $k);
            $cate_id = $temp[0];
            $goods_id = $temp[1];

            $w = array();
            $w['cate_id'] = $cate_id;
            $w['goods_id'] = $goods_id;
            $w['discount_id'] = $discount_id;
            $info = Dever::db('act/discount_goods')->one($w);
            if ($v['del'] == 2) {
                if ($info) {
                    Dever::db('act/discount_goods')->update(array('where_id' => $info['id'], 'state' => 2));
                }
            } else {

                if (!$info) {
                    Dever::db('act/discount_goods')->insert($w);
                } else {
                    $w['where_id'] = $info['id'];
                    $w['state'] = 1;
                    Dever::db('act/discount_goods')->update($w);
                }
            }
        }

        $link = Dever::input('link');
        if ($link) {
            return $link;
        } else {
            return 'reload';
        }
    }
}