<template>
    <div>
        <el-form :inline="true" :model="query" class="query-form" size="mini">
            <el-form-item>
                <el-button-group>
                    <el-button type="primary" @click.native="addCustomizeParam()">添加参数</el-button>
                </el-button-group>
            </el-form-item>
        </el-form>
        <el-card>


            <el-table v-loading="loading" :data="list" style="width: 100%">
                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column label="参数名" prop="paramName"></el-table-column>
                <el-table-column label="参数键" prop="paramKey" width="200"></el-table-column>
                <el-table-column label="参数值" prop="paramValue" width="100"></el-table-column>
                <!--<el-table-column label="参数状态">-->
                <!--<template slot-scope="scope">-->
                <!--<el-tag :type="scope.row.paramStatus | paramStatusFilterType">{{-->
                <!--scope.row.paramStatus | paramStatusFilterName-->
                <!--}}-->
                <!--</el-tag>-->
                <!--</template>-->
                <!--</el-table-column>-->
                <el-table-column label="参数说明" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <span>{{ scope.row.paramExplain }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="200">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native="handleForm(scope.$index, scope.row)"
                        >设置
                        </el-button>
                        <el-button
                            type="text"
                            size="small"
                            @click.native="deleteParam(scope.$index, scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                :current-page="query.page"
                :page-size="query.limit"
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="total"
            >
            </el-pagination>
        </el-card>
        <!--表单-->
        <el-dialog
            :title="formMap[formName]"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="formVisible"
            :before-close="hideForm"
            width="85%"
            top="5vh"
        >
            <el-form :model="formData" :rules="formRules" ref="dataForm">
                <el-form-item label="参数名" prop="paramName">
                    <el-input v-model="formData.paramName" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="参数键" prop="paramKey">
                    <el-input v-model="formData.paramKey" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="参数值" prop="paramValue">
                    <el-input
                        type=""
                        v-model="formData.paramValue"
                        auto-complete="off"
                    ></el-input>
                </el-form-item>
                <!--<el-form-item label="参数状态" prop="paramStatus">-->
                <!--<el-radio-group v-model="formData.paramStatus">-->
                <!--<el-radio :label="0">禁用</el-radio>-->
                <!--<el-radio :label="1">正常</el-radio>-->
                <!--</el-radio-group>-->
                <!--</el-form-item>-->
                <el-form-item label="参数说明">
                    <el-input type="textarea" v-model="formData.paramExplain" :disabled="true" rows="6"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="hideForm">取消</el-button>
                <el-button
                    type="primary"
                    @click.native="formSubmit()"
                    :loading="formLoading"
                >提交
                </el-button
                >
            </div>
        </el-dialog>

        <el-dialog
            title="添加自定义参数"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="addParamVisible"
            :before-close="hideAddParamForm"
            width="85%"
            top="5vh"
        >
            <el-table v-loading="loading" :data="customizeParamList" style="width: 100%">
                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column label="参数名" prop="paramName"></el-table-column>
                <el-table-column label="参数键" prop="paramKey" width="200"></el-table-column>
                <el-table-column label="参数值" prop="paramValue" width="100"></el-table-column>
                <el-table-column label="参数说明" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <span>{{ scope.row.paramExplain }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="200">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native="addParam(scope.$index, scope.row)"
                        >添加
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

        </el-dialog>
    </div>
</template>

<script>
import {
    systemParamConfigList,
    systemParamConfigSave,
    getCustomizeParams,
    addCustomizeParam,
    deleteParam
} from "../../../api/param/systemParamConfig";

const formJson = {
    id: "",
    parentId: "",
    paramName: "",
    paramKey: "",
    paramValue: "",
    paramStatus: 1,
    paramType: 1,
    paramExplain: "",
    customerId: null
};
export default {
    data() {
        return {
            query: {
                paramName: "",
                paramStatus: 1,
                page: 1,
                limit: 20,
                customerId: this.$store.getters.customerId
            },
            list: [],
            total: 0,
            loading: true,
            index: null,
            formName: null,
            formMap: {
                add: "新增",
                update: "设置"
            },
            formLoading: false,
            formVisible: false,
            addParamVisible: false,
            customizeParamList: [],
            formData: formJson,
            formRules: {
                paramValue: [
                    {required: true, message: "请输入参数值", trigger: "blur"}
                ]
                // ,
                // paramStatus: [
                //     {required: true, message: "请选择参数状态", trigger: "change"}
                // ]
            },
            deleteLoading: false
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: ""
            });
            this.query = {
                paramName: "",
                paramStatus: 1,
                page: 1,
                limit: 20
            };
            this.getList();
        },
        onSubmit() {
            this.getList();
        },
        handleCurrentChange(val) {
            this.query.page = val;
            this.getList();
        },
        getList() {
            this.loading = true;
            systemParamConfigList(this.query)
                .then(response => {
                    this.loading = false;
                    this.list = response.data.list || [];
                    this.total = response.data.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        // 刷新表单
        resetForm() {
            if (this.$refs["dataForm"]) {
                // 清空验证信息表单
                this.$refs["dataForm"].clearValidate();
                // 刷新表单
                this.$refs["dataForm"].resetFields();
                this.getList();
            }
        },
        // 隐藏表单
        hideForm() {
            // 更改值
            this.formVisible = !this.formVisible;
            return true;
        },
        hideAddParamForm() {
            this.addParamVisible = !this.addParamVisible;
        },
        // 显示表单
        handleForm(index, row) {
            this.formVisible = true;
            this.resetForm();
            formJson.customerId = this.$store.getters.customerId;
            this.formData = JSON.parse(JSON.stringify(formJson));
            if (row !== null) {
                this.formData = Object.assign({}, row);
            }
            this.formName = "add";
            if (index !== null) {
                this.index = index;
                this.formName = "update";
            }
        },
        formSubmit() {
            this.$refs["dataForm"].validate(valid => {
                if (valid) {
                    this.formLoading = true;
                    let data = Object.assign({}, this.formData);
                    data.paramValue = data.paramValue.trim();
                    systemParamConfigSave(data, this.formName)
                        .then(response => {
                            this.formLoading = false;
                            if (response.code !== 20000) {
                                this.$message.error(response.message);
                                return false;
                            }
                            this.$message.success("操作成功");
                            this.formVisible = false;
                            if (this.formName === "add") {
                                // 向头部添加数据
                                if (response.data && response.data.id) {
                                    data.id = response.data.id;
                                    this.list.unshift(data);
                                }
                            } else {
                                this.list.splice(this.index, 1, data);
                            }
                            // 刷新表单
                            this.resetForm();
                            this.getList();
                        })
                        .catch(() => {
                            this.formLoading = false;
                        });
                }
            });
        },
        //添加自定义参数弹窗
        addCustomizeParam() {
            this.addParamVisible = true;
            let query = {customerId: this.$store.getters.customerId};
            getCustomizeParams(query).then((res) => {
                this.customizeParamList = res.data || [];
            }).catch((error) => {
            });
        },
        //添加参数
        addParam(index, row) {
            row.customerId = this.$store.getters.customerId;
            addCustomizeParam(row).then((res) => {
                this.$message.success("添加成功");
                this.getList();
                this.customizeParamList.splice(index, 1);
                if (this.customizeParamList.length == 0) {
                    this.addParamVisible = false;
                }
            }).catch((error) => {
                this.$message.error(error.message);
            });
        },
        //删除参数
        deleteParam(index, row) {
            this.$confirm('是否确认删除此参数?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let data = {id: row.id};
                deleteParam(data).then((res) => {
                    this.getList();
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch((error) => {
                    this.$message.error("删除失败");
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });

        }
    },
    filters: {
        paramStatusFilterType(paramStatus) {
            const paramStatusMap = {
                0: "gray",
                1: "success"
            };
            return paramStatusMap[paramStatus];
        },
        paramStatusFilterName(paramStatus) {
            const paramStatusMap = {
                0: "禁用",
                1: "正常"
            };
            return paramStatusMap[paramStatus];
        }
    },
    mounted() {
    },
    created() {
        // 加载表格数据
        this.getList();
    }
};
</script>

<style type="text/scss" lang="scss">
</style>
