<template>
    <div>
        <div class="bigBox">
            <el-dialog v-model="dialogVisible" :title="tips" width="30%" @close="closees">
                <el-form :model="numForm" label-width="120px" :rules="numRules">
                    <el-form-item label="参数编码" prop="name">
                        <el-input v-model="numForm.name" />
                    </el-form-item>
                    <el-form-item label="参数值" prop="value" >
                        <el-input v-model="numForm.value" :disabled="ae == 1" />
                    </el-form-item>
                    <el-form-item label="参数描述" prop="memo">
                        <el-input v-model="numForm.memo" />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="cacncel">取消</el-button>
                        <el-button type="primary" @click="sureBack">
                            确认
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import system from '@/api/system'
import { ref, onMounted, toRefs, reactive } from 'vue'
import { ElMessage } from 'element-plus'
export default {
    props: {
        addRule: {
            type: Boolean,
            required: true
        },
        addEdit: {
            type: Number,
            required: true
        },
        searchSystemCode: {
            type: Function,
            required: true
        },
        editForm: {
            type: Object,
            required: true
        }
    },
    setup(props, { emit }) {
        let dialogVisible = ref(false)
        let result = props.addRule
        let ae = props.addEdit
        let tips = ref('新增')
        let numForm = ref({
            name: "",
            value: "",
            memo: ""
        })
        let aeList = props.editForm
        let numRules = reactive({
            name: [
                { required: true, message: '请输入编码', trigger: 'blur' },
            ],
            value: [
                { required: true, message: '请输入值', trigger: 'blur' },
            ],
            memo: [
                { required: true, message: '请输入描述', trigger: 'blur' },
            ]
        })
        let { cacncel, sureBack } = toRefs(props)
        function lineList() {
            dialogVisible.value = result
            if (ae == 0) {
                tips.value = '新增'
            }
            if (ae == 1) {
                tips.value = '修改'
                numForm.value.name = aeList.param_name
                numForm.value.value = aeList.param_value
                numForm.value.memo = aeList.param_memo
            }
        }
        function closees() {
            dialogVisible.value = false
            emit("asBack", dialogVisible.value)
        }
        function getBack() {
            dialogVisible.value = false
            emit("asBack", dialogVisible.value)
        }
        function fullBack() {
            if (ae == 0) {
                system.editSystemCode({
                    param_name: numForm.value.name,
                    param_value: numForm.value.value,
                    param_memo: numForm.value.memo
                }).then(res => {
                    if (res.code == 0) {
                        dialogVisible.value = false
                        emit("asBack", dialogVisible.value)
                        ElMessage({
                            type: "success",
                            message: "新增成功",
                            duration: 2000
                        })
                        props.searchSystemCode()
                    }
                })
            } else {
                system.editSystemCode({
                    param_name: numForm.value.name,
                    param_value: numForm.value.value,
                    param_memo: numForm.value.memo
                }).then(res => {
                    if (res.code == 0) {
                        dialogVisible.value = false
                        emit("asBack", dialogVisible.value)
                        ElMessage({
                            type: "success",
                            message: "编辑成功",
                            duration: 2000
                        })
                        props.searchSystemCode()
                    }
                })
            }

        }
        onMounted(() => {
            lineList()
        })
        return {
            dialogVisible,
            closees,
            cacncel: getBack,
            sureBack: fullBack,
            tips,
            numForm,
            numRules,
            ae
        }
    }
}
</script>

<style lang="scss" scoped></style>