<?php

namespace web\admin\ap;

class usr_real_apply {
    static function setwhere($db, $post, $rsuser) {
        $query = $post->get('query', array());
        $csql = new \ciy\sql('ap_usr_real_apply');
        $liid = objint($query, 'liid');
        if ($liid > 0)
            $csql->where('auditstatus', $liid);
        $val = objstr($query, 'vuser');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('ap_user');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('vuser', $trow['id']);
                $query['vuser'] = $trow['name'];
            } else {
                $csql->where('vuser=0');
            }
        }
        $csql->where('name like', objstr($query, 'name'));
        $csql->where('idid like', objstr($query, 'idid'));
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $csql->where('audituser', objstr($query, 'audituser'));
        $csql->wheredaterange('audittimes', objstr($query, 'audittimes'));
        $csql->where('auditmsg like', objstr($query, 'auditmsg'));
        $order = objstr($query, 'order', 'id desc');
        $csql->order($order);
        $query['order'] = $order;
        return [$query, $csql];
    }

    public static function json_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post, $rsuser);
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $mainrowcount = $post->getint('count');
        $mrows = $db->get($csql, $mainrowcount);
        if ($mrows === false)
            return errjson($db->error);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $mrows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'ap_usr_real_apply');
            foreach ($field as $fr => $v) {
                if (get('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array('type' => 'input', 'form' => 'vuser', 'name' => '所属用户', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'input', 'form' => 'name', 'name' => '真实姓名', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'input', 'form' => 'idid', 'name' => '身份证号', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'daterange', 'form' => 'addtimes', 'name' => '申请时间');
            $input[] = array('type' => 'select', 'form' => 'audituser', 'name' => '审核员', 'all' => '全部', 'select' => 'adminuser');
            $input[] = array('type' => 'daterange', 'form' => 'audittimes', 'name' => '审核时间');
            $input[] = array('type' => 'input', 'form' => 'auditmsg', 'name' => '驳回原因', 'prop' => ' style="width:8em;"');
            $ret['searchinput'] = $input;
        }
        $ret['ap_user'] = getrelation($db, $mrows, 'ap_user', 'vuser');
        return succjson($ret);
    }

    public static function json_audit() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p a'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $auditstatus = $post->getint('auditstatus');
        $auditmsg = $post->get('auditmsg');
        if ($auditstatus == 90 && empty($auditmsg))
            return errjson('请填写驳回原因');
        $csql = new \ciy\sql('ap_usr_real_apply');
        $csql->where('id in', $ids);
        $mrows = $db->get($csql);
        $ids = array();
        try {
            $db->begin();
            foreach ($mrows as $mrow) {
                if ($auditstatus == 100) {
                    $updata = array();
                    $updata['truename'] = $mrow['name'];
                    $updata['idid'] = $mrow['idid'];
                    $csql = new \ciy\sql('ap_usr_ext');
                    $csql->where('id', $mrow['vuser']);
                    if ($db->update($csql, $updata) === false)
                        throw new \Exception('用户更新失败:' . $db->error);
                }
                $updata = array();
                $updata['auditstatus'] = $auditstatus;
                $updata['audituser'] = $rsuser['id'];
                $updata['audittimes'] = tostamp();
                $updata['auditmsg'] = $auditmsg;
                $csql = new \ciy\sql('ap_usr_real_apply');
                $csql->where('id', $mrow['id']);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('审核失败:' . $db->error);
                $ids[] = $mrow['id'];
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        $ret['ids'] = $ids;
        return succjson($ret);
    }

    public static function json_exportxls() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p e'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post, $rsuser);
        $mrows = $db->get($csql);
        if (count($mrows) > 10000)
            return errjson('将导出' . count($mrows) . '条，不建议超过1万条，请筛选缩小范围');
        $fields = array();
        $fields[] = array('style' => 'l', 'width' => 60,  'field' => 'id', 'name' => '行码');
        $fields[] = array('style' => 'c', 'width' => 100,  'field' => 'auditstatus', 'name' => '审核状态');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'vuser', 'name' => '所属用户');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'name', 'name' => '真实姓名');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'idid', 'name' => '身份证号');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'idcard1', 'name' => '身份证人像面');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'idcard2', 'name' => '身份证国徽面');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'addtimes', 'name' => '申请时间');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'audituser', 'name' => '审核员');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'audittimes', 'name' => '审核时间');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'auditmsg', 'name' => '驳回原因');
        $code_auditstatus = getcatas($db, 'auditstatus');
        $code_vuser = getrelation($db, $mrows, 'ap_user', 'vuser', 'id,name');
        $code_audituser = getcatas($db, 'adminuser');
        $datas = array();
        foreach ($mrows as $mrow) {
            $dat = array();
            foreach ($fields as $field) {
                $field = $field['field'];
                $val = isset($mrow[$field]) ? $mrow[$field] : '';
                if ($field == 'id')
                    $val = enid($val);
                if ($field == 'auditstatus')
                    $val = ccode($code_auditstatus, $val);
                if ($field == 'vuser')
                    $val = ccode($code_vuser, $val);
                if ($field == 'addtimes')
                    $val = ($val <= 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'audituser')
                    $val = ccode($code_audituser, $val);
                if ($field == 'audittimes')
                    $val = ($val <= 0 ? '--' : date('Y-m-d H:i', $val));
                $dat[] = $val;
            }
            $datas[] = $dat;
        }
        $param = array();
        $param['field'] = $fields;
        $param['data'] = $datas;
        $param['sheetname'] = '数据报表';
        $param['titleheight'] = '25'; //列头高度
        $param['landscape'] = true; //横向打印
        $param['fixtopage'] = true; //打印整个工作表
        $param['toptitle'] = '实名认证申请数据报表';
        $str = \ciy\excel::general_excel_xml($param);
        $filename = '';
        if (empty($filename))
            $filename = date('Y-m-d_H-i-s') . rand(1000, 9999);
        $filename .= '.xls';
        file_put_contents(PATH_WEB . 'ud/tmp/' . $filename, $str);
        return succjson(array('url' => '/ud/tmp/' . $filename));
    }
}
