<template>
    <div class="app-container">
        <el-form ref="dataFormRef" :rules="rules" :model="dataForm" status-icon label-width="300px">
            <el-form-item label="运费满减所需最低消费" prop="litemall_express_freight_min">
                <el-input v-model="dataForm.litemall_express_freight_min" />
            </el-form-item>
            <el-form-item label="运费满减不足所需运费" prop="litemall_express_freight_value">
                <el-input v-model="dataForm.litemall_express_freight_value" />
            </el-form-item>
            <el-form-item>
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="update">确定</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { listExpress, updateExpress } from '@/services/api/config/index'

let dataFormRef = ref()
let dataForm = ref({
    litemall_express_freight_min: 0,
    litemall_express_freight_value: 0
})
let rules = ref({
    litemall_express_freight_min: [
        { required: true, message: '不能为空', trigger: 'blur' }
    ],
    litemall_express_freight_value: [
        { required: true, message: '不能为空', trigger: 'blur' }
    ]
})

let init = () => {
    listExpress().then((res: any) => {
        if (res.code === 200)
            dataForm.value = res.data
    })
}

init()

let cancel = () => {
    init()
}
let update = () => {
    dataFormRef.value?.validate((valid: any) => {
        if (!valid) return false
        doUpdate()
    })
}
let doUpdate = () => {
    updateExpress(dataForm.value).then((res: any) => {
        if (res.code === 200)
            ElMessage({
                type: 'success',
                message: '运费配置修改成功'
            })

    }).catch(res => {
        ElMessage({
            type: 'error',
            message: res.msg
        })
    })
}
</script>
