<?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){
            $update['where_id'] = $id;
            $update['set_mid'] = $info['id'];
            $update['set_role'] = $info['role'];
            if (!$soft['order_num']) {
                $update['set_order_num'] = $this->getOrderId($id);
            }
            
            if($cash){
                $update['set_cash'] = $cash;
            } else {
                $cash = $soft['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){
                    $update['set_status'] = 3;
                    $update['set_zdate'] = time();
                    $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);
                }
        	}
            
            if (!$audit) {
                if ($soft['status'] == 4) {
                    $update['status'] = 2;
                }
            }
            $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',$info['buy_date']).'<br/>'.date('Y-m-d',$info['cdate']);
        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>';
        }
        $picture = explode(',',$info['pic']);
        $pic = '';
        foreach ($picture as $k => $v){
            $pic .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" 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' => '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 '';
        }
    }
    public function list($id){
        $info = Dever::db('agent/soft_cash')->find(array('id'=>$id));
        $config_status = Dever::db('agent/soft_cash')->config['config_status'];
        $data['status'] = Dever::status($config_status,$info['status'],true);
        $config_type = Dever::db('agent/soft_cash')->config['config_type'];
        $data['type'] = Dever::status($config_type,$info['type']);
        if($info['zdate']){
            $data['zdate'] = date('Y-m-d H:i',$info['zdate']);
        }else{
            $data['zdate'] = '-';
        }
        if($info['fdate']){
            $data['fdate'] = date('Y-m-d H:i',$info['fdate']);
        }else{
            $data['fdate'] = '-';
        }
        // print_R($data);die;
        $html = $data['status'];
        $html .= '<br />' . $data['type'];
        $html .= '<br />' . $data['zdate'];
        $html .= '<br />' . $data['fdate'];
        return $html; 
    }

    #数据导出
    public function out_soft_cash_api($data){
        $header = array('合同乙方', '手机号码', '合同总期数（年）', '合同类型', '签订日期', '合同金额', '购买日期', '录入日期', '终审日期');
        $body = array();
        $agreement_name = '';
        foreach($data as $k => $v){
            $member = Dever::db('agent/member')->find($v['mid']);
            $role = Dever::db('setting/role')->find($member['role']);
            $agreement = Dever::db('agent/member_agreement')->find(array('id'=>$v['agreement_id'],'mid'=>$v['mid']));
            if($agreement && $agreement['agreement_id']){
                $name = Dever::db('setting/agreement')->find(array('id'=>$agreement['agreement_id']));
                $config_type= Dever::db('setting/agreement')->config['config_type'];
                $agreement_name = Dever::status($config_type,$name['type']);
            }
            $type = 2;
            # 这里加判断
            if ($role['type'] == 2 && $role['area'] == 1) {
                $type = 1;   
            } elseif ($v['cash'] > 6000 && $v['cash'] <= 30000 && $v['cash']%5000 == 0) {
                $type = 3;
            } elseif ($v['cash'] <= 6000 && $v['cash']%1000 == 0) {
                $type = 4;
            } else {
                $type = 2;
            }
            # 城市代理
            if ($type == 1) {
                $name = '城市代理版';
                $year = 1;
            }
            # 定制版
            elseif ($type == 2) {
                $name = '定制版';
                $year = 1;
            }
            # 升级版
            elseif ($type == 3) {
                $name = '升级版';
                $year = intval($v['cash']/5000);
                if ($year > 6) {
                    $year = 6;
                }
            }
            # 基础版
            else {
                $name = '基础版';
                $year = intval($v['cash']/1000);
                if ($year > 6) {
                    $year = 6;
                }
            }
            if($v['fdate']){
                $fdate = date('Y-m-d H:i',$v['fdate']);
            }else{
                $fdate = '-';
            }
            if($v['buy_date']){
                $buy_date = date('Y-m-d H:i',$v['buy_date']);
            }else{
                $buy_date = '-';
            }
            if($v['zdate']){
                $zdate = date('Y-m-d H:i',$v['zdate']);
            }else{
                $zdate = '-';
            }
            if($v['cdate']){
                $cdate = date('Y-m-d H:i',$v['cdate']);
            }else{
                $cdate = '-';
            }
            $d = array
            (
                $member['name'],
                $v['mobile'],
                $year,
                $name,
                $fdate,
                $v['cash'],
                $buy_date,
                $cdate,
                $zdate,
            );
            $body[] = $d;
        }
        $file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }

    public function update_role_api(){
        $data = Dever::db('agent/soft_cash')->state();
        foreach($data as $k => $v){
            if(!$v['role']){
                $member = Dever::db('agent/member')->find($v['mid']);
                $where['where_id'] = $v['id'];
                $where['set_role'] = $member['role'];
                Dever::db('agent/soft_cash')->update($where);
            }
            if(!$v['fdate'] && $v['status'] == 3){
                $agreement = Dever::db('agent/member_agreement')->find(array('id'=>$v['agreement_id'],'mid'=>$v['mid']));
                $where['where_id'] = $v['id'];
                $where['set_type'] = $agreement['status'];
                $where['set_zdate'] = $agreement['qdate'];
                $where['set_fdate'] = $agreement['qdate'];
                Dever::db('agent/soft_cash')->update($where);
            }
        }
    }
}