<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="100px" :rules="formRules">

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目预算书" prop="budget_doc_name">
                            <el-input v-model="formData.name" clearable placeholder="点击选择项目预算书" @click="showDialog = true"
                                readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="结算说明">
                            <el-input v-model="formData.desc" clearable placeholder="请输入结算说明" />
                        </el-form-item>
                    </el-col>
                    <createUserLable flag :formData="formData" name="结算"></createUserLable>
                </el-row>

                <formTable :formData="formData.detail" :config="tableConfig" :dictData="dictData">
                    <el-table-column label="部门">
                        <template #default="{ row }">
                            <el-form-item label-width="0">
                                <el-select v-model="row.dept_id" placeholder="请选择部门">
                                    <el-option :label="item.name" :value="item.id" v-for="item in deptList">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </template>
                    </el-table-column>
                </formTable>
                <el-dialog v-model="showDialog" title="项目预算书" width="70%">
                    <dialogTable :config="financial_budget_doc" @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="financialDepartmentIncomeSettlementEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {
    apiFinancialDepartmentIncomeSettlementAdd, apiFinancialDepartmentIncomeSettlementEdit, apiFinancialDepartmentIncomeSettlementDetail,
    apiFinancialDepartmentIncomeSettlementDetailLists, apiFinancialDepartmentIncomeSettlementDetailDelete
} from '@/api/financial_department_income_settlement'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { deptLists } from "@/api/org/department"
import { financial_budget_doc } from "@/components/dialogTable/dialogTableConfig";

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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑财务管理--部门收入结算' : '新增财务管理--部门收入结算'
})

// 表单数据
const formData = reactive({
    id: '',
    budget_doc_id: '',
    name: "",
    desc: '',
    create_user: '',
    create_time: '',
    "detail": []
})



const customEvent = (e) => {
    formData.budget_doc_id = e.id
    formData.name = e.name
    showDialog.value = false
}

const tableConfig = reactive(
    {
        title: "结算明细",
        tableConfig: [
            {
                label: "预算分成方式",
                value: 'type',
                select: 'budget_share_method'

            },
            {
                label: "预算分成金额",
                value: 'amount',
                type: 'number'

            },
            {
                label: "结算金额",
                value: 'settlement_amount',
                type: 'number'

            },
            {
                label: "结算单号",
                value: 'settlement_code',
                type: 'number'

            },
            {
                label: "备注",
                value: 'remark',
            },

        ],
        deleteApi: apiFinancialDepartmentIncomeSettlementDetailDelete,
    }
)



// 表单验证
const formRules = reactive<any>({
    budget_doc_id: [{
        required: true,
        message: '请输入预算书id',
        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]
        }
    }
    let res = await apiFinancialDepartmentIncomeSettlementDetailLists({ department_income_settlement_id: data.id })
    formData.detail = res.lists

}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiFinancialDepartmentIncomeSettlementDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {

    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiFinancialDepartmentIncomeSettlementEdit(data)
        : await apiFinancialDepartmentIncomeSettlementAdd(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>
