<?php

namespace Option\Src;

use Dever;
use Option\Lib\Core;

class My extends Core
{
    public function test()
    {
        $id = Dever::input('id');
        $email = Dever::input('email');
        $account = Dever::db('option/account')->find($id);
        if ($account && $account['email']) {
            if ($email) {
                $account['email'] = $email;
            }
            $member = Dever::db('option/member')->find($account['mid']);
            $code = Dever::load('passport/reg')->code(false, false);
            $email = base64_encode($account['email']);
            return Dever::load('option/lib/email.renzheng?aid='.$account['id'].'&email=' . $email . '&code=' . $code . '&username=' . $member['name']);
        }
    }

    /**
     * 获取基本信息
     *
     * @return mixed
     */
    public function getInfo()
    {
        $this->data['user'] = $this->user;

        $this->data['cash'] = Dever::db('option/cash')->getTotal(array('aid' => $this->uid));

        $jiaofu = Dever::db('option/bill_jiaofu')->find(array('status' => 2, 'aid' => $this->uid));

        $this->data['jiaofu_state'] = 2;
        if ($jiaofu) {
            $this->data['jiaofu_state'] = 1;
        }

        $agreement = Dever::db('option/agreement')->find(array('aid' => $this->uid, 'audit' => 3));
        $this->data['agreement_state'] = 2;
        if ($agreement) {
            $this->data['agreement_state'] = 1;
        }

        $agreement = Dever::db('option/agreement')->getOne(array('aid' => $this->uid));

        $audit = array
        (
            'status' => 1,
            'desc' => '',
            'date' => date('Y.m.d H:i'),
        );

        $this->data['audit'] = $audit;

        $total = 0;

        if ($this->data['cash']) {
            foreach ($this->data['cash'] as $k => $v) {
                if ($v['type'] == 1) {
                    $this->data['cash'][$k]['type_name'] = '期权交付价值';
                } else {
                    $this->data['cash'][$k]['type_name'] = '原始期权交付价值';
                }
                $total += $v['daijiaofu'];
                $this->data['cash'][$k]['total'] = number_format($v['jiaofu'] + $v['fafang'] + $v['duifu'], 2, '.', '');
                if ($this->data['cash'][$k]['total'] <= 0) {
                    unset($this->data['cash'][$k]);
                }
            }
            $this->data['cash'] = array_slice($this->data['cash'], 0);
        }

        if ($agreement) {
            if ($agreement['audit'] == 3 && $total > 0) {
                $audit['status'] = 1;
                $audit['desc'] = '';
            } else {
                $audit['status'] = $agreement['audit'];
                $audit['desc'] = $agreement['audit_desc'];
            }
        }
        
        return $this->data;
    }

    # 待交付期权列表
    public function getValue()
    {
        $this->data['list'] = Dever::db('option/cash')->getTotal(array('aid' => $this->uid));

        $agreement = Dever::db('option/agreement')->getOne(array('aid' => $this->uid));

        $audit = array
        (
            'status' => 1,
            'desc' => '',
            'date' => date('Y.m.d H:i'),
        );

        $date = 0;
        $total = $qiquan = $yuanshi = 0;

        if ($this->data['list']) {
            foreach ($this->data['list'] as $k => $v) {
                if ($v['type'] == 1) {
                    $this->data['list'][$k]['type_name'] = '待交付期权';
                } else {
                    $this->data['list'][$k]['type_name'] = '待交付原始期权';
                }
                if ($v['daijiaofu_date'] > $date) {
                    $date = $v['daijiaofu_date'];
                }
                $total += $v['daijiaofu'];

                if ($v['type'] == 1) {
                    $qiquan += $v['daijiaofu'];
                } else {
                    $yuanshi += $v['daijiaofu'];
                }
            }
        }
        if ($date) {
            $audit['date'] = date('Y.m.d H:i', $date);
        }

        if ($agreement) {
            if ($agreement['audit'] == 3 && $total > 0) {
                $audit['status'] = 1;
                $audit['desc'] = '';
            } else {
                $audit['status'] = $agreement['audit'];
                $audit['desc'] = $agreement['audit_desc'];
            }
        }
        
        $total = number_format($total, 2, '.', '');

        $this->data['audit'] = $audit;

        $name = Dever::load('option/lib/agreement')->name(false, $this->user, 1, 2, $date, $total, $qiquan, $yuanshi);
        $this->data['agreement_link'] = $name[1];

        return $this->data;
    }

    # 获取交付列表
    public function getJiaofu()
    {
        $this->data['list'] = Dever::load('option/lib/cash')->getList($this->uid, 'jiaofu', 1, '交付');

        return $this->data;
    }

    # 获取交付详情
    public function getJiaofuView()
    {
        $id = Dever::input('id');
        $this->data['info'] = Dever::load('option/lib/cash')->getView($id, $this->uid, 'jiaofu', '交付');

        return $this->data;
    }

    # 获取发放列表
    public function getFafang()
    {
        $this->data['list'] = Dever::load('option/lib/cash')->getList($this->uid, 'fafang', 1, '发放');

        return $this->data;
    }

    # 获取发放详情
    public function getFafangView()
    {
        $id = Dever::input('id');
        $this->data['info'] = Dever::load('option/lib/cash')->getView($id, $this->uid, 'fafang', '发放');

        return $this->data;
    }

    # 获取兑付列表
    public function getDuifu()
    {
        $this->data['list'] = Dever::load('option/lib/cash')->getList($this->uid, 'duifu', 1, '兑付');

        return $this->data;
    }

    # 获取兑付详情
    public function getDuifuView()
    {
        $id = Dever::input('id');
        $this->data['info'] = Dever::load('option/lib/cash')->getView($id, $this->uid, 'duifu', '兑付');

        return $this->data;
    }

    # 修改资料
    public function setInfo()
    {
        $where = array();
        /*
        $where['name'] = Dever::input('name');
        if (!$where['name']) {
            Dever::alert('请输入姓名');
        }
        $where['idcard'] = Dever::input('idcard');
        if (!$where['idcard']) {
            Dever::alert('请输入身份证号');
        }*/
        $where['idcard_front'] = Dever::input('idcard_front');
        if (!$where['idcard_front']) {
            Dever::alert('请上传身份证正面照片');
        }
        $where['idcard_back'] = Dever::input('idcard_back');
        if (!$where['idcard_back']) {
            Dever::alert('请上传身份证背面照片');
        }

        $where['idcard_path'] = Dever::input('idcard_path');
        if($where['idcard_path']){
            $idcard_path = (array)json_decode($where['idcard_path']);
            if($idcard_path['face']){
                $rest = (array)$idcard_path['face'];
                if($this->user['name'] != $rest['name'] || $this->user['idcard'] != $rest['idNumber']){
                    Dever::alert('姓名或身份证号不匹配');
                }
            }
            if($idcard_path['back']){
                $rest = (array)$idcard_path['back'];
                $date = explode('-',$rest['validPeriod']);
                $cdate = strtotime(str_replace('.','-',$date[1]));
                if(time()>=$cdate){
                    Dever::alert('身份证背面已过期');
                }
            }
        }

        $where['email'] = Dever::input('email');
        if (!$where['email']) {
            Dever::alert('请填写邮箱');
        }
        $check = Dever::db('option/account')->find(array('email' => $where['email']));
        if ($check && $check['id'] != $this->uid) {
            Dever::alert('邮箱已被验证,请重新填写');
        }
        if ($where['email'] != $this->user['email']) {
            $where['is_email'] = 2;
        }

        if ($where) {
            $where['where_id'] = $this->uid;
            $where['audit'] = 2;
            $where['status'] = 2;
            $where['is_idcard'] = 1;
            $where['up_date'] = time();
            Dever::db('option/account')->update($where);

            Dever::db('option/member')->update(array('where_id' => $this->user['mid'], 'idcard_bind' => 1));
        }

        if (isset($where['is_email']) && $where['is_email'] == 2) {
            $code = Dever::load('passport/reg')->code(false, false);
            $email = base64_encode($where['email']);
            //Dever::daemon('lib/email.renzheng?aid='.$this->uid.'&email=' . $email . '&code=' . $code . '&username=' . $this->user['name'], 'option');

            Dever::load('option/lib/email.renzheng?aid='.$this->uid.'&email=' . $email . '&code=' . $code . '&username=' . $this->user['name']);
        }

        return Dever::db('option/account')->find(array('id' => $this->uid, 'clear' => true));
    }

    # 确认签署
    public function act_commit()
    {
        Dever::config('base')->hook = true;
        $update['sign'] = Dever::input('sign');
        if (!$update['sign']) {
            Dever::alert('请上传签名');
        }

        $data = Dever::db('option/cash')->select(array('aid' => $this->uid));

        if (!$data) {
            Dever::alert('没有待交付的期权价值');
        }

        $date = 0;
        $total = $qiquan = $yuanshi = 0;
        if ($data) {
            foreach ($data as $k => $v) {
                if ($v['daijiaofu_date'] > $date) {
                    $date = $v['daijiaofu_date'];
                }
                $total += $v['daijiaofu'];
                if ($v['type'] == 1) {
                    $qiquan += $v['daijiaofu'];
                } else {
                    $yuanshi += $v['daijiaofu'];
                }
            }
        }
        $total = number_format($total, 2, '.', '');

        # 签署合同
        $this->user['sign'] = $update['sign'];
        $agreement_id = Dever::load('option/lib/agreement')->up($this->user, $date, $total, $qiquan, $yuanshi);

        return $this->user;
    }

    # 获取期权协议内容
    public function getAgreementContent()
    {
        return 'test';
    }

    # 获取我的合同
    public function getAgreement()
    {
        $where['aid'] = $this->uid;
        $where['audit'] = 3;
        $this->data['list'] = Dever::db('option/agreement')->getAll($where);

        if ($this->data['list']) {
            $status = Dever::db('option/agreement')->config['status'];
            foreach ($this->data['list'] as $k => $v) {
                $this->data['list'][$k]['status_name'] = $status[$v['status']];
                $this->data['list'][$k]['qdate_string'] = '';
                if ($v['qdate']) {
                    $this->data['list'][$k]['qdate_string'] = date('Y.m.d', $v['qdate']);
                }

                $name = Dever::load('option/lib/agreement')->name($v['agreement_id'], $v, 2, 2);
                $this->data['list'][$k]['link'] = $name[1];
            }
        }

        return $this->data;
    }

    # 获取我的合同
    public function getAgreementView()
    {
        $where['aid'] = $this->uid;
        $where['id'] = Dever::input('id');
        $this->data['info'] = array();
        if ($where['id']) {
            $status = Dever::db('option/agreement')->config['status'];
            $this->data['info'] = Dever::db('option/agreement')->find($where);
            if ($this->data['info']) {
                $this->data['info']['status_name'] = $status[$this->data['info']['status']];
                $this->data['info']['qdate_string'] = '';
                if ($this->data['info']['qdate']) {
                    $this->data['info']['qdate_string'] = date('Y.m.d', $this->data['info']['qdate']);
                }
                
                $name = Dever::load('option/lib/agreement')->name($this->data['info']['agreement_id'], $this->data['info'], 2, 2);
                $this->data['info']['link'] = $name[1];
            }
        }

        return $this->data;
    }
}