<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="90px" :rules="formRules">

                <el-row>
                    <el-col :span="8">
                        <el-form-item label="所属商户" prop="merchant">
                            <el-input v-model="formData.merchant" clearable placeholder="请输入所属商户" :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="归属门店" prop="store_id">
                            <el-input v-model="formData.store_id" clearable placeholder="请输入归属门店" :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="客户ID" prop="customer">
                            <el-input v-model="formData.customer" clearable placeholder="请输入客户ID" :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单据时间" prop="time">
                            <!-- <el-input v-model="formData.time" clearable placeholder="请输入单据时间" :readonly="false" /> -->
                            <el-date-picker :readonly="false" class="flex-1 !flex" v-model="formData.time" clearable
                                type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择单据时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单据编号" prop="number">
                            <el-input v-model="formData.number" clearable placeholder="请输入单据编号" :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单据金额" prop="total">
                            <el-input v-model="formData.total" type="number" clearable placeholder="请输入单据金额"
                                :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="抵扣金额" prop="deduction_price">
                            <el-input v-model="formData.deduction_price" clearable placeholder="请输入抵扣金额"
                                :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实际金额" prop="actual">
                            <el-input v-model="formData.actual" type="number" clearable placeholder="请输入实际金额"
                                :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="实收金额" prop="money">
                            <el-input v-model="formData.money" type="number" clearable placeholder="请输入实收金额"
                                :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="支付方式 " prop="pay_type">
                            <el-select class="flex-1" v-model="formData.pay_type" clearable placeholder="请选择支付方式"
                                :disabled="false">
                                <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="data">
                            <el-input v-model="formData.data" clearable placeholder="请输入备注信息" type="textarea"
                                :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核状态" prop="type">
                            <el-radio-group v-model="formData.type" placeholder="请选择审核状态" :disabled="false">
                                <el-radio v-for="(item, index) in dictData.auditing_type" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核人" prop="auditinguser">
                            <el-input v-model="formData.auditinguser" clearable placeholder="请输入审核人" :readonly="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核时间" prop="auditingtime">
                            <el-date-picker :readonly="false" class="flex-1 !flex" v-model="formData.auditingtime" clearable
                                type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择审核时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="扩展信息" prop="more">
                            <el-input v-model="formData.more" clearable placeholder="请输入扩展信息" type="textarea"
                                :readonly="false" />
                        </el-form-item>
                    </el-col>



                </el-row>


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

<script lang="ts" setup name="cashierclassEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCashierclassAdd, apiCashierclassEdit, apiCashierclassDetail } from '@/api/cashierclass'
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: '',
    merchant: '',
    store_id: '',
    customer: '',
    time: '',
    number: '',
    total: '',
    deduction_price: '',
    actual: '',
    money: '',
    pay_type: '',
    data: '',
    type: '',
    auditinguser: '',
    auditingtime: '',
    more: '',
})


// 表单验证
const formRules = reactive<any>({
    merchant: [{
        required: true,
        message: '请输入所属商户',
        trigger: ['blur']
    }],
    customer: [{
        required: true,
        message: '请输入客户ID',
        trigger: ['blur']
    }],
    time: [{
        required: true,
        message: '请输入单据时间',
        trigger: ['blur']
    }],
    number: [{
        required: true,
        message: '请输入单据编号',
        trigger: ['blur']
    }],
    total: [{
        required: true,
        message: '请输入单据金额',
        trigger: ['blur']
    }],
    actual: [{
        required: true,
        message: '请输入实际金额',
        trigger: ['blur']
    }],
    money: [{
        required: true,
        message: '请输入实收金额',
        trigger: ['blur']
    }],
    pay_type: [{
        required: true,
        message: '请选择支付方式 0余额 1微信 2小程序 3 h5  4支付宝 5 支付宝扫码 6 微信扫码 8先货后款 10对公转账',
        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]
        }
    }

    //@ts-ignore
    formData.auditingtime = timeFormat(formData.auditingtime, 'yyyy-mm-dd hh:MM:ss')
}

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


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