<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="160px" :rules="formRules">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="商户头像" prop="mer_avatar">
                            <material-picker v-model="formData.mer_avatar" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="商户名称" prop="mer_name">
                            <el-input v-model="formData.mer_name" clearable placeholder="请输入商户名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="商户分类" prop="category_id">
                            <el-select class="flex-1" v-model="formData.category_id" clearable placeholder="请选择商户分类">
                                <el-option v-for="(item, index) in dictData.mer_category_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="type_id">
                            <el-select class="flex-1" v-model="formData.type_id" clearable placeholder="请选择店铺类型">
                                <el-option v-for="(item, index) in dictData.merchat_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="credit_buy">
                            <el-radio-group v-model="formData.credit_buy" placeholder="请选择是否支持先货后款">
                                <el-radio v-for="(item, index) in dictData.show_status" :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="settle_cycle">
                            <el-input v-model="formData.settle_cycle" clearable placeholder="请输入结算周期，单位天" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="利率" prop="interest_rate">
                            <el-input v-model="formData.interest_rate" clearable placeholder="请输入利率" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="城市" prop="city_id">
                            <el-input v-model="formData.city_id" clearable placeholder="请输入城市" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="区县" prop="area_id">
                            <el-input v-model="formData.area_id" clearable placeholder="请输入区县" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="镇街" prop="street_id">
                            <el-input v-model="formData.street_id" clearable placeholder="请输入镇街" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="村" prop="village_id">
                            <el-input v-model="formData.village_id" clearable placeholder="请输入村" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="商户地址" prop="mer_address">
                            <el-input v-model="formData.mer_address" clearable placeholder="请输入商户地址" />
                        </el-form-item>
                    </el-col>

                    <!-- <el-col :span="8">
                        <el-form-item label="" prop="sort">
                            <el-input v-model="formData.sort" clearable placeholder="请输入" />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="商户是否禁用" prop="status">
                            <el-radio-group v-model="formData.status" placeholder="请选择商户是否禁用">
                                <el-radio v-for="(item, index) in dictData.off_status" :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="commission_rate">
                            <el-input v-model="formData.commission_rate" clearable placeholder="请输入提成比例" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="商户手续费单独设置" prop="commission_switch">
                            <el-radio-group v-model="formData.commission_switch" placeholder="请选择商户手续费单独设置">
                                <el-radio v-for="(item, index) in dictData.off_status" :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="long">
                            <el-input v-model="formData.long" clearable placeholder="请输入经度" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="纬度" prop="lat">
                            <el-input v-model="formData.lat" clearable placeholder="请输入纬度" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="店铺电话" prop="service_phone">
                            <el-input v-model="formData.service_phone" clearable placeholder="请输入店铺电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="商户余额" prop="mer_money">
                            <el-input v-model="formData.mer_money" clearable placeholder="请输入商户余额" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="商户备注" prop="mark">
                            <el-input v-model="formData.mark" type="textarea" clearable placeholder="请输入商户备注" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="银行卡转账信息" prop="financial_bank">
                            <el-input v-model="formData.financial_bank" clearable placeholder="请输入银行卡转账信息" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="微信转账信息" prop="financial_wechat">
                            <el-input v-model="formData.financial_wechat" clearable placeholder="请输入微信转账信息" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="支付宝转账信息" prop="financial_alipay">
                            <el-input v-model="formData.financial_alipay" clearable placeholder="请输入支付宝转账信息" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="merchantEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiMerchantAdd, apiMerchantEdit, apiMerchantDetail } from '@/api/merchant'
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({
    mer_id: '',
    category_id: '',
    type_id: '',
    mer_name: '',
    credit_buy: '',
    settle_cycle: '',
    interest_rate: '',
    city_id: '',
    area_id: '',
    street_id: '',
    village_id: '',
    mer_address: '',
    mer_avatar: '',
    mark: '',
    sort: '',
    status: '',
    commission_rate: '',
    commission_switch: '',
    long: '',
    lat: '',
    service_phone: '',
    mer_money: '',
    financial_bank: '',
    financial_wechat: '',
    financial_alipay: '',
})


// 表单验证
const formRules = reactive<any>({
    category_id: [{
        required: true,
        message: '请选择商户分类',
        trigger: ['blur']
    }],
    mer_name: [{
        required: true,
        message: '请输入商户名称',
        trigger: ['blur']
    }],
    credit_buy: [{
        required: true,
        message: '请选择是否支持先货后款',
        trigger: ['blur']
    }],
    settle_cycle: [{
        required: true,
        message: '请输入结算周期，单位天',
        trigger: ['blur']
    }],
    interest_rate: [{
        required: true,
        message: '请输入利率',
        trigger: ['blur']
    }],
    mer_address: [{
        required: true,
        message: '请输入商户地址',
        trigger: ['blur']
    }],
    mark: [{
        required: true,
        message: '请输入商户备注',
        trigger: ['blur']
    }],
    sort: [{
        required: true,
        message: '请输入',
        trigger: ['blur']
    }],
    status: [{
        required: true,
        message: '请选择商户是否禁用0锁定,1正常',
        trigger: ['blur']
    }],
    service_phone: [{
        required: true,
        message: '请输入店铺电话',
        trigger: ['blur']
    }],
    mer_money: [{
        required: true,
        message: '请输入商户余额',
        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 apiMerchantDetail({
        mer_id: row.mer_id
    })
    setFormData(data)
}


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