<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <el-form-item prop="orderRate" label="订单比率">
                <el-input v-model="formData.orderRate" :disabled="'orderRate'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="tixianRate" label="提现比率">
                <el-input v-model="formData.tixianRate" :disabled="'tixianRate'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="personalDeposit" label="个人押金">
                <el-input v-model="formData.personalDeposit" ></el-input>
            </el-form-item>
            <el-form-item prop="companyDeposit" label="公司押金">
                <el-input v-model="formData.companyDeposit" ></el-input>
            </el-form-item>
            <el-form-item prop="extra" label="订单清理天数">
                <el-input v-model="formData.extra" :disabled="'extra'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="useFlag" label="启用标识">
                <el-switch v-model="useFlag" active-color="#13ce66" inactive-color="#ff4949" :disabled="'useFlag'==subColumn">
                </el-switch>
            </el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'

export default {
    created() {
        if (this.$route.query.column) {
            this.subColumn = this.$route.query.column.split(',')[1];
            this.subValue = this.$route.query.column.split(',')[0];
        }

        if (this.$route.query.id) {
            this.$http.get({
                url: '/systemConfig/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {


                    this.formData = res.data;
                    this.useFlag = this.formData.useFlag == 'Y' ? true : false;

                    if (this.$route.query.column) {
                        this.formData[this.subColumn] = this.subValue;
                    }
                }
            })
        } else {
            if (this.$route.query.column) {
                this.formData[this.subColumn] = this.subValue;
            }
        }

    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
            },
            subColumn: '',
            subValue: '',
            useFlag: true,
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            this.formData.useFlag = this.useFlag ? 'Y' : 'N';
            var data = JSON.parse(JSON.stringify(this.formData));
            this.$http.post({
                url: this.formData.id ? '/systemConfig/update' : '/systemConfig/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/systemConfig/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
