<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="700px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <!-- <el-form-item label="借款单号" prop="loan_apply_code">
                    <el-input v-model="formData.loan_apply_code" clearable placeholder="请输入借款单号" />
                </el-form-item> -->
                <el-form-item label="项目名称" prop="project_id">
                    <el-input v-model="formData.project_name" clearable placeholder="点击选择" readonly
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="借款人" prop="apply_user">
                    <el-input v-model="formData.apply_user" clearable placeholder="请输入借款人" />
                </el-form-item>
                <el-form-item label="借款申请日期" prop="loan_date">
                    <!-- <el-input v-model="formData.loan_date" clearable placeholder="请输入借款申请日期" /> -->
                    <el-date-picker class="flex-1 !flex" v-model="formData.loan_date" clearable value-format="YYYY-MM-DD "
                        placeholder="选择日期">
                    </el-date-picker>


                </el-form-item>
                <el-form-item label="借款金额" prop="loan_amount">
                    <el-input v-model="formData.loan_amount" clearable placeholder="请输入借款金额" />
                </el-form-item>
                <el-form-item label="收款人姓名" prop="payee_name">
                    <el-input v-model="formData.payee_name" clearable placeholder="请输入收款人姓名" />
                </el-form-item>
                <el-form-item label="收款银行" prop="payee_bank">
                    <el-input v-model="formData.payee_bank" clearable placeholder="请输入收款银行" />
                </el-form-item>
                <el-form-item label="收款账号" prop="payee_account">
                    <el-input v-model="formData.payee_account" clearable placeholder="请输入收款账号" />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                </el-form-item>
                <el-col :span="24">
                    <el-form-item label="附件" prop="field127">
                        <el-upload
                            accept="doc, docx, xls, xlsx, ppt, pptx, pdf, txt, zip, rar, tar, jpg, png, gif, jpeg, webp, wmv, avi, mpg, mpeg, 3gp, mov, mp4, flv, f4v, rmvb, mkv"
                            class="upload-demo" :show-file-list="false" aria-hidden="true"
                            :headers="{ Token: userStore.token }" :action="base_url + '/upload/file'"
                            :on-success="handleAvatarSuccess_four" ref="upload">
                            <el-button type="primary">
                                上传
                            </el-button>
                        </el-upload>
                        <div>
                            <div v-for="(item, index) in formData.annex" style="margin-left: 5px;display: block;">
                                <a style="margin-left: 10px; color: #4a5dff; align-self: flex-start" :href="item.uri"
                                    target="_blank">{{ item.name }}</a>
                                <span style="cursor: pointer;margin-left: 5px;" @click="delFileFn(index)">x</span>
                            </div>
                        </div>
                    </el-form-item>
                </el-col>
                <el-form-item label="账户编码" prop="bank_account_id">
                    <el-input v-model="formData.bank_account.account_sn" clearable placeholder="点击选择"
                        @click="showDialog1 = true" readonly />
                </el-form-item>
                <el-form-item label="开户银行">
                    <el-input v-model="formData.bank_account.deposit_bank" clearable readonly />
                </el-form-item>
                <el-form-item label="开户名称">
                    <el-input v-model="formData.bank_account.account_name" readonly />
                </el-form-item>
                <el-form-item label="开户账号">
                    <el-input v-model="formData.bank_account.account" readonly />
                </el-form-item>
            </el-form>

            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <projectDialog @customEvent="customEvent"></projectDialog>
            </el-dialog>
            <el-dialog v-model="showDialog1" title="选择银行" width="70%">
                <bankAccount @customEvent="customEvent1"></bankAccount>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectLoanApplyEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectLoanApplyAdd, apiProjectLoanApplyEdit, apiProjectLoanApplyDetail } from '@/api/project_loan_apply'
import { timeFormat } from '@/utils/util'
import projectDialog from '@/components/project/index.vue'
import bankAccount from "@/components/bankaccount/index.vue"
import type { PropType } from 'vue'
import configs from "@/config"
import useUserStore from "@/stores/modules/user";


const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore()
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 showDialog1 = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目财务-借款申请' : '新增项目财务-借款申请'
})

// 表单数据
const formData = reactive({
    id: '',
    "project_id": '',
    project_name: "",
    "apply_user": "",
    "loan_date": "",
    "loan_amount": 0,
    "payee_name": "",
    "payee_bank": "",
    "payee_account": "",
    "remark": "",
    "annex": [],
    "bank_account_id": '',
    bank_account: {
        "account_sn": "",
        "deposit_bank": "",
        "account_name": "",
        "account": ""
    }

})
// 上传文件
const handleAvatarSuccess_four = (response: any) => {
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};
const customEvent = (e: any) => {
    console.log(e)
    formData.project_id = e.id
    formData.project_name = e.name
    showDialog.value = false
}
const customEvent1 = (e: any) => {
    formData.bank_account_id = e.id
    formData.bank_account = {
        "account_sn": e.account_sn,
        "deposit_bank": e.deposit_bank,
        "account_name": e.account_name,
        "account": e.account
    }
    showDialog1.value = false
}
// 表单验证
const formRules = reactive<any>({
    loan_apply_code: [{
        required: true,
        message: '请输入借款单号',
        trigger: ['blur']
    }],
    project_id: [{
        required: true,
        message: '请输入项目id',
        trigger: ['blur']
    }],
    apply_user: [{
        required: true,
        message: '请输入借款人',
        trigger: ['blur']
    }],
    loan_date: [{
        required: true,
        message: '请输入借款申请日期',
        trigger: ['blur']
    }],
    loan_amount: [{
        required: true,
        message: '请输入借款金额',
        trigger: ['blur']
    }],
    payee_name: [{
        required: true,
        message: '请输入收款人姓名',
        trigger: ['blur']
    }],
    payee_bank: [{
        required: true,
        message: '请输入收款银行',
        trigger: ['blur']
    }],
    payee_account: [{
        required: true,
        message: '请输入收款账号',
        trigger: ['blur']
    }],
    bank_account_id: [{
        required: true,
        message: '请输入付款银行账户id',
        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 apiProjectLoanApplyDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectLoanApplyEdit(data)
        : await apiProjectLoanApplyAdd(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>
