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

if($do=='huifu')
  {
  		
        $sql = "update broker_certificate set valid_flag='1' and  reason='' where id = '".$certificate_id."'";
       
        $db->Execute($sql);
        $sql = "select * from admin_broker_check_log where id = '".$id."'";
        
	    $log = $dbback->GetRow($sql);
        $arr['dateline'] = time();
        $arr['account'] = $_SESSION[$web_auth]["username"];
        $arr['valid_flag'] = 1;
        $arr['certificate_id'] = $log['certificate_id'];
        $arr['uid'] = $log['uid'];
		$arr['agentcode'] = $log['agentcode'];
		$arr['type'] = $log['type'];
		$arr['pic'] = $log['pic'];
		$q = insert_sql("admin_broker_check_log",$arr);
        $mark=$db->Execute($q);
        if($mark)
            {
             echo 1;
            }else
            {
             echo 2;
            }
  }

  if($do=='getbrokerdetail')
  {
  		
     $q = "select * from broker where uid=$uid";
     $arr = $dbback->GetAll($q);
     $q1 = "select agentshortname,agentcode from agency where agentcode='" . $arr[0]["agentcode"] . "'";
     $agentshortname = $db->GetRow($q1);
     $arr[0]['agentshortname'] = $agentshortname['agentshortname'];
     encodeUTF8($arr);
     print json_encode($arr[0]);
	 exit;
  }

  //公共函数
function encodeUTF8(&$array) {
    foreach ($array as $key => $value) {
        if (!is_array($value)) {
            $array[$key] = iconv("GBK", "UTF-8", $value);
        } else {
            encodeUTF8($array[$key]);
        }
    }
}


  if($do=='broker_modify')
  {
    //当天时间戳
    $expiretime = strtotime(date('Y-m-d'));
    //判断数据是否正确
    if (trim($usernametc) == "") {
        echo 3;
        exit;
    }
    if ($passwordtc == "") {
        echo 4;
        exit;
    }

    /*客户名是否重复*/
    $q = " select username from broker ";
    $q.=" where username='" . qstr($usernametc) . "'";
    $q.=" and   uid<>" . intval($uidtc);
    $rs = &$db->GetRow($q);
    if (count($rs)!=0){echo 5;exit;}

    if ($opened == 1 && $regdatetc == '0000-00-00') {
        $tt["regdate"] = date("Y-m-d", time());
    }

    $uid = iconv("utf-8","gb2312",$uidtc);
    $tt = array();
  	$tt['agentcode'] = iconv("utf-8","gb2312",$agentcodetc);
	$tt['companyname'] = iconv("utf-8","gb2312",$companynametc);
	$tt['dist'] = iconv("utf-8","gb2312",$disttc);
    $tt['idno'] = iconv("utf-8","gb2312",$idnotc);
    $tt['is_pay'] = iconv("utf-8","gb2312",$is_paytc);
    $tt['master'] = iconv("utf-8","gb2312",$mastertc);
    $tt['password'] = iconv("utf-8","gb2312",$passwordtc);
    $tt['remark'] = iconv("utf-8","gb2312",$remarktc);
    $tt['telno'] = iconv("utf-8","gb2312",$telnotc);
    $tt['truename'] = iconv("utf-8","gb2312",$truenametc);
    $tt['username'] = iconv("utf-8","gb2312",$usernametc);


  if ($opened == 1) {
        if ($tt['expiredate'] != '0000-00-00' && $expiredatetc != '') {
            $expr_array = @explode('-', $expiredatetc);
            if (checkdate($expr_array[1], $expr_array[2], $expr_array[0])) {
                $ss["expiredate"] = $expiredatetc;
                if ($expiretime <= strtotime($ss["expiredate"])) {
                    $valid_flag = 1;
                    $tt["groupid"] = $groupidtc == '' ? 2 : $groupidtc;
                } else {
                    $valid_flag = 0;
                    $tt["groupid"] = $groupidtc == '' ? 2 : $groupidtc; //取消过期分组，改到禁言组
                }
            } else {
                echo "<script>alert('请输入正确的日期!');window.history.go(-1);</script>";
                exit;
            }
        } else {
            $q = " select expiredate from broker_group ";
            $q.=" where  id=" . intval($groupidtc);
            $rew = &$db->GetRow($q);

            $end_date = date("Y-m-d", $expiretime + $rew['expiredate'] * 86400);

            if ($expiretime < $end_date) {
                $valid_flag = 1;
                $ss["groupid"] = $groupidtc == '' ? 2 : $groupidtc;
            } else {
                $valid_flag = 0;
                $ss["groupid"] = $groupidtc == '' ? 2 : $groupidtc; //取消过期分组，改到禁言组
            }
            $ss["expiredate"] = $end_date;
        }
    } else {
        $valid_flag = 0;
    }


  
     $q = "select * from broker where uid='".$uid."' and password='".$tt['password']."'";
     $arr = $dbback->GetRow($q);
    //如果密码被修改
     if(empty($arr))
      {
       $tt['password']=MD5($tt['password']);
      }
	
	$sql = update_sql('broker', $tt, " where uid=" . intval($uid));
	$flg = $db->Execute($sql);
    if($flg)
      {
        echo 1;
      }else{
        echo 2;
      }
  }

if($do=='del')
{
   //删除记录
  $q="delete from broker where uid=".intval($uid);

  $msg=$db->Execute($q);
  if($msg)
    {

    echo "<script>alert('删除成功');location.href='/modulebroker/account_manage.php';</script>";
    }else{
       
    echo "<script>alert('删除失败');location.href='/module/broker/account_manage.php';</script>";
    }
}

    //邮件数据excel统计
if($actdo == "export_data"){
          //查询条件
        $arr[kind] = array('p_2' => '身份认证', 'p_3' => '名片认证', 'p_4_1' => $conf_cityname.'房产经纪人证书', 'p_4_2' => '全国房产经纪人证书', 'p_2_2' => '一寸工作照', 'p_2_3' => "身份证照片");

        $wherecond = "valid_flag = 2";

        $time1 = strtotime($cond_y1.'-'.$cond_m1.'-'.$cond_d1.' 00:00:00');
        $time2 = strtotime($cond_y2.'-'.$cond_m2.'-'.$cond_d2.' 23:59:59');
        $wherecond .= " and (dateline >= $time1 and dateline <= $time2) ";


        if(isset($keyword) && !empty($keyword))
        {
            $wherecond .= " and agentcode like '%".$keyword."%' ";
        }

        //分页参数
        $pagesize	= $display_rows ? intval($display_rows) : 10;
        $page_now	= $page_now ? max(1, intval($page_now)) : 1 ; // 获取当前页数
        $offset		= ( $page_now - 1 ) * $pagesize;

        //获得数据总条数
        $sql = "SELECT COUNT(*) FROM admin_broker_check_log WHERE ".$wherecond;

        $numrows = $dbback->GetOne($sql);

        //获得数据当前页面数据
        $sql = "SELECT * FROM admin_broker_check_log WHERE $wherecond ORDER BY id DESC LIMIT ".$offset.",".$pagesize." ";;

        $data_info = $dbback->GetAll($sql);
        $count_num = count($data_info);

		$export_str = '<html><head><meta http-equiv="Content-type" content="text/html;charset=gbk" /><style type="text/css">.td_border{border-bottom:solid 1px #ccc;border-right:solid 1px #ccc;text-align:center;}</style></head>';
		$export_str .= '<body><TABLE style="border-left:solid 1px #ccc;border-top:solid 1px #ccc;" width="100%" cellspacing = "0" cellspadding = "0" align="center" >';
		$export_str .= '<tr><td class="td_border">编号</td>';
		$export_str .= '<td class="td_border">操作时间</td>';
		$export_str .= '<td class="td_border">操作对象</td>';
		$export_str .= '<td class="td_border">图片类型</td>';
        $export_str .= '<td class="td_border">操作</td>';
		$export_str .= '<td class="td_border">操作人</td></tr>';

		if($count_num >= 1){
			foreach ($data_info as $key=>$value){


				$export_str .= '<tr>';
				$export_str .= "<td height=200 class='td_border'>".$value['uid']."</td>";
				$export_str .= "<td height=200 class='td_border'>".date("Y-m-d H:i:s",$value['dateline'])."</td>";
				$export_str .= "<td height=200 class='td_border'><img src=".$value['pic']." style='width:130px;height:90px;'></td>";
				$export_str .= "<td height=200 class='td_border'>".$arr['kind'][$value['type']]."</td>";
                $export_str .= "<td height=200 class='td_border'>不通过</td>";
                $export_str .= "<td height=200 class='td_border'>".$value['account']."</td>";
				$export_str .= "</tr>";
			}
		}else{
				$export_str .= "<tr><td colspan='6' align='center' class='td_border'>暂无统计数据</td></tr>";
		}

		$export_str .= "</table></body></table>";
		$filename = "日志查询恢复(".date("Y-m-d").").xls";

		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition: attachment; filename=$filename");

		echo $export_str;
		echo "<script>location.replace('".$referer."');</script>";
		exit;
	}

?>