import Modal from '@core/component/modal'
import Switch from '@core/component/switch'
import { fetch } from '../../../core/src/component/httpClient'
import INTERFACE from '../const/INTERFACE'
import '@core/scss/index.scss';
import './index.scss';
export const  tpl =  `
<div class="cd-module-info-modal-content">
    <div class='row'>
        <span class='row-left'>开启详细日志：</span>
        <div class='row-right'>
            <div class='cd-property-content cd-form-container cd-force-update'>
                <div class="cd-form-group">
                    <div class="cd-body">
                        <div class="J_forceUpdate"></div>
                        <p class="cd-form-tips J_formTips">请在项目启动的控制台看日志（非浏览器），打开后开启详细日志打印，关闭后只有少量日志打印</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
`;
class Setting {
    config: any
    dialog: any
    debugSwitch: any
    constructor(config = {}) {
        this.config = this._getConfig(config);
    }
    _getConfig(config) {
        return $.extend(true, {}, config);
    }
    async _initUI($modal, setting) {
        console.log("setting:", setting)
        this.debugSwitch = new Switch({
            node: $modal.find('.J_forceUpdate')[0],
            initState: setting.debug === true || setting.debug === "true",
            id: 'switch',
        })
    }
    _bindEvent($modal) {
        
    }
    
    async getSetting() {
        const reulst = await fetch({ method: 'get', url: INTERFACE.getSetting})
        if (reulst && reulst.code == 200) {
            return reulst.data
        }
        return {}
    }
    
    async saveSetting(setting) {
        await fetch({ url: INTERFACE.saveSetting, data: setting})
    }
    show(){
        this.dialog = new Modal({
            title: '设置',
            className:"cd-module-info-modal",
            stopBubble: false,
            content: tpl,
            footer: {
                show: true,
                suctionBottom: false,    
                okLabel: "确定",
                cancelLabel: "取消",
                align: "center"
            },
            onDomReady: async ($modal) => {
                const setting = await this.getSetting()
                await this._initUI($modal, setting)
                this._bindEvent($modal);
            },
            onClose: async (flag)=>{
                if(flag === Modal.CloseType.TYPE_OK){
                    const debug = this.debugSwitch.getState()
                    await this.saveSetting({debug})
                }
            }
        });
    }
    dispose(){
        if(this.dialog){
            this.dialog.close();
        }
    }
}
export default Setting;