<?php
defined('IN_TS') or die('Access Denied.');

switch ($ts) {
    case "":
        break;
    case"add_item":
        echo"add item\n";
        
        $item_suk=tsTitle($_POST['SUK']);
        $platform=tsTitle($_POST['platform']);
        $salestart=tsTitle($_POST['salestart']);
        $mainphoto=tsTitle($_POST['mainphoto']);
        


        //这个phpthinks 会将所有_POST 里面的所有引号加反斜杠，需要使用stripslashes去反斜杠
        $names=json_decode(stripslashes($_POST['names']));
        $tags=json_decode(stripslashes($_POST['tags']));
        

        $item = $new ['pricetrend']->find ( 'pricetrend_item', array (
            'SUK'=>$item_suk,
			'platform'=>$platform,
            'salestart'=>$salestart,
            ) );

        if(!$item)
        {
            $itemid = $new['pricetrend']->create('pricetrend_item',array(
                'SUK'=>$item_suk,
                'platform'=>$platform,
                'salestart'=>$salestart,
                'uploaddate'=> date("Y-m-d H:i:s"),           
            ));
            $arrUpload = tsUpload($_FILES['mainphoto'],$itemid,'pricetrend',array('jpg','png','jpeg','gif'));
            $new['pricetrend'] -> update('pricetrend_item', array('SUK' => $item_suk), 
            array(
                'mainphoto' => $arrUpload['url'],
                ));
            
        }else
        {
            $itemid=$item['id'];
        }

        //将名字写进数据库，并对应itemid
        foreach($names as $name)
        {
            $tempname =json_decode(json_encode($name), true);  
            $tempname['name']=tsTitle($tempname['name']);
            $tempname['region']=tsTitle($tempname['region']);

            $sqlname = $new ['pricetrend']->find ( 'pricetrend_item_name', array (
                'item_id'=>$itemid,
                'name'=>$tempname['name'],
                'region'=>$tempname['region'],
                ) );
            if(!$sqlname)
            {
                $new ['pricetrend']->create ( 'pricetrend_item_name', array (
                    'item_id'=>$itemid,
                    'name'=>$tempname['name'],
                    'region'=>$tempname['region'],
                    ) );
            }
        }
        
        // 处理标签
        foreach($tags as $tag)
        {
		    aac ( 'tag' )->addTag ( 'pricetrend', 'id', $itemid, $tag );
        }

        
        //print_r($itemid);

        break;

    case "add_more":
        echo "add more\n";
        $item_suk=tsTitle($_POST['SUK']);
        $price=tsTitle($_POST['price']);
        $const=tsIntval($_POST['const']);
        $price_type=tsTitle($_POST['price_type']);
        $sources=tsTitle($_POST['sources']);
        $shop=tsTitle($_POST['shop']);
        $platform=tsTitle($_POST['platform']);
        $salestart=tsTitle($_POST['salestart']);
        //先获取item id
        $item = $new ['pricetrend']->find ( 'pricetrend_item', array (
            'SUK'=>$item_suk,
			'platform'=>$platform,
            'salestart'=>$salestart,
            ) );

        if(!$item)
        {
            $itemid = $new['pricetrend']->create('pricetrend_item',array(
                'SUK'=>$item_suk,
                'platform'=>$platform,
                'salestart'=>$salestart,
                'uploaddate'=> date("Y-m-d H:i:s"),           
            ));
            
        }else
        {
            $itemid=$item['id'];
        }
        //再获取shop id
        $shopit = $new ['pricetrend']->find ( 'pricetrend_shop', array (
            'name'=>$shop,

            ) );

        if(!$shopit)
        {
            $shopid = $new['pricetrend']->create('pricetrend_shop',array(
                'name'=>$shop,

                'uploaddate'=> date("Y-m-d H:i:s"),           
            ));
            
        }else
        {
            $shopid=$shopit['id'];
        }

        $new['pricetrend']->create('pricetrend_item_price',array(
            'item_id'=>$itemid,
            'shop_id'=>$shopid,
            'price_type'=>$price_type,
            'price'=> $price,    
            'sources'=>$sources,
            'uploaddate'=>date("Y-m-d H:i:s"),       
        ));
        $new['pricetrend']->create('pricetrend_item_const',array(
            'item_id'=>$itemid,
            'shop_id'=>$shopid,
            'const'=>$const,
            'uploaddate'=>date("Y-m-d H:i:s"),       
        ));       
        echo "ok";
        break;
}