<?php

namespace Agent\Lib;

use Dever;

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

    # 获取商品列表
    public function goods_api()
    {
        $id = Dever::input('role_id', 1);
        return Dever::load('agent/lib/set')->getData($id);
    }

    # 设置商品
    public function home_api()
    {
        $data = array();
        $data['order_id'] = Dever::input('order_id');
        $data['role_id'] = Dever::input('role_id', 1);
        $data['host'] = Dever::url('lib/order_set.home?id=' . $data['role_id'] . '&order_id=' . $data['order_id'], 'agent');
        $data['url'] = Dever::url('lib/order_set.goods?role_id=' . $data['role_id'] . '&order_id=' . $data['order_id'], 'agent');
        $data['submit'] = Dever::url('lib/order_set.action_cmmmit?json=1', 'agent');

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

    # 设置生产能力
    public function action_cmmmit_api()
    {
        $goods = Dever::input('goods');
        if (!$goods) {
            Dever::alert('请传入商品');
        }
        $goods = Dever::json_decode($goods);
        $order_id = Dever::input('order_id');
        $role_id = Dever::input('role_id');

        $where['option_order_id'] = $order_id;
        $where['set_state'] = 2;
        Dever::db('agent/order_goods')->updates($where);

        foreach ($goods as $k => $v) {
            $type = 1;
            $type_id = $k;
            $sku_id = -1;
            if (strstr($k, '_')) {
                $temp = explode('_', $k);
                $type_id = $temp[0];
                $type = $temp[1];
            } elseif (strstr($k, '-')) {
                $temp = explode('-', $k);
                $type_id = $temp[0];
                $sku_id = $temp[1];
            }
            
            if ($v['total'] <= 0) {
                continue;
            }
            $w = array();
            $w['type'] = $type;
            $w['type_id'] = $type_id;
            $w['sku_id'] = $sku_id;
            $w['order_id'] = $order_id;
            
            $info = Dever::db('agent/order_goods')->one($w);
            if ($v['del'] == 2) {
                if ($info) {
                    Dever::db('agent/order_goods')->update(array('where_id' => $info['id'], 'state' => 2));
                }
            } else {
                $w['num'] = $v['total'];
                
                if (!$info) {
                    Dever::db('agent/order_goods')->insert($w);
                } else {
                    $w['where_id'] = $info['id'];
                    $w['state'] = 1;
                    Dever::db('agent/order_goods')->update($w);
                }

                /*
                $wp = array();
                $wp['order_id'] = $order_id;
                $wp['status'] = 3;
                $process = Dever::db('agent/order_process')->find($wp);
                if ($process) {
                    $process['type'] = explode(',', $process['type']);
                    $wp['audit'] = 1;
                    $wp['type'] = '1,2';
                    Dever::db('agent/order_process')->insert($wp);
                } else {
                    $wp['audit'] = 1;
                    $wp['type'] = '1,2';
                    Dever::db('agent/order_process')->insert($wp);
                }
                */
            }
        }

        return Dever::url('project/database/list&table=order_process&project=agent&order_id='.$order_id.'&page_type=1', 'manage');
    }
}