<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-form-item label="店员名称" prop="staff_name">
                    <el-input v-model="formData.staff_name" clearable placeholder="请输入店员名称" />
                </el-form-item>
                <el-form-item label="头34像">
                    <material-picker v-model="formData.avatar" :limit="1" />
                </el-form-item>
                <el-form-item label="店员账号" prop="account">
                    <el-input v-model="formData.account" clearable placeholder="请输入店员账号" />
                </el-form-item>
                <el-form-item label="店员密码" prop="pwd">
                    <el-input v-model="formData.pwd" clearable placeholder="请输入店员账号" type="password" />
                </el-form-item>
                <el-form-item label="确认密码" prop="rePwd">
                    <el-input v-model="formData.rePwd" clearable placeholder="请确认账号密码" type="password" />
                </el-form-item>
                <el-form-item label="核销开关" prop="rePwd">
                    <el-switch v-model="formData.verify_status" :active-value="1" :inactive-value="0"></el-switch>
                </el-form-item>
                <el-form-item label="订单状态" prop="rePwd">
                    <el-switch v-model="formData.order_status" :active-value="1" :inactive-value="0"></el-switch>
                </el-form-item>
                <el-form-item label="是否是店长" prop="rePwd">
                    <el-switch v-model="formData.is_manager" :active-value="1" :inactive-value="0"></el-switch>
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="formData.phone" clearable placeholder="请输入手机号码" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="oaPlanEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import type { PropType } from 'vue'
import { apiStaffAdd, apiStaffEdit } from '@/api/goodsList.ts'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑日程安排' : '新增日程安排'
})

// 表单数据
const formData = reactive({
    id: '',
    staff_name: '',
    avatar: 'https://lihaiim.oss-cn-chengdu.aliyuncs.com/public/uploads/images/20240606/20240606162227e3e005096.jpg',
    account: '',
    pwd: '',
    rePwd: '',
    verify_status: 0,
    order_status: 0,
    is_manager: 0,
    phone: '',
})

const chekcDate = (rule: any, value: any, callback: any) => {
    if ((formData.rePwd) != (formData.pwd)) {
        callback(new Error('两次密码不一致'))
    } else {
        callback()
    }
}

// 表单验证
const formRules = reactive<any>({
    staff_name: [{
        required: true,
        message: '不能为空',
        trigger: ['blur']
    }],
    avatar: [{
        required: true,
        message: '不能为空',
        trigger: ['blur']
    }],
    account: [{
        required: true,
        message: '不能为空',
        trigger: ['blur']
    }],
    pwd: [{
        required: true,
        message: '不能为空',
        trigger: ['blur']
    }
    ],
    rePwd: [{
        required: true,
        message: '不能为空',
        trigger: ['blur']
    },
    {
        validator: chekcDate,
        trigger: ['blur']
    }


    ],
    phone: [{
        required: true,
        message: '不能为空',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}



// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiStaffEdit(data)
        : await apiStaffAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add', start_time = '') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
})
</script>
