<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="单据编号">
                    <el-input v-model="formData.code" clearable placeholder="系统自动生成" disabled />
                </el-form-item>
                <el-form-item label="所在部门" prop="dept">
                    <el-select v-model="formData.dept" placeholder="请选择部门" class="flex-1">
                        <el-option :label="item.name" :value="item.id" v-for="item in deptList">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="费用类别" prop="cost_type">
                    <el-select v-model="formData.cost_type" placeholder="请选择费用类别" class="flex-1">
                        <el-option :label="item.name" :value="parseInt(item.value)" v-for="item in dictData.cost_type">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="支付方式" prop="pay_type">
                    <el-select v-model="formData.pay_type" placeholder="请选择支付方式" class="flex-1">
                        <el-option :label="item.name" :value="parseInt(item.value)"
                            v-for="item in dictData.financial_pay_type">
                        </el-option>
                    </el-select>
                </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="事由" prop="content">
                    <el-input v-model="formData.content" clearable placeholder="请输入事由" type="textarea" />
                </el-form-item>
                <el-form-item label="附件">
                    <uploadAnnex :formData="formData"></uploadAnnex>
                </el-form-item>
                <createUserLable :formData="formData" name="申请"></createUserLable>

            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="financialBorrowMoneyEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiFinancialBorrowMoneyAdd, apiFinancialBorrowMoneyEdit, apiFinancialBorrowMoneyDetail } from '@/api/financial_borrow_money'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { deptLists } from "@/api/org/department"

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: '',
    code: '',
    dept: '',
    cost_type: '',
    pay_type: '',
    amount: '',
    content: '',
    annex: '',
    create_user: '',
    create_time: '',
})


// 表单验证
const formRules = reactive<any>({
    code: [{
        required: true,
        message: '请输入单据编号',
        trigger: ['blur']
    }],
    dept: [{
        required: true,
        message: '请输入所在部门',
        trigger: ['blur']
    }],
    cost_type: [{
        required: true,
        message: '请输入费用类别',
        trigger: ['blur']
    }],
    pay_type: [{
        required: true,
        message: '请输入支付方式',
        trigger: ['blur']
    }],
    amount: [{
        required: true,
        message: '请输入金额',
        trigger: ['blur']
    }],
    content: [{
        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 apiFinancialBorrowMoneyDetail({
        id: row.id
    })
    setFormData(data)
}


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

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

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

const deptList = ref([])
const getDeptList = async () => {
    let res = await deptLists()
    deptList.value = res.lists
}
getDeptList()

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