
<template>
    <base-dialog :title="prop.title" width="60%" :visible="prop.visible" :autoHeight="false" @open="handleOpen"
        @handleConfirm="handleConfirm" @close="handleClose">
        <div class="body">
            <el-form ref="formRef" :model="prop.form" label-width="120px" :rules="rule">
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="prop.form.name" placeholder="部门名称" />
                </el-form-item>
                <el-form-item label="英文名称">
                    <el-input v-model="prop.form.enName" placeholder="英文名称" />
                </el-form-item>
                <el-form-item label="排序">
                    <el-input type="number" v-model="prop.form.sort" placeholder="排序" />
                </el-form-item>
            </el-form>
        </div>
    </base-dialog>
</template>
   
<script setup>
import { ref, watch, getCurrentInstance } from 'vue'
import { Check, Close } from '@element-plus/icons-vue'
import { uploadAvatar } from "@/api/system/user";
import { getToken } from '@/utils/auth'
import { addDept, updateDept } from '@/api/big/dept'
import ImageUpload from '@/components/ImageUpload/index.vue'


const uploadImgUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 上传的图片服务器地址
const prop = defineProps({
    visible: {
        type: Boolean,
        default: false,
    },
    title: {
        type: String,
        default: '提示',
    },
    type: {
        type: String,
        default: 'add',
    },
    form: {
        type: Object,
        default: {
            name: String,
            enName: String,
            sort: Number
        },
    }
})
const formRef = ref(null)
const { proxy: ctx } = getCurrentInstance()
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const disabled = ref(false)
const uploadRef = ref(null)
const rule = {
    name: [
        { required: true, message: '请输入部门名称', trigger: 'blur' },
    ],
    // enName: [
    //     { required: true, message: '请输入英文名称', trigger: 'blur' },
    // ],
    // sort: [
    //     { required: true, message: '请输入排序', trigger: 'blur' },
    // ],
}

const onExceed = (files, uploadFiles) => {
    uploadRef.value.clearFiles()
    nextTick(() => {
        uploadRef.value.handleStart(files[0])
    })
}

const handleClose = () => {
    //baseFormRef.value.reset()
    emit('closeDialog')
    emit('refreshTable')
}
// 弹窗弹出
const handleOpen = () => {
    // getAddFormOptions()
}


// 刷新表格事件
const emit = defineEmits(['refreshTable', 'closeDialog'])
// 添加/修改
async function addDeductionInfo() {
    if (!formRef.value) return
    let isValid = false
    await formRef.value.validate((valid, fields) => {
        if (valid) {
            isValid = true
            console.log('submit!')
        } else {
            console.log('error submit!', fields)
            isValid = false
        }
    })
    if (!isValid) return
    //增加
    if (prop.type == 'add') {
        let { code, msg } = await addDept(prop.form)
        ctx.$message({
            type: code === 200 ? 'success' : 'warning',
            message: code === 200 ? '添加成功' : '添加失败',
        })
        if (code === 200) {
            // 刷新表格
            emit('refreshTable')
            emit('closeDialog')
        }
    } else { //修改
        let { code, msg } = await updateDept(prop.form)
        ctx.$message({
            type: code === 200 ? 'success' : 'warning',
            message: code === 200 ? '修改成功' : '修改失败',
        })
        if (code === 200) {
            // 刷新表格
            emit('refreshTable')
            emit('closeDialog')
        }
    }


}
// 点击确定
const handleConfirm = () => {
    addDeductionInfo()
}



</script>
   
<style lang="scss" scoped>
.body {
    height: 100%;
}

.img-preview {
    max-width: 100%;
    max-height: 100%;
    display: block;
    margin: 0 auto;
    padding-top: 10px;
}
</style>