<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="90px" :rules="formRules">
                <el-form-item label="借款单编号" prop="borrow_code">
                    <el-input v-model="formData.borrow_code" clearable placeholder="点击选择借款单" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="单据编号">
                    <el-input v-model="formData.code" clearable placeholder="系统自动生成" disabled />
                </el-form-item>
                <el-form-item label="还款人" prop="payer_name">
                    <el-input v-model="formData.payer_name" clearable placeholder="点击选择还款人" @click="userclick" />
                </el-form-item>
                <el-form-item label="还款金额" prop="amount">
                    <el-input v-model="formData.amount" clearable placeholder="请输入还款金额" type="number" />
                </el-form-item>
                <el-form-item label="说明">
                    <el-input v-model="formData.desc" clearable placeholder="请输入说明" type="textarea" />
                </el-form-item>
                <el-form-item label="经办人" prop="create_user">
                    <el-input v-model="formData.create_user" clearable placeholder="请输入经办人" />
                </el-form-item>
                <el-form-item label="还款日期" prop="create_time">
                    <el-date-picker class="flex-1 !flex" v-model="formData.create_time" clearable type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择还款日期">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <el-dialog v-model="showDialog" title="选择借款单" width="70%">
                <dialogTable :config="financial_borrow_money" @customEvent="customEvent">
                </dialogTable>
            </el-dialog>
            <div v-if="showDialog3">
                <personnelselector ref="personnel" @confirm="submituser" type="1">
                </personnelselector>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="financialRepaymentEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiFinancialRepaymentAdd, apiFinancialRepaymentEdit, apiFinancialRepaymentDetail } from '@/api/financial_repayment'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { financial_borrow_money } from "@/components/dialogTable/dialogTableConfig";
import useUserStore from "@/stores/modules/user";

const userStore = useUserStore().userInfo;
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 showDialog = ref(false)
const showDialog3 = ref(false)
const personnel = ref<any>()


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑财务管理--还款单' : '新增财务管理--还款单'
})

//打开弹窗
const userclick = async (e: any, key) => {
    showDialog3.value = true
    await nextTick()
    personnel.value.open()

}

//确认
const submituser = (e: any) => {
    formData.payer = e.id
    formData.payer_name = e.name
    showDialog3.value = false
}
// 表单数据
const formData = reactive({
    id: '',
    borrow_money_id: '',
    borrow_code: '',
    code: '',
    payer: '',
    payer_name: "",
    amount: '',
    desc: '',
    create_user: userStore.account,
    create_time: timeFormat('', 'yyyy-mm-dd hh:MM:ss'),
})


const customEvent = (e) => {
    formData.borrow_money_id = e.id
    formData.borrow_code = e.code
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({
    borrow_money_id: [{
        required: true,
        message: '请输入借款单id',
        trigger: ['blur']
    }],
    code: [{
        required: true,
        message: '请输入单据编号',
        trigger: ['blur']
    }],
    payer_name: [{
        required: true,
        message: '请输入还款人',
        trigger: ['change']
    }],
    amount: [{
        required: true,
        message: '请输入还款金额',
        trigger: ['blur']
    }],
    desc: [{
        required: true,
        message: '请输入说明',
        trigger: ['blur']
    }],
    create_user: [{
        required: true,
        message: '请输入经办人',
        trigger: ['blur']
    }],
    create_time: [{
        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 getDetail = async (row: Record<string, any>) => {
    const data = await apiFinancialRepaymentDetail({
        id: row.id
    })
    setFormData(data)
}


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

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

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



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