<?php

$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");

$type_arr = array('1'=>'二手房贷款','2'=>'抵押贷款','3'=>'信用贷款','4'=>'租房贷款');
$type_url = array('1'=>'second_hand_house_loan.php','2'=>'mortage_loan.php','3'=>'character_loan.php','4'=>'rent_loan.php'); 

$title = $type_arr[$type_id]."修改";
$loanProduct = new ZsbLoanProduct();
$redirect_url = $type_url[$type_id];
if($action =="modify"){
    
    $data_info = $loanProduct->findById($id);
     //二手房利率处理
    if($type_id==1){        
        $rate = explode('|', $data_info['rate']);
    }elseif($type_id == 2|| $type_id==3){
        $rate = $data_info['rate'];
    }
    $url = $type_url[$type_id];
    if (count($data_info) == 0)
    echo "<script>alert('没有发现您查询的记录');location.href = '".$redirect_url."';</script>";
    $action = "update";
}elseif ($action =="insert") {
    //添加
    //二手房利率处理
    if($type_id==1){    
        $rateArr = array($rate[0],$rate[1]);
        $rate = implode('|', $rateArr);
    }else{
        $rate= $rate;
    }    
    
    $fieldData = array('product'=>$product,'rate'=>$rate,'feature'=>$feature,'type_id'=>$type_id);
    $return_value = $loanProduct->insert($fieldData);
    $url = $type_url[$type_id];
    if($return_value>0){
        echo "<script>alert('添加成功！');location.href = '".$redirect_url."';</script>";
    }else{exit;
        echo "<script>alert('添加失败！');location.href = '".$redirect_url."';</script>";
    }
        
    
}elseif ($action == "update") {
    //二手房利率处理
    if($type_id==1){    
        $rateArr = array($rate[0],$rate[1]);
        $rate = implode('|', $rateArr);
    }else{
        $rate= $rate;
    }    

    $fields = array('id'=>$id,'product'=>$product,'rate'=>$rate,'feature'=>$feature,'type_id'=>$type_id);
  
    $return_value = $loanProduct->saveData($fields);
    if($return_value>0){
        echo "<script>alert('修改成功！');location.href = '".$redirect_url."';</script>";
    }else{
        echo "<script>alert('修改失败！');location.href = '".$redirect_url."';</script>";
    }
    
}elseif ($action == "delete") {
    $return_value = $loanProduct->delBy("id=$id");
    if($return_value>0){
        echo "<script>alert('删除成功！');location.href =  '".$redirect_url."';</script>";
    }else{
        echo "<script>alert('删除失败！');location.href =  '".$redirect_url."';</script>";
    }
    
}elseif ($action == "add") {
    $action = 'insert';
}


include_once(Template("header"));
include_once(Template("loan/loan_modify"));
include_once(Template("footer"));