<?php

namespace Agent\Lib;

use Dever;

Class Soft_cash
{
	 public function updateSoft($id, $name, $data){
	 	// print_R($data);die;
        $mobile = Dever::param('mobile', $data);
        $cash = Dever::param('cash', $data);
        $price = Dever::param('price', $data);
        $pic = Dever::param('pic', $data);
        $status = Dever::param('status', $data);
        $audit = Dever::param('audit', $data);
        $audit_desc = Dever::param('audit_desc', $data);
        $cdate = Dever::param('cdate',$data);
        $buy_cdate = Dever::param('buy_cdate',$data);

        $soft = Dever::db('agent/soft_cash')->one($id);
        if($soft['mid']<0){
        	$info = Dever::db('agent/member')->find(array('mobile'=>$soft['mobile']));
        }else{
        	$info = Dever::db('agent/member')->find(array('id'=>$soft['mid']));
        }
        
        if($info){
            // print_R($info);die;
            $update['where_id'] = $id;
            $update['set_mid'] = $info['id'];
            $update['set_order_num'] = $this->getOrderId($id);
            if($cash){
                $update['set_cash'] = $cash;
            }
            if($price){
                $update['set_price'] = $price;
            }
            if($pic){
                $update['set_pic'] = $pic;
            }
            if($status){
                $update['set_status'] = $status;
            }
             if($audit){
                $update['set_audit'] = $audit;
            }
            if($audit_desc){
                $update['set_audit_desc'] = $audit_desc;
            }
            
           	if($soft){
               
                if($audit == 3){
                    $update['set_status'] = 4;
                }elseif($audit == 2 && $soft['audit'] == 3){
                    $update['set_status'] = 3;
                }elseif($soft['audit'] == 2 && $soft['status'] == 1){
	            	$update['set_status'] = 2;
	            }elseif($soft['audit'] == 2 && $soft['status'] == 2){
	            	$update['set_status'] = 3;
	            }elseif($soft['audit'] == 2 && $soft['status'] == 4){
                    $update['set_status'] = 3;
                }
                // print_R($update);die;
	            if($soft['audit']== 2 && $update['set_status'] == 3){
	                $order['role'] = $info['role'];
	                $order['soft_cash'] = $cash;
	                $order['price'] = $cash;
	                $order['cdate'] = $cdate;
	                $order['type'] = 3;
	                $order['sign'] = '';
	                $order['name'] = $info['name'];
	                $order['mobile'] = $info['mobile'];
	                $order['mid'] = $info['id'];
	                $order['id'] = $id;
	                $order['area'] = $info['area'];
	                $order['idcard'] = $info['idcard'];
	                $order['idcard_front'] = $info['idcard_front'];
	                $order['idcard_back'] = $info['idcard_back'];
	                $order['company_name'] = $info['company_name'];
	                $order['company_pic'] = $info['company_pic'];
	                // print_R($order);die;
	                $update['set_agreement_id'] = Dever::load('agent/lib/agreement')->up($order,1,3);
	            }
        	}
            
            $ids=Dever::db('agent/soft_cash')->update($update);
        }
    }

    public function getOrderId($id)
    {
        $order = Dever::order();
        $info = Dever::db('agent/soft_cash')->one(array('order_num'=>$order));
        if ($info) {
            return $this->createOrderId($id);
        } else {
            return $order;
        }
    }
    public function soft_show($id){
        $info = Dever::db('agent/soft_cash')->find($id);
        $html['num'] = $info['order_num'].'</br>'.date('Y-m-d H:i',$info['buy_date']);
        return $html;
    }
	public function show(){
        $id = Dever::input('id');
        $info = Dever::db('agent/soft_cash')->find($id);
        $member = Dever::db('agent/member')->find($info['mid']);
        $type = Dever::db('agent/member')->config['config_type'];
        $config_status = Dever::db('agent/soft_cash')->config['config_status'];

        $status = Dever::status($config_status,$info['status']);
         // print_R($status);die;
        $buy_date = date('Y-m-d H:i',$info['buy_date']);
        $role = Dever::db('setting/role')->one($member['role']);
        $area = Dever::load("area/api.string", $member['area']);
        $idcard_pic = '<a href="'.$member['idcard_front'].'" target="_blank"><img src="'.$member['idcard_front'].'" width="100" /></a>';
        if ($member['idcard_back']) {
            $idcard_pic .= '<a href="'.$member['idcard_back'].'" target="_blank"><img src="'.$member['idcard_back'].'" width="100" /></a>';
        }
        $pic = '<a href="'.$info['pic'].'" target="_blank"><img src="'.$info['pic'].'" width="100" /></a>';
        $result['订单信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('订单号', $info['order_num']),
                    array('订单状态', $status),
                    array('购买时间', $buy_date),
                ),
                array
                (
                    array('备注', $info['audit_desc']),
                    array('管理员备注', $info['admin_desc']),
                    // array('购买时间', $buy_date),
                ),
            ),
        );
        $result['代理商信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('手机号', $member['mobile']),
                    array('名称', $member['name']),
                    array('类型', $type[$member['type']]),
                ),
                array
                (
                    array('代理商角色', $role['name']),
                    array('代理区域', $area),
                    array('身份证号', $member['idcard']),
                ),
                array
                (
                	array('身份证照片', $idcard_pic),
                ),
            ),
        );
        $result['费用信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('软件服务费', $info['cash']),
                    array('期权价值', $info['price']),
                ),
                 array
                (
                	array('支付凭证', $pic),
                ),
            ),
        );
        $button = $this->show_button($info);

        if($info['status'] == 3 ){
        	$member_goods = Dever::db('agent/member_agreement')->find($info['agreement_id']);
        	if ($member_goods && $member_goods['sign']) {
            $sign_pic = '<a href="'.$member_goods['sign'].'" target="_blank"><img src="'.$member_goods['sign'].'" width="100" /></a>';
        } else {
            $sign_pic = '暂无';
        }
        
        	$this->show_agreement($info, $role, $sign_pic, $result);
        }
        $head = array
        (
            'name' => '基本信息',
            'btn' => $button,
        );
         
        // $foot = $button;
        $html = Dever::show($head, $result);
        return $html;
    }
    public function show_button($info){
    	$button = array();
    	$purl = Dever::url('project/database/update?project=agent&table=soft_cash&where_id='.$info['id'].'&col=audit_desc', 'manage');
        $button[] = array
        (
            'type' => 'edit',
            'link' => $purl,
            'name' => '备注',
        );
        $purl = Dever::url('project/database/update&project=agent&&page_type=2&table=soft_cash&menu=agent&search_option_state=1&where_id='.$info['id']);
        if($info['status'] < 3 || $info['status'] == 4){
            $button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '修改信息',
            );
            
        }
        $purl = Dever::url('project/database/update?project=agent&table=soft_cash&where_id='.$info['id'].'&audit=2&col=id,audit,audit_desc', 'manage');
        // if($aduit)
        if($info['status'] == 1){
        	$button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '待业务初审',
            );
        }
        $purl = Dever::url('project/database/update?project=agent&table=soft_cash&where_id='.$info['id'].'&audit=2&col=id,audit,audit_desc', 'manage');
        if (Dever::load('manage/auth')->checkFunc('agent.soft_cash', 'edit1', '待财务终审')) {
	        if($info['status'] == 2){
	        	$button[] = array
	            (
	                'type' => 'edit',
	                'link' => $purl,
	                'name' => '待财务终审',
	            );
	        }
	    }
        
        $purl = Dever::url('project/database/update?project=agent&table=soft_cash&where_id='.$info['id'].'&audit=3&col=id,audit,audit_desc', 'manage');
        if (Dever::load('manage/auth')->checkFunc('agent.soft_cash', 'edit2', '驳回')) {
	        if($info['status'] == 4){
	        	$button[] = array
	            (
	                'type' => 'edit',
	                'link' => $purl,
	                'name' => '待财务终审',
	            );
	        }
	    }
        $button[] = array
        (
            'type' => 'link',
            'link' => 'refer',
            'name' => '返回上一页',
        );
        return $button;
    }
    private function show_agreement($info, $role, $sign_pic, &$result)
    {
        $table = array();
        $table['head'] = array('合同类型', '合同名称', '合同签名', '合同状态');
        $table['body'] = array();
        $agreement_type = Dever::db('setting/agreement')->config['config_type'];
        $agreement_status = Dever::db('agent/member_agreement')->config['status'];
        if($info['status'] == 3 && $info['agreement_id']){
        	$agreement_title = '(已生成合同)';
        	$agreement_button = array();
        	$agreement = Dever::db('agent/member_agreement')->find(array('id' => $info['agreement_id']));
        	if($agreement){
        		$agreement_info = Dever::db('setting/agreement')->find($agreement['agreement_id']);
        		// print_r($agreement_info);die;
        		$agreement_status_name = Dever::status($agreement_status, $agreement['status']);
        		// echo dever::sql();die;
        		$table['body'] = array(Dever::status($agreement_type, $agreement_info['type']), Dever::load('agent/lib/agreement')->name($agreement_info, $agreement, $role, 2), $sign_pic, $agreement_status_name);
        	}else{
        		$agreement_title = '';
        	}
        }

        $result['生成软件服务合同' . $agreement_title] = array
        (
            'type' => 'table',
            'head' => array('name' => '&nbsp;', 'btn' => $agreement_button),
            'content' => $table
        );
    }
    public function insertSoft($id,$name,$data){
        $mobile = Dever::param('mobile',$data);
        $member = Dever::db('agent/member')->find(array('mobile'=>$mobile));
        if(!$member){
            Dever::alert('代理商不存在');
        }
    }
    public function search_api()
    {
        $value = Dever::input('value');
        if(!preg_match('/^1[3|5|6|7|8|9]\d{9}$/', $value)){
                return '代理商手机号不正确';
            }
        if ($value) {
            $data = Dever::db('agent/member')->one(array('mobile' => $value,'state' =>1));
            $table = array();
            if ($data) {
                $table = array();
                $table['head'] = array('序列号', '姓名', '手机号');
                $table['body'] = array();
                $table['body'] = array($data['id'], $data['name'], $data['mobile']);
                // foreach ($data as $k => $v) {
                //     $member = Dever::db('agent/member')->find($v['mid']);
                //     $table['body'][] = array($k+1, $member['name'], $v['mobile']);
                // }

                $result[] = array
                (
                    'type' => 'table',
                    'content' => $table,
                );

                $html = Dever::show('', $result, false, false);
                return $html;
            }else{
                return '代理商不存在';
                // Dever::alert('代理商不存在');
            }
            // Dever::alert('暂无账号');
            // return '暂无账号';
        } else {
            return '';
        }
    }
}