<?php

require_once(dirname(dirname(__FILE__)) . '/app.php');
need_manager();


if($_POST){
    $p = array();
    $p['name'] = strval($_POST['name']);
    $p['brand'] = strval($_POST['brand']);
    $p['channel'] = strval($_POST['channel']);
    $p['province'] = strval($_POST['province']);
    $p['address'] = strval($_POST['address']);
    $p['zipcode'] = strval($_POST['zipcode']);
    $p['tel'] = strval($_POST['tel']);
    $p['remark'] = strval($_POST['remark']);
}


if($_GET){
    if(isset($_GET['id'])){
        $id = $_GET['id'];
        $provider = Table::Fetch('provider', $id);
        
        if(isset($p)){
            $re = Table::UpdateCache('provider', $id, $p);
            if($re){
                Session::Set('success', '供货商信息修改成功！');
                redirect(WEB_ROOT . '/manage/provider_detail.php?id='.$id);
            }else{
                Session::Set('error', '供货商信息修改失败！');
                redirect(WEB_ROOT . '/manage/provider_detail.php?id='.$id);
            }
        }
    }
}else{
    
    if(isset($p)){
        $tb = new Table('provider',$p);
        $re = $tb->Insert(array_keys($p));
        $provider = Table::Fetch('provider', $re);
        if($re){
            Session::Set('success', '供货商信息添加成功！');
            redirect(WEB_ROOT . '/manage/provider_detail.php?id='.$re);
        }else{
            Session::Set('error', '供货商信息添加失败！');
            redirect(WEB_ROOT . '/manage/provider_detail.php');
        }
    }
}

include template('manage_provider_detail');
?>
