<template>
    <el-container>
        <el-main>
            <el-card shadow="never" header="系统设置" class="borderNone">
                <el-tabs v-model="activeName" class="demo-tabs" @tab-change="handleClick">
                    <el-tab-pane label="基础设置" name="service" v-loading="isLoading">
                        <el-form :model="sys" :rules="rules" ref="form" label-position="top" @keyup.enter="saveForm">
                            <el-form-item label="系统名称" prop="title" class="label-item">
                                <el-input v-model="sys.title" placeholder="请输入" clearable />
                                <div class="el-form-item-msg"></div>
                            </el-form-item>
                            <el-form-item label="联系电话" prop="tel" class="label-item">
                                <el-input v-model="sys.tel" placeholder="请输入" clearable />
                                <div class="el-form-item-msg"></div>
                            </el-form-item>
                            <el-form-item label="系统Logo" prop="logo">
                                <sc-upload v-model="sys.logo" title="请上传"></sc-upload>
                            </el-form-item>
                            <el-form-item label="备案号" prop="miitbeian" class="label-item">
                                <el-input v-model="sys.miitbeian" placeholder="请输入" clearable />
                                <div class="el-form-item-msg">支持Html格式</div>
                            </el-form-item>
                            <el-form-item label="版权信息" prop="copyright" class="label-item">
                                <el-input v-model="sys.copyright" placeholder="请输入" clearable />
                                <div class="el-form-item-msg"></div>
                            </el-form-item>
                            <el-form-item label="隐私协议" prop="privacy" class="label-item">
                                <sc-editor v-model="sys.privacy" placeholder="请输入" :height="400"></sc-editor>
                                <div class="el-form-item-msg"></div>
                            </el-form-item>
                            <el-form-item label="服务协议" prop="agreements" class="label-item">
                                <sc-editor v-model="sys.agreements" placeholder="请输入" :height="400"></sc-editor>
                                <div class="el-form-item-msg"></div>
                            </el-form-item>
                        </el-form>
                    </el-tab-pane>
                    <el-tab-pane label="小程序设置" name="wechat" v-loading="isLoading">
                        <el-form :model="sys" :rules="rules2" ref="form" label-position="top" @keyup.enter="saveForm" v-if="activeName=='wechat'">
                            <el-row :gutter="15">
                                <el-col :span="this.$store.state.global.ismobile?24:12">
                                    <fieldset>
                                        <legend><el-tag>小程序配置</el-tag></legend>
                                        <el-form-item label="AppId(小程序 APPID)" prop="appid">
                                            <el-input v-model="sys.appid" clearable placeholder="请输入"></el-input>
                                            <div class="el-form-item-msg">小程序后台获取</div>
                                        </el-form-item>
                                        <el-form-item label="AppSecret(小程序密钥)" prop="secret">
                                            <el-input v-model="sys.secret" show-password clearable placeholder="请输入"></el-input>
                                            <div class="el-form-item-msg">小程序后台获取</div>
                                        </el-form-item>
                                        <el-form-item label="分享标题" prop="share_title">
                                            <el-input v-model="sys.share_title" clearable placeholder="请输入"></el-input>
                                            <div class="el-form-item-msg">小程序分享显示</div>
                                        </el-form-item>
                                        <el-form-item label="分享图片" prop="share_img">
                                            <sc-upload v-model="sys.share_img" title="请上传"></sc-upload>
                                            <div class="el-form-item-msg">小程序分享显示</div>
                                        </el-form-item>
                                    </fieldset>
                                </el-col>
                                <el-col :span="this.$store.state.global.ismobile?24:12">
                                    <fieldset>
                                        <legend><el-tag>支付配置</el-tag></legend>
                                        <el-form-item label="MCH_ID(商户账号)" prop="mch_id">
                                            <el-input v-model="sys.mch_id" clearable placeholder="请输入"></el-input>
                                            <div class="el-form-item-msg">微信商户账号，需要在微信商户平台获取，MCH_ID 与 APPID 匹配</div>
                                        </el-form-item>
                                        <el-form-item label="支付密钥" prop="mini_secret">
                                            <el-input v-model="sys.mini_secret" show-password clearable placeholder="请输入"></el-input>
                                            <div class="el-form-item-msg"></div>
                                        </el-form-item>
                                        <el-form-item label="应用公钥" prop="app_public_key">
                                            <el-input v-model="sys.app_public_key" type="textarea" rows="10" show-password clearable placeholder="请输入"></el-input>
                                            <div class="el-form-item-msg"></div>
                                        </el-form-item>
                                        <el-form-item label="平台公钥" prop="public_key">
                                            <el-input v-model="sys.public_key" type="textarea" rows="10" show-password clearable placeholder="请输入"></el-input>
                                            <div class="el-form-item-msg"></div>
                                        </el-form-item>
                                    </fieldset>
                                </el-col>
                            </el-row>
                        </el-form>
                    </el-tab-pane>
                    
                </el-tabs>
            </el-card>
        </el-main>
        <el-footer>
            <el-button type="primary" size="large" :loading="isSaveing" @click="saveForm">保存</el-button>
        </el-footer>
    </el-container>
</template>
<script>
import { defineAsyncComponent } from 'vue';
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
    components: {
        scEditor
    },
    data(){
        return {
            activeName:"service",
            isSaveing: false,
            sys: {},
            smsRegion:[],
            rules2:{
                appid: [
                    { required: true, message: '请输入' }
                ],
                secret: [
                    { required: true, message: '请输入' }
                ],
                share_title: [
                    { required: true, message: '请输入' }
                ],
                share_img: [
                    { required: true, message: '请上传' }
                ],
                mch_id: [
                    { required: true, message: '请输入' }
                ],
                mini_secret: [
                    { required: true, message: '请输入' }
                ],
                mini_public_key: [
                    { required: true, message: '请输入' }
                ],
                min_private_key: [
                    { required: true, message: '请输入' }
                ],
            },
            rules: {
                title: [
                    { required: true, message: '请输入' }
                ],
                tel: [
                    { required: true, message: '请输入' }
                ],
                logo: [
                    { required: true, message: '请选择', trigger:"change" }
                ],
                miitbeian: [
                    { required: true, message: '请输入' }
                ],
                beian: [
                    { required: true, message: '请输入' }
                ],
                copyright: [
                    { required: true, message: '请输入' }
                ]
            },
            isLoading:false
        }
    },
    created() {
        this.getConfig();
    },
    methods: {
        handleClick(event){
            this.activeName = event;
            this.getConfig();
        },
        saveForm() {
            this.isSaveing = true;
            this.$refs.form.validate(async (valid) => {
                console.log(valid)
                if (valid) {
                    var result = await this.$API.setting.configSave.post({type:this.activeName,data: this.sys });
                    this.isSaveing = false;
                    if(result.code == 0)
                    {
                        this.$message.error(result.msg);
                        return false;
                    }
                    if (this.activeName == "service") {
                        this.$TOOL.data.set("SERVICE", this.sys)
                    }
                    this.$message.success(result.msg);
                } else {
                    this.isSaveing = false;
                    return false;
                }
            })
        },
        async getConfig() {
            this.isLoading = true;
            var result = await this.$API.setting.config.get({ type: this.activeName });
            this.isLoading = false;
            if (result.code == 0) {
                this.$message.error(result.msg);
                return;
            }
            if (this.activeName == "sms") {
                this.smsRegion = result.data.channel
            }
            this.sys = result.data;
        },
    }
}
</script>