<?php

namespace web\admin\develop;

class code_create {
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $dbcid = objint($query, 'dbcid');
        $tabname = objstr($query, 'table');
        $csql = new \ciy\sql('zc_dev_dbsaas');
        $csql->where('id', $dbcid);
        $dbsaasrow = $db->getone($csql);
        if (!is_array($dbsaasrow))
            return errjson('dbsaas不存在');
        $schema = $dbsaasrow['schem'];
        $csql = new \ciy\sql('INFORMATION_SCHEMA.COLUMNS');
        $csql->where('TABLE_SCHEMA', $schema);
        $csql->where('TABLE_NAME', $tabname);
        $rows = $db->get($csql);
        for ($i = 0; $i < count($rows); $i++)
            $rows[$i]['id'] = $i;
        $ret = array('list' => $rows);
        $ret['dbsaas'] = $dbsaasrow;
        $ret['table'] = $tabname;
        $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;

        $csql = new \ciy\sql('zc_dev_dbcode');
        $csql->where('dbcid', $dbcid);
        $csql->where('tabl', $tabname);
        $csql->column('pcjson');
        $jnstr = $db->get1($csql);
        if (empty($jnstr)) {
            $csql = new \ciy\sql('INFORMATION_SCHEMA.TABLES');
            $csql->where('TABLE_SCHEMA', $schema);
            $csql->where('TABLE_NAME', $tabname);
            $tabrow = $db->getone($csql);
            $chk_audit = false;
            foreach ($rows as $row) {
                if ($row['COLUMN_NAME'] == 'auditstatus')
                    $chk_audit = true;
            }
            $ret['code_create'] = array('bcod' => array(), 'col' => array());
            $ret['code_create']['bcod']['tabname'] = $tabname;
            $ret['code_create']['bcod']['titname'] = str_replace('表', '', $tabrow['TABLE_COMMENT']);
            $maintab = substr($tabname, strpos($tabname, '_') + 1);
            $subend = substr($dbsaasrow['subpath'], strrpos($dbsaasrow['subpath'], '\\') + 1);
            if (substr($maintab, 0, strlen($subend)) == $subend)
                $maintab = substr($maintab, strlen($subend));
            if (substr($maintab, 0, 1) == '_')
                $maintab = substr($maintab, 1);
            $ret['code_create']['bcod']['savepc'] = 'web\\' . $dbsaasrow['subpath'] . '\\' . $maintab;
            $ret['code_create']['bcod']['maintab'] = $maintab;
            $ret['code_create']['bcod']['pctemplate'] = 'sigpage';
            $ret['code_create']['bcod']['pcparam'] = ',listchk,';

            if ($chk_audit) {
                $ret['code_create']['bcod']['newline'] = '导出,帮助';
                $ret['code_create']['bcod']['multibtn'] = '批量删除,审核通过,驳回';
                $ret['code_create']['bcod']['btnlist_line'] = '审核';
            } else {
                $ret['code_create']['bcod']['newline'] = '添加,导入,导出,帮助';
                $ret['code_create']['bcod']['multibtn'] = '批量删除';
                $ret['code_create']['bcod']['btnlist_line'] = '查看,修改';
            }
        } else {
            $ret['code_create'] = json_decode($db->get1($csql), true);
        }
        return succjson($ret);
    }
    public static function json_save() {
        $post = new \ciy\post();
        $ret = self::save($post);
        if (is_string($ret))
            return errjson($ret);
        return succjson();
    }
    public static function save($post) {
        global $db;
        $dbcid = getint('dbcid');
        $tabname = get('table');
        $json = array();
        $json['bcod'] = array();
        //$json['bcod']['maindb'] = $post->get('_bcod_maindb', '', 'all');
        $json['bcod']['verifyfunc'] = $post->get('_bcod_verifyfunc', '', 'all');
        $json['bcod']['titname'] = $post->get('_bcod_titname', '', 'all');
        //$json['bcod']['logtype'] = $post->get('_bcod_logtype', '', 'all');
        $json['bcod']['uploadpat'] = $post->get('_bcod_uploadpat', '', 'all');
        $json['bcod']['liiddict'] = str_replace('，', ',', $post->get('_bcod_liiddict', '', 'all'));
        $json['bcod']['uptabcard'] = str_replace('，', ',', $post->get('_bcod_uptabcard', '', 'all'));
        $json['bcod']['selectorder'] = $post->get('_bcod_select_order', '', 'all');
        $json['bcod']['newline'] = $post->get('_bcod_btntop_newline', '', 'all');
        $json['bcod']['multibtn'] = $post->get('_bcod_btntop_multibtn', '', 'all');
        $json['bcod']['btnlist_line'] = $post->get('_bcod_btnlist_line', '', 'all');
        $json['bcod']['savepc'] = $post->get('_bcod_savepc', '', 'all');
        $json['bcod']['htmllang'] = $post->get('_bcod_htmllang', '', 'all');
        $json['bcod']['proglang'] = $post->get('_bcod_proglang', '', 'all');
        $json['bcod']['pctemplate'] = $post->get('_bcod_pctemplate', '', 'all');
        $json['bcod']['pcparam'] = $post->get('_bcod_pcparam', '', 'all');
        $json['bcod']['mbtemplate'] = $post->get('_bcod_mbtemplate', '', 'all');

        $total = $post->getint('total');
        $json['col'] = array();
        for ($i = 0; $i < $total; $i++) {
            $col = $post->get('col_' . $i);
            if (empty($col))
                continue;
            $json['col'][$col] = array();
            $json['col'][$col]['bchk'] = $post->get('bchk_' . $i);
            $json['col'][$col]['set'] = $post->get('set_' . $i, '', 'all');
        }
        try {
            $db->begin();
            $updata = array();
            $updata['dbcid'] = $dbcid;
            $updata['tabl'] = $tabname;
            $updata['pcjson'] = json_encode($json, JSON_PARTIAL_OUTPUT_ON_ERROR);
            $csql = new \ciy\sql('zc_dev_dbcode');
            $csql->where('dbcid', $dbcid);
            $csql->where('tabl', $tabname);
            $row = $db->getone($csql);
            $csql = new \ciy\sql('zc_dev_dbcode');
            if (is_array($row)) {
                $csql->where('id', $row['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;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_del() {
        global $db;
        $dbcid = getint('dbcid');
        $tabname = get('table');
        $csql = new \ciy\sql('zc_dev_dbcode');
        $csql->where('dbcid', $dbcid);
        $csql->where('tabl', $tabname);
        if ($db->delete($csql) === false)
            return errjson('删除失败:' . $db->error);
        return succjson();
    }
    public static function json_createhtml() {
        $post = new \ciy\post();
        require_once 'code_common.php';
        require_once 'code_pc_html.php';
        $codex = code_pc_html($post);
        return succjson(array('source' => $codex));
    }

    public static function json_createphp() {
        $post = new \ciy\post();
        require_once 'code_common.php';
        require_once 'code_pc_php.php';
        $codex = code_pc_php($post);
        return succjson(array('source' => $codex));
    }

    public static function json_creatembvue3() {
        $post = new \ciy\post();
        require_once 'code_common.php';
        require_once 'code_mb_vue3.php';
        $codex = code_mbvue3($post);
        return succjson(array('source' => $codex));
    }
    public static function json_savepc() {
        $post = new \ciy\post();
        require_once 'code_common.php';
        require_once 'code_pc_html.php';
        require_once 'code_pc_php.php';
        $savepc = $post->get('_bcod_savepc', '', 'all');
        if (empty($savepc))
            return errjson('请填写PC保存路径');
        if (strpos($savepc, ':') === false)
            $savepc = PATH_ROOT . $savepc;
        $tpath = dirname($savepc);
        if (!is_dir($tpath))
            return errjson('目录不存在' . $tpath);
        $ret = self::save($post);
        if (is_string($ret))
            return errjson($ret);

        $fname = substr($savepc, strrpos($savepc, '\\') + 1);
        if (empty($fname))
            return errjson('必须指定文件名');
        if ($fname[0] == '_')
            return errjson('文件名不能以_开头');
        $codex = code_pc_html($post);
        $ret = file_put_contents($savepc . '.html', $codex);
        if ($ret === false)
            return errjson('写入html失败');
        $codex = code_pc_php($post);
        $ret = file_put_contents($savepc . '.php', $codex);
        if ($ret === false)
            return errjson('写入php失败');
        return succjson();
    }
}
