<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" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <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"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <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" @click="showDialog1 = true">
                            <el-input v-model="contract_no" clearable 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="period"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-select class="flex-1" v-model="formData.period" clearable placeholder="请选择期次" disabled>
                                <el-option v-for="(item, index) in dictData.pay_period" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-button @click="showDialog2 = true" type="primary">选择待付款计划</el-button>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="付款日期" prop="pay_date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.pay_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="payment_nature"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.payment_nature" clearable placeholder="请选择付款性质">
                                <el-option v-for="(item, index) in dictData.payment_natrue" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="付款方式" prop="pay_type"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.pay_type" clearable placeholder="请选择付款方式">
                                <el-option v-for="(item, index) in dictData.pay_type" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="申请人" prop="apply_user">
                            <el-input v-model="formData.apply_user" clearable placeholder="请输入申请人" />

                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票状态" prop="invoice_status"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select class="flex-1" v-model="formData.invoice_status" clearable placeholder="请选择开票状态">
                                <el-option v-for="(item, index) in dictData.invoice_status" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="本次申请金额" prop="amount"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.amount" @input="amountinput" clearable placeholder="请输入本次申请金额" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="金额大写" prop="amount_daxie">
                            <el-input v-model="formData.amount_daxie" readonly clearable placeholder="请输入金额大写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="款项说明" prop="desc">
                            <el-input v-model="formData.desc" 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="24">

                        <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="payee_bank"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.payee_bank" clearable placeholder="请输入开户银行" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="银行支行名称" prop="payee_account_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.payee_account_name" clearable placeholder="请输入收款开户银行名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="收款账号" prop="payee_account"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.payee_account" clearable placeholder="请输入收款账号" />
                        </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_info"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.bank_account_info.account_sn" @click="showDialog4 = true" readonly
                                clearable placeholder="请输入收款账号" />
                        </el-form-item>

                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="开户银行" prop="collection_bank">
                            <el-input v-model="formData.bank_account_info.deposit_bank" clearable readonly
                                placeholder="请输入收款银行" />
                        </el-form-item>

                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户名称" prop="collection_bank">
                            <el-input v-model="formData.bank_account_info.account_name" clearable readonly
                                placeholder="请输入收款银行" />
                        </el-form-item>

                    </el-col> <el-col :span="8">
                        <el-form-item label="开户账号" prop="collection_bank">
                            <el-input v-model="formData.bank_account_info.account" clearable readonly
                                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>
                <div v-if="showDialog2">
                    <el-dialog v-model="showDialog2" title="选择待付款计划" width="70%">
                        <dialogTable :config="finance_payment_plan"
                            :query="{ contract_id: formData?.contract_id, status: 1 }" @customEvent="customEvent2" />
                    </el-dialog>
                </div>

                <el-dialog v-model="showDialog4" title="选择付款银行" width="70%">
                    <dialogTable :config="bank_account" @customEvent="customEvent4" />
                </el-dialog>
            </el-form>
        </popup>

    </div>
</template>
<!-- paylist -->

<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 { useDictData } from '@/hooks/useDictOptions'
import { supplier_tab, procurement_contract, subcontracting_contract, finance_payment_plan, bank_account } from "@/components/dialogTable/dialogTableConfig"
import { paymentapplyAdd, paymentapplyEdit, paymentapplyDetail } from '@/api/paymentrequest'
import { toChinesNum } from "@/utils/util";
import { timeFormat } from '@/utils/util'
import { isEmail, isIdCard, isPhone } from '@/utils/validate'
import type { PropType } from 'vue'
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();
const formDataannex = reactive([])
const contract_name = ref('')
const project_name = ref('')
const project_code = ref('')
const supplier_name = ref('')
const supplier_code = ref('')
const project_amount = ref('')
const contract_no = ref('')
const custom_name = ref('')
const userInfo = userStore.userInfo

let props = defineProps({
    project: Object
})

if (props.project) {
    project_name.value = props.project.name
    project_code.value = props.project.project_code
}


const { dictData } = useDictData('pay_period,pay_type,payment_natrue,invoice_status')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const amount_daxie = ref('')
const showDialog = ref(false)
const showDialog1 = ref(false)
const showDialog2 = ref(false)
const showDialog4 = 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: any) => {
    formData.finance_payment_plan_id = e.id;
    formData.period = e.period
    formData.amount = e.amount
    project_name.value = e.project_name;
    project_code.value = e.project_code;
    formData.amount_daxie = toChinesNum(e.amount)
    formData.period = e.period
    showDialog2.value = false;
};


const customEvent4 = (e: any) => {
    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
    showDialog4.value = false
};
const resetFormData = () => {
    project_name.value = ""
    project_code.value = ''
    contract_no.value = ""
    contract_name.value = ''
    formData.period = ""
}

//监听输入
const amountinput = (e) => {
    if (e && e > 0) {
        formData.amount_daxie = toChinesNum(e)
    }
}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑付款申请表' : '新增付款申请表'
})

// 表单数据
const formData = reactive({
    id: "",
    "supplier_id": '',
    "contract_cate": '',
    "contract_id": '',
    "finance_payment_plan_id": '',
    "pay_date": "",
    "payment_nature": '',
    "pay_type": '',
    period: "",
    "apply_user": "",
    "invoice_status": '',
    "amount": '',
    "desc": "",
    "remark": "",
    "annex": [],
    "payee_bank": "",
    "payee_account_name": "",
    "payee_account": "",
    "bank_account_id": '',
    "bank_account_info": {
        "account_sn": "",
        "deposit_bank": "",
        "account_name": "",
        "account": ""
    },
})


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

})


// 获取详情
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]
        }
    }

    project_name.value = data.project_name
    project_code.value = data.project_code
    contract_name.value = data.contract_name;
    contract_no.value = data.contract_no
    supplier_name.value = data.supplier_name
    supplier_code.value = data.supplier_code
    formData.amount_daxie = toChinesNum(formData.amount)



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


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

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

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

formData.applier = userInfo.name

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