<!-- 转移线索 -->
<template>
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="850px">
        <p class="tips">*温馨提示:提交后当前{{ typeName }}下相关数据将同步转移</p>
        <el-form ref="formRef" :model="form" :rules="rules" label-width="92px">
            <el-form-item label="接收对象：" prop="newOwnerBy">
                <SelectUser v-model="form.newOwnerBy"></SelectUser>
            </el-form-item>
            <el-form-item label="备注信息：" prop="remark">
                <el-input
                    v-model="form.remark"
                    type="textarea"
                    :rows="4"
                    :maxlength="300"
                    show-word-limit
                    resize="none"
                    placeholder="请输入备注信息"
                />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="cancel">取消</el-button>
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">提交</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
    import { tranferCustomer, tranferTypes } from '@/api/customer'

    const dialog = reactive({
        visible: false,
        title: ''
    })
    const buttonLoading = ref(false)

    const emits = defineEmits(['refresh'])
    const typeName = ref('')
    const tag = ref('')
    const open = (data: { resourceType: string, typeName: string, idList: (string | number)[] , tag: string}) => {
        typeName.value = data.typeName
        dialog.title = '转移' + data.typeName
        form.value.idList = data.idList
        form.value.resourceType = data.resourceType
        dialog.visible = true
        tag.value = data.tag
    }

    const form = ref<tranferTypes>({
        newOwnerBy: '',
        remark: '',
        resourceType: '',
        idList: []
    })

    const rules =  {
        newOwnerBy: [
            { required: true, message: "接收对象不能为空", trigger: "change" }
        ]
    }

    const formRef = ref()
    const submitForm = () => {
        formRef.value?.validate(async (valid: boolean) => {
            if(valid) {
                tranferCustomer(form.value).then(res => {
                    if(res.success) {
                        ElMessage.success("转移成功")
                        emits('refresh', tag.value)
                        cancel()
                    }
                })
            }
        })
    }

    const cancel = () => {
        dialog.visible = false
    }

    defineExpose({
        open
    })
</script>

<style lang="scss" scoped>
    .tips{
        font-weight: bold;
        margin-bottom: 10px;
    }
</style>
