<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.mer_name" clearable placeholder="请输入用户类型 1-商户 2-供应商" :readonly="true" />
                </el-form-item>
                <el-form-item label="提现银行">
                    <el-input v-model="formData.merchant_bank_info.bank_info.name" clearable placeholder="请输入商户id"
                        :readonly="true" />
                </el-form-item>
                <el-form-item label="提现金额">
                    <el-input v-model="formData.amount" clearable placeholder="请输入供应商id" :readonly="true" />
                </el-form-item>
                <el-form-item label="提现账号">
                    <el-input v-model="formData.merchant_bank_info.bank_code" clearable placeholder="请输入提现账户id"
                        :readonly="true" />
                </el-form-item>
                <el-form-item label="开户网点">
                    <el-input v-model="formData.merchant_bank_info.bank_branch" clearable placeholder="请输入提现金额"
                        :readonly="true" />
                </el-form-item>
                <el-form-item label="账户名称">
                    <el-input v-model="formData.merchant_bank_info.name" clearable placeholder="账户类型"
                        :readonly="true" />
                </el-form-item>
                <el-form-item label="账户类型">
                    <el-select v-model="formData.merchant_bank_info.is_own">
                        <el-option label="对公" :value="1"></el-option>
                        <el-option label="个人" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核状态">
                    <el-select v-model="formData.is_check">
                        <el-option label="审核不通过" :value="2"></el-option>
                        <el-option label="审核通过" :value="1"></el-option>
                        <el-option label="待审核" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否到账">
                    <el-select v-model="formData.is_arrival">
                        <el-option label="到账" :value="1"></el-option>
                        <el-option label="未到账" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="操作人员">
                    <el-input v-model="formData.admin_name" clearable placeholder="请输入操作人员id" :readonly="true" />
                </el-form-item> -->
                <el-form-item label="审核意见" prop="is_check" v-if="formData.is_check == 0">
                    <el-radio-group v-model="form.is_check" class="ml-4">
                        <el-radio :label="1" size="large">通过</el-radio>
                        <el-radio :label="2" size="large">不通过</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="拒绝原因" v-if="form.is_check == 2" prop="fail_msg">
                    <el-input type="textarea" v-model="form.fail_msg" clearable placeholder="请输入拒绝原因"
                        :readonly="false" />
                </el-form-item>

                <el-form-item label="上传凭证" prop="is_check" v-if="formData.is_check == 1">
                    <material-picker :limit="5" v-model="form.arrival_proof" />
                    <!-- <el-radio-group v-model="formData.is_check" class="ml-4">
                        <el-radio :label="1" size="large">通过</el-radio>
                        <el-radio :label="2" size="large">不通过</el-radio>
                    </el-radio-group> -->
                </el-form-item>

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

<script lang="ts" setup name="merchantWithdrawEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMerchantWithdrawAdd, apimerchantwithdraw, apiMerchantWithdrawDetail, apimerchantwithdrawCheck } from '@/api/merchant_withdraw'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
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: "",
    mer_name: "",
    merchant_bank_info: {},
    amount: "",
    is_arrival: "",
    is_check: '',
    fail_msg: "",
    admin_name: "",
    arrival_proof: []
})


const form = reactive({
    id: '',
    is_check: '',
    fail_msg: "",
    arrival_proof: []



})


// 表单验证
const formRules = reactive<any>({
    user_type: [{
        required: true,
        message: '请输入用户类型 1-商户 2-供应商',
        trigger: ['blur']
    }],
    mer_id: [{
        required: true,
        message: '请输入商户id',
        trigger: ['blur']
    }],
    supplier_id: [{
        required: true,
        message: '请输入供应商id',
        trigger: ['blur']
    }],
    merchant_bank_id: [{
        required: true,
        message: '请输入提现账户id',
        trigger: ['blur']
    }],
    amout: [{
        required: true,
        message: '请输入提现金额',
        trigger: ['blur']
    }],
    is_check: [{
        required: true,
        message: '请输入审核状态 0-待审核 1-已审核 2-审核不通过',
        trigger: ['blur']
    }],
    is_arrival: [{
        required: true,
        message: '请输入是否到账 0-未到账 1-已到账',
        trigger: ['blur']
    }],
    admin_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]
        }
    }
    if (data.arrival_proof.length) {
        form.arrival_proof = JSON.parse(data.arrival_proof)
    }
    form.id = data.id
}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...form, }
    formData.is_check == 0 ?
        await apimerchantwithdrawCheck(data)
        : await apimerchantwithdraw(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>
