<?php

namespace Option\Lib;

use Dever;

class Manage
{
    public function updateMember($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $where['id'] = $id;
        $where['clear'] = true;
        $info = Dever::db('option/member')->find($where);
        if ($info) {
            if ($info['main'] == 1) {
                $update['mid'] = $id;
                $account = Dever::db('option/account')->one($update);
                if (!$account) {
                    $update['audit'] = 1;
                    $aid = Dever::db('option/account')->insert($update);
                } else {
                    $aid = $account['id'];
                }
                Dever::db('option/member')->update(array('where_id' => $info['id'], 'aid' => $aid));
            }

            $main = Dever::db('option/member')->getMain(array('key' => $info['key']));
            if ($main && $main['aid']) {
                $where = array();
                $where['option_key'] = $info['key'];
                $where['set_aid'] = $main['aid'];
                Dever::db('option/member')->updates($where);
            }
        }
    }

    public function updateMain($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $main = Dever::param('main', $data);

        if ($main && $main == 1) {
            $member = Dever::db('option/member')->find($id);
            if ($member) {
                $main_member = Dever::db('option/member')->select(array('main' => 1, 'aid' => $member['aid']));
                if ($main_member) {
                    foreach ($main_member as $k => $v) {
                        if ($v['id'] != $id) {
                            Dever::db('option/member')->update(array('where_id' => $v['id'], 'main' => 2));
                        }
                    }
                }
                $account = Dever::db('option/account')->find($member['aid']);
                if ($account) {
                    Dever::db('option/account')->update(array('where_id' => $account['id'], 'mid' => $member['id']));
                }
            }
        }
    }

    public function updateAccount($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $update = array();
        $audit = Dever::param('audit', $data);
        $desc = Dever::param('audit_desc', $data);
        $send_email = Dever::param('send_email', $data);
        $email = Dever::param('email', $data);
        $info = Dever::db('option/account')->one($id);
        if ($info) {
            if ($audit && $audit > 2) {
                $update['audit_date'] = time();
                $update['where_id'] = $info['id'];
                Dever::db('option/account')->update($update);
            }
            if ($send_email == 2 && (($email && $email != $info['email']) || $info['is_email'] == 2)) {
                $update = array();
                $update['is_email'] = 2;
                $update['where_id'] = $info['id'];
                Dever::db('option/account')->update($update);
                if ($send_email == 2) {
                    $member = Dever::db('option/member')->one($info['mid']);
                    if ($member) {
                        $code = Dever::load('passport/reg')->code(false, false);
                        $email = base64_encode($email);
                        //Dever::daemon('lib/email.renzheng?aid='.$info['id'].'&email=' . $email . '&code=' . $code . '&username=' . $member['name'], 'option');
                        Dever::load('option/lib/email.renzheng?aid='.$info['id'].'&email=' . $email . '&code=' . $code . '&username=' . $member['name']);
                    }   
                }
            }

            $member = Dever::db('option/member')->one($info['mid']);
            if ($audit == 3) {
                if (Dever::project('sms')) {
                    Dever::load('sms/api')->send('qiquan_reg_yes', $member['mobile'], array('name' => $member['name']));
                }
            } elseif ($audit == 4) {
                if (Dever::project('sms')) {
                    Dever::load('sms/api')->send('qiquan_reg_no', $member['mobile'], array('name' => $member['name'], 'content' => $desc));
                }
            }
        }
    }

    # 协议审核
    public function setAgreement($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $update = array();
        $audit = Dever::param('audit', $data);
        $desc = Dever::param('audit_desc', $data);
        if ($audit > 1) {
            $mul_type = Dever::config('base')->mul_type;
            if ($mul_type == 2) {
                $list = Dever::db('option/agreement')->select(array('status' => 2));
            } else {
                $list = explode(',', $id);
            }

            $admin = Dever::load('manage/auth.info');
            if (!$admin) {
                foreach ($list as $k => $v) {
                    Dever::db('option/agreement')->update(array('where_id' => $v, 'audit' => 2));
                }
                
                Dever::alert('请先登录');
            }
            $update['audit_date'] = time();
            $update['audit_admin'] = $admin['id'];
            if ($audit == 3) {
                $update['status'] = 2;
            }

            foreach ($list as $k => $v) {
                $info = Dever::db('option/agreement')->one($v);
                if ($info) {
                    $update['where_id'] = $info['id'];
                    $state = Dever::db('option/agreement')->update($update);
                    if ($state && $audit == 3) {
                        Dever::load('option/lib/cash.setValue_act', $info);
                    }
                    if ($audit == 3) {
                        if (Dever::project('sms')) {
                            Dever::load('sms/api')->send('qiquan_audit', $info['mobile'], array('name' => $info['name']));
                        }
                    } elseif ($audit == 4) {
                        if (Dever::project('sms')) {
                            Dever::load('sms/api')->send('qiquan_audit', $info['mobile'], array('name' => $info['name'], 'content' => $desc));
                        }
                    }
                }
            }
        }
    }

    # 交付审核 已废弃
    public function setJiaofu($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $update = array();
        $audit = Dever::param('audit', $data);
        $info = Dever::db('option/bill_jiaofu')->one($id);
        if ($audit > 1 && $info) {
            $admin = Dever::load('manage/auth.info');
            $update['audit_date'] = time();
            $update['audit_admin'] = $admin['id'];
            $update['where_id'] = $info['id'];
            $state = Dever::db('option/bill_jiaofu')->update($update);
            if ($state && $audit == 3) {
                Dever::load('option/lib/cash.setValue_commit', $info);
            }
        }
    }

    public function setFafang($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $update = array();
        $audit = Dever::param('audit', $data);
        $info = Dever::db('option/bill_fafang')->one($id);
        if ($audit > 1 && $info) {
            $admin = Dever::load('manage/auth.info');
            $update['audit_date'] = time();
            $update['audit_admin'] = $admin['id'];
            $update['where_id'] = $info['id'];
            $state = Dever::db('option/bill_fafang')->update($update);
            if ($state && $audit == 2) {
                # 已作废,减掉这个数据
                $account_cash = Dever::db('option/cash')->find(array('type' => $info['type'], 'aid' => $info['aid']));
                if ($account_cash) {
                    $update['where_id'] = $account_cash['id'];
                    $update['fafang'] = $account_cash['fafang'] - $info['cash'];
                    $update['jiaofu'] = $account_cash['jiaofu'] + $info['cash'];
                    if ($update['fafang'] < 0) {
                        $update['fafang'] = 0;
                    }
                    $update['clear'] = true;
                    Dever::db('option/cash')->update($update);
                }
            }
        }
    }

    public function setDuifu($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $update = array();
        $audit = Dever::param('audit', $data);
        $info = Dever::db('option/bill_duifu')->one($id);
        if ($audit && $audit > 1 && $info) {
            $admin = Dever::load('manage/auth.info');
            $update['audit_date'] = time();
            $update['audit_admin'] = $admin['id'];
            $update['where_id'] = $info['id'];
            $state = Dever::db('option/bill_duifu')->update($update);
            if ($state && $audit == 2) {
                # 已作废,恢复这个数据
                $account_cash = Dever::db('option/cash')->find(array('type' => $info['type'], 'aid' => $info['aid']));
                if ($account_cash) {
                    $update['where_id'] = $account_cash['id'];
                    $update['duifu'] = $account_cash['duifu'] - $info['cash'];
                    $update['fafang'] = $account_cash['fafang'] + $info['cash'];
                    if ($update['duifu'] < 0) {
                        $update['duifu'] = 0;
                    }
                    $update['clear'] = true;
                    Dever::db('option/cash')->update($update);
                }
            }
        }
    }

    public function cashUpdate($id, $name, $data)
    {
        Dever::config('base')->hook = true;
        $aid = Dever::param('aid', $data);
        $mid = Dever::param('mid', $data);
        $type = Dever::param('type', $data);
        $cash = Dever::param('cash', $data);
        $desc = Dever::param('desc', $data);
        if ($aid && $type && $cash && $desc) {
            $account = Dever::db('option/account')->find($aid);
            if ($account && $account['status'] <= 2) {
                Dever::load('option/lib/cash')->up($aid, $mid, $type, $cash, $desc);
            } else {
                Dever::db('option/push_cash')->update(array('where_id' => $id, 'status' => 2));
                Dever::alert('期权账户已停用或者未审核,操作失败');
            }
        }
    }

    public function getDate($id)
    {
        $info = Dever::db('option/agreement')->find($id);
        if ($info) {
            $html = '';
            $html .= '处理时间:' . date('Y-m-d H:i', $info['cdate']);
            if ($info['qdate']) {
                $html .= '<br />确认时间:' . date('Y-m-d H:i', $info['qdate']);
            }
            if ($info['audit_date']) {
                $html .= '<br />审核时间:' . date('Y-m-d H:i', $info['audit_date']);
            }
            return $html;
        }
        return '';
    }

    public function upAgreement_api()
    {
        $id = Dever::input('id');
        if ($id) {
            Dever::load('option/lib/agreement')->upContent($id);
        }
        

        return 'reload';
    }

    public function dropAgreement_api()
    {
        $id = Dever::input('id');
        if ($id) {
            Dever::load('option/lib/agreement')->drop($id);
        }
        

        return 'reload';
    }

    public function checkMember($id, $name, $data)
    {
        $main = Dever::param('main', $data);
        $key = Dever::param('key', $data);
        if (!$key) {
            Dever::alert('请传入账户索引');
        }

        if ($main == 2) {
            $main = Dever::db('option/member')->getMain(array('key' => $key));
            if (!$main) {
                Dever::alert('请先创建主账号');
            }
        } elseif ($main == 1) {
            $main = Dever::db('option/member')->getMain(array('key' => $key));
            if ($main) {
                Dever::alert('已存在主账号');
            }
        }
    }

    public function setAudit($id, $name, $data)
    {
        $audit = Dever::param('audit', $data);
        if ($audit == 3) {
            $data['set_audit_desc'] = 'null';
            return $data;
        } else {
            return $data;
        }
    }
}