<?php

namespace Agent\Lib;

use Dever;

Class Option_account{
	#列表展示
	public function show($id){
		$info = Dever::db('agent/option_account')->find($id);
		$data =array();
		$data['list'] = $info['name'].'<br/>'.$info['mobile'].'<br/>'.$info['idcard'];
		$count = $info['price']+$info['zh_price']+$info['zj_price']+$info['other_price'];
		$data['price'] = '设备费：'.$info['price'].'<br/>装修费：'.$info['zh_price'].'<br/>租金费：'.$info['zj_price'].'<br/>其他：'.$info['other_price'].'<br/>总计：'.$count;
		$config = Dever::db('agent/option_account')->config['config_status'];
		$status = Dever::status($config,$info['status']);
		if($info['jf_date']){
			$jf_date = date('Y-m-d H:i',$info['jf_date']);
		}else{
			$jf_date = '-';
		}
		$data['date'] = $status.'<br/>'.$jf_date;
		// $audit = Dever::db('agent/option_account')->config['config_audit_type'];
		// $type = Dever::status($audit,$info['audit_type']);
		if($info['audit_type'] == 1){
			$type = '待审核';
		}elseif($info['audit_type'] == 2 || $info['audit_type'] == 3){
			$type = '审核通过';
		}else{
			$type = '审核不通过';
		}
		if($info['audit_date']){
			$audit_date = date('Y-m-d H:i',$info['audit_date']);
		}else{
			$audit_date = '-';
		}
		$data['audit'] = $type.'<br/>'.$audit_date;
		return $data;
	}
	#详情页
	public function list(){
		$id = Dever::input('id');
		$info = Dever::db('agent/option_account')->find($id);
		$type_name = '';
		if($info['type'] == 1){
			$type_name = '零售店';
		}
		$count = $info['price']+$info['zh_price']+$info['zj_price']+$info['other_price'];
		$info_price = '设备费：'.$info['price'].'万元；装修费：'.$info['zh_price'].'万元；租金费：'.$info['zj_price'].'万元；其他：'.$info['other_price'].'万元；总计：'.$count.'万元';
		$area = Dever::load("area/api.string", $info['area']);
		$area = str_replace(',','',$area);
		$image = explode(',',$info['pic']);
		$pic = '';
		foreach($image as $k => $v){
			$pic .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100" /></a>';
		}
		if($info['annex']){
			$pic .= '<a href="'.$info['annex'].'" target="_blank">点此查看</a>';
		}
		// $audit = Dever::db('agent/option_account')->config['config_audit_type'];
		// $audit_type = Dever::status($audit,$info['audit_type']);
		if($info['audit_type'] == 1){
			$audit_type = '待审核';
		}elseif($info['audit_type'] == 2 || $info['audit_type'] == 3){
			$audit_type = '审核通过';
		}else{
			$audit_type = '审核不通过';
		}
        $config = Dever::db('agent/option_account')->config['config_status'];
		$status = Dever::status($config,$info['status']);
		$result = array();

        $result['期权记录详情'] = array
        (
            'type' => 'info',
            'content' => array
            (
            	array
            	(
            		array('期权记录标题', $info['title']),
	                array('记录说明', $info['desc']),
	                array('备注', $info['remark']),
            	),
                array
            	(
            		array('记录类型', $type_name),
	                array('资金记录明细', $info_price),
	                array('行权说明', '行权3年期。自门店注册日期开始计算。满一年30%，满两年35%，满三年35%'),
            	),
            ),
        );
        $result['受益人资料'] = array
        (
            'type' => 'info',
            'content' => array
            (
            	array
            	(
            		array('受益人姓名', $info['name']),
	                array('受益人电话', $info['mobile']),
	                array('受益人身份证号', $info['idcard']),
            	),
                array
            	(
            		array('门店地址', $area.$info['address']),
            	),
            ),
        );
        $result['凭证照片/附件'] = array
        (
            'type' => 'info',
            'content' => array
            (
            	array
            	(
            		array('', $pic),
            	),
            ),
        );
        if($info['audit_type'] == 1){
        	$result['审核结果'] = array
	        (
	            'type' => 'info',
	            'content' => array
	            (
	            	array
	            	(
	            		array('审核状态', $audit_type),
	            		array('交付状态', $status),
	            	),
	            ),
	        );
        }else{
        	$result['审核结果'] = array
	        (
	            'type' => 'info',
	            'content' => array
	            (
	            	array
	            	(
	            		array('审核状态', $audit_type),
	            		array('确认期权数', $info['num']),
	            		array('审核备注', $info['audit_desc']),
	            	),
	            	array
	            	(
	            		array('交付状态', $status),
	            	),
	            ),
	        );
        }

        $button = array();
        

        if (Dever::load('manage/auth')->checkFunc('agent.option_account', 'edit', '备注')) {
            $purl = Dever::url('project/database/update?project=agent&table=option_account&where_id='.$info['id'].'&col=remark', 'manage');
            $button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '备注',
            );
        }
        if (Dever::load('manage/auth')->checkFunc('agent.option_account', 'edit1', '修改资料')) {
            $purl = Dever::url('project/database/update?project=agent&table=option_account&where_id='.$info['id'].'&audit='.$info['audit'].'&col=name,mobile,idcard,area,address,title,remark,type,price,zh_price,zj_price,other_price','manage');

            $button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '修改资料',
            );
        }
        if (Dever::load('manage/auth')->checkFunc('agent.option_account', 'edit2', '财务终审')) {
        	if($info['audit_type'] == 1 && $info['audit'] == 1){
        		$purl = Dever::url('project/database/update?project=agent&table=option_account&where_id='.$info['id'].'&col=id,audit_type,audit,audit_desc,num', 'manage');
	            $button[] = array
	                (
	                    'type' => 'edit',
	                    'link' => $purl,
	                    'name' => '财务终审',
	                );
        	}
            
        }
        if (Dever::load('manage/auth')->checkFunc('agent.option_account', 'edit3', '交付确认')) {
        	if($info['audit_type'] == 2){
        		$purl = Dever::url('project/database/update?project=agent&table=option_account&where_id='.$info['id'].'&audit_type=2&col=id', 'manage');
	            $button[] = array
	                (
	                    'type' => 'action',
	                    'link' => Dever::url('lib/option_account.setStatus?id=' . $info['id'] . '&value=2', 'agent'),
	                    'name' => '交付确认',
	                );
        	}
            
        }
        $head = array
        (
            'name' => '基本资料',
            'btn' => $button,
        );
        $foot = $button;
        $html = Dever::show($head, $result, $foot);

        return $html;
	}
	 public function setStatus_api()
    {
        $where['where_id'] = Dever::input('id');
        $where['set_audit_type'] = 3;
		$where['set_status'] = 2;
		$where['set_jf_date'] = time();
		Dever::db('agent/option_account')->update($where);

        return 'reload';
    }
	public function updateAccount($id,$name,$data){
		
		$audit = Dever::param('audit',$data);
		$num = Dever::param('num',$data);
		$info = Dever::db('agent/option_account')->find($id);
		if($audit){
			if ($audit == 1) {
				$where['where_id'] = $id;
				$where['set_audit_date'] = time();
				$where['set_audit_type'] = 2;
				if($num){
					$where['set_num'] = $num;
				}
				Dever::db('agent/option_account')->update($where);
			}
			
		}
		if(!$audit){
			if($info['audit'] ==2){
				$update['where_id'] = $id;
	            $update['audit'] = 1;
	            Dever::db('agent/option_account')->update($update);
			}
		}
		if($id){
			$admin = Dever::load('manage/auth.data');
			$where['where_id'] = $id;
			$where['audit_admin'] = $admin['id'];
	    	Dever::db('agent/option_account')->update($where);
		}
	}
}