<?php

namespace web\admin\develop;

class code_list {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('INFORMATION_SCHEMA.TABLES');
        return [$query, $csql];
    }
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $csql = new \ciy\sql('zc_dev_dbsaas');
        $csql->where('id', getint('dbcid'));
        $dbsaasrow = $db->getone($csql);
        list($where, $csql) = self::setwhere($db, $post);
        $csql->where('TABLE_SCHEMA', $dbsaasrow['schem']);
        $rows = $db->get($csql);
        for ($i = 0; $i < count($rows); $i++)
            $rows[$i]['id'] = $i + 1;
        $ret = array('searchwhere' => $where, 'list' => $rows);

        if ($post->getbool('field')) {
            $field = array();
            $field['_btn'] = array('c' => '操作');
            $field['TABLE_COMMENT'] = array('c' => '表名');
            $field['TABLE_NAME'] = array('c' => '英文名');
            $field['TABLE_COLLATION'] = array('c' => '编码');
            $field['ENGINE'] = array('c' => '引擎');
            $ret['field'] = $field;
        }

        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'input',
                'form' => 'TABLE_COMMENT',
                'name' => '表名',
                'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input',
                'form' => 'TABLE_NAME',
                'name' => '英文名',
                'prop' => ' style="width:8em;"'
            );
            $ret['searchinput'] = $input;
            $ret['dbsaas'] = $dbsaasrow;
            $csql = new \ciy\sql('zc_dev_dbcode');
            $csql->where('dbcid', getint('dbcid'));
            $csql->column('tabl');
            $ret['dbcode'] = $db->get($csql);
        }
        return succjson($ret);
    }

    public static function json_getschema() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('INFORMATION_SCHEMA.SCHEMATA');
        $csql->column('SCHEMA_NAME as name');
        $sehrows = $db->get($csql);
        $ret['schema'] = $sehrows;
        return succjson($ret);
    }
    public static function json_update() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->get('id');
        $tabname = $post->get('tabname');
        $name = $post->get('name');
        if (empty($name))
            return errjson('请填写表名');
        if ($db->execute("ALTER TABLE {$tabname} COMMENT = '{$name}';") === false)
            return errjson('更新失败:' . $db->error);
        $ret['data'] = array('id' => $id, 'TABLE_COMMENT' => $name);
        return succjson($ret);
    }
}
