<template>
    <div class="edit-popup">

        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">

            <el-form ref="formRef" :model="formData" label-width="auto" :rules="formRules">
                <el-row :gutter="10">
                    <el-col :span="8">
                        <el-form-item label="供应商名称" prop="supplier_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" @click="showDialog = true">
                            <el-input v-model="supplier_name" readonly clearable placeholder="请选择供应商" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="供应商编码" prop="supplier_id">
                            <el-input v-model="supplier_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同类型" prop="contract_cate"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select v-model="formData.contract_cate" placeholder="请选择" @change="resetFormData"
                                class="flex-1">
                                <el-option label="采购合同" :value="1"></el-option>
                                <el-option label="分包合同" :value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>


                    <el-col :span="8">
                        <el-form-item label="合同编号" prop="contract_no">
                            <el-input v-model="contract_no" clearable @click="showDialog1 = true" readonly
                                placeholder="点击选择" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同名称" prop="contract_name">
                            <el-input v-model="contract_name" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="contract_id">
                            <el-input v-model="project_name" clearable disabled placeholder="请选择项目" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="contract_id">
                            <el-input v-model="project_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="退款日期" prop="refund_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.refund_date" clearable
                                value-format="YYYY-MM-DD" placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="退款金额" prop="refund_amount"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.refund_amount" clearable placeholder="请输入退款金额" type="number"
                                @input="amountinput" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="金额大写" prop="refund_amount_daxie">
                            <el-input v-model="formData.refund_amount_daxie" readonly clearable placeholder="请输入金额大写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="退款方式" prop="refund_type"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.refund_type" clearable placeholder="请选择退款方式">
                                <el-option v-for="(item, index) in dictData.refund_type" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="退款原因" prop="reason">
                            <el-input v-model="formData.reason" type='textarea' clearable placeholder="请输入退款原因" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" type="textarea" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <el-form-item label="附件">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <div style="margin: 20px 0;">付款银行信息</div>

                <el-row :gutter="10">

                    <el-col :span="8">
                        <el-form-item label="账户编码" prop="bank_account_id" @click="showDialog2 = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.bank_account_info.account_sn" readonly clearable
                                placeholder="点击选择银行" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户银行" prop="deposit_bank">
                            <el-input v-model="formData.bank_account_info.deposit_bank" disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户名称" prop="deposit_bank">
                            <el-input v-model="formData.bank_account_info.account_name" disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户账号" prop="bank_accnout">
                            <el-input v-model="formData.bank_account_info.account" disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-dialog v-model="showDialog" title="选择供应商" width="70%">
                    <dialogTable :config="supplier_tab" :query="{ project_id: project?.id || '' }"
                        @customEvent="customEvent">
                    </dialogTable>
                </el-dialog>

                <div v-if="formData.contract_cate == 1 && showDialog1">
                    <el-dialog v-model="showDialog1" title="选择采购合同" width="70%">
                        <dialogTable :config="procurement_contract" :query="{ supplier_id: formData?.supplier_id || '' }"
                            @customEvent="customEvent1" />
                    </el-dialog>
                </div>

                <div v-if="formData.contract_cate == 2 && showDialog1">
                    <el-dialog v-model="showDialog1" title="选择分包合同" width="70%">
                        <dialogTable :config="subcontracting_contract" :query="{ supplier_id: formData?.supplier_id || '' }"
                            @customEvent="customEvent1" />
                    </el-dialog>
                </div>

                <el-dialog v-model="showDialog2" title="选择银行账户" width="70%">
                    <dialogTable :config="bank_account" @customEvent="customEvent2" />
                </el-dialog>
            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import customDialog from '@/components/custom-dialog/index.vue'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { supplier_tab, procurement_contract, subcontracting_contract, finance_payment_plan, bank_account } from "@/components/dialogTable/dialogTableConfig"
import { apiFinancerefundAdd, apiFinancerefundEdit, apiFinancerefundDetail } from '@/api/refundrecord'
import { toChinesNum } from "@/utils/util";
import { getAllProjectTypes } from '@/api/projecttype'
import { timeFormat } from '@/utils/util'
import { isEmail, isIdCard, isPhone } from '@/utils/validate'
import type { PropType } from 'vue'
import configs from "@/config"
import useUserStore from "@/stores/modules/user";
import { useDictData } from '@/hooks/useDictOptions'
const protype = reactive([])
const userStore = useUserStore();
const formDataannex = reactive([])
const contract_name = ref('')
const project_name = ref('')
const project_code = ref('')
const contract_no = ref('')
const supplier_name = ref('')
const supplier_code = ref('')



const { dictData } = useDictData('refund_type,isaccrued,contract_type')

const props = defineProps({
    project: Object
})
if (props.project) {
    project_name.value = props.project.name
    project_code.value = props.project.project_code
}
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const refund_refund_amount_daxie = ref('')
const showDialog = ref(false)
const showDialog1 = ref(false)
const showDialog2 = ref(false)
const customEvent = (e: any) => {
    formData.supplier_id = e.id;
    supplier_code.value = e.supplier_code
    supplier_name.value = e.supplier_name
    resetFormData()
    showDialog.value = false;
};
const customEvent1 = (e: any) => {
    formData.contract_id = e.id;
    project_name.value = e.project_name;
    project_code.value = e.project_code;
    contract_name.value = e.contract_name;
    contract_no.value = e.contract_no
    showDialog1.value = false;
};



const customEvent2 = (e) => {
    formData.bank_account_id = e.id;
    formData.bank_account_info.account_sn = e.account_sn
    formData.bank_account_info.deposit_bank = e.deposit_bank
    formData.bank_account_info.account_name = e.account_name
    formData.bank_account_info.account = e.account
    showDialog2.value = false
}
const resetFormData = () => {
    project_name.value = ""
    project_code.value = ''
    contract_no.value = ""
    contract_name.value = ''
}
//监听输入
const amountinput = (e) => {
    if (e && e > 0) {
        formData.refund_amount_daxie = toChinesNum(e)
    }
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑退款记录表' : '新增退款记录表'
})

// 表单数据
const formData = reactive({
    id: '',
    "supplier_id": '',
    "contract_cate": '',
    "contract_id": '',
    "refund_amount": '',
    "refund_date": "",
    "refund_type": '',
    "reason": "",
    "remark": "",
    "annex": "",
    "bank_account_id": '',
    "bank_account_info": {
        "account_sn": "",
        "deposit_bank": "",
        "account_name": "",
        "account": ""
    },
})

// 表单验证
const formRules = reactive<any>({

})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    if (data.annex && data.annex.length > 0) {

        const arry1 = data.annex.map((item: any, index: any) => {
            return {
                name: `文件${index + 1}`,
                uri: item
            };
        });
        Object.assign(formDataannex, arry1)


    }


    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    supplier_name.value = data.supplier_name
    supplier_code.value = data.supplier_code
    project_name.value = data.project_name
    project_code.value = data.project_code
    contract_name.value = data.contract_name;
    contract_no.value = data.contract_no
    formData.refund_amount_daxie = toChinesNum(formData.refund_amount)

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


// 提交按钮
const handleSubmit = async () => {
    if (formDataannex.length > 0) {
        formData.annex = formDataannex.map((item: any) => item.uri)
    }
    await formRef.value?.validate()


    const data = { ...formData }
    mode.value == 'edit'
        ? await apiFinancerefundEdit(data)
        : await apiFinancerefundAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

    getAllProjectTypes().then((res) => {

        protype.splice(0, protype.length, ...res);
    })
}

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



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