<?php

namespace web\admin\ap;

class invoicedetail {
    static function setwhere($db, $post, $rsuser) {
        $query = $post->get('query', array());
        $csql = new \ciy\sql('ap_invoicedetail');
        $csql->where('name like', objstr($query, 'name'));
        $csql->wheredaterange('uptimes', objstr($query, 'uptimes'));
        $val = objstr($query, 'adminuser');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('zc_cata');
            $csqlt->where('cbid in (select id from zc_cata where cbid=0 and codeid=\'adminuser\')');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('adminuser', $trow['codeid']);
                $query['adminuser'] = $trow['name'];
            } else {
                $csql->where('adminuser=0');
            }
        }
        $csql->where('memo like', objstr($query, 'memo'));
        $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');
        $rows = $db->get($csql, $mainrowcount);
        if($rows === false)
            return errjson($db->error);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'ap_invoicedetail');
            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' => 'name', 'name' => '名称', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'daterange', 'form' => 'uptimes', 'name' => '更新时间');
            $input[] = array('type' => 'input', 'form' => 'adminuser', 'name' => '维护人', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'input', 'form' => 'memo', 'name' => '备注信息', 'prop' => ' style="width:8em;"');
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }
    public static function json_update() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p u'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $name = $post->get('name');
        $adminuser = $post->getint('adminuser');
        $memo = $post->get('memo');
        if (empty($name))
            return errjson('请填写名称');
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('ap_invoicedetail');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['uptimes'] = tostamp();
            $updata['adminuser'] = $adminuser;
            $updata['memo'] = $memo;
            $csql = new \ciy\sql('ap_invoicedetail'); //auto
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            //savelogdb($db, $rsuser['id'], 'ap_invoicedetail', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }

    public static function json_del() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p d'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('ap_invoicedetail');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                //delcheck($db, $delid, 'tablexx', 'xxid', '管理员');
                //delall($db, $delid, 'tablexx', 'xxid', '运动员');  //deltimeall
                delme($db, $delid, 'ap_invoicedetail');
                savelogdb($db, $rsuser['id'], 'ap_invoicedetail', $row, null);
                $vids[] = $delid;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['ids'] = $vids;
        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);
        $rows = $db->get($csql);
        if (count($rows) > 10000)
            return errjson('将导出' . count($rows) . '条，不建议超过1万条，请筛选缩小范围');
        $fields = array();
        $fields[] = array('style' => 'l', 'width' => 60,  'field' => 'id', 'name' => '行码');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'name', 'name' => '名称');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'uptimes', 'name' => '更新时间');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'adminuser', 'name' => '维护人');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'memo', 'name' => '备注信息');
        $code_adminuser = getcatas($db, 'adminuser');
        $datas = array();
        foreach ($rows as $row) {
            $dat = array();
            foreach ($fields as $field) {
                $field = $field['field'];
                $val = isset($row[$field]) ? $row[$field] : '';
                if ($field == 'id')
                    $val = enid($val);
                if ($field == 'uptimes')
                    $val = ($val <= 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'adminuser')
                    $val = ccode($code_adminuser, $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));
    }
}
