<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="640px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="110px" :rules="formRules">
                <el-form-item label="业绩日期" prop="yeji_date">
                    <el-date-picker
                        v-model="formData.yeji_date"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择日期"
                        :disabled="mode === 'edit'"
                        class="w-full"
                    />
                </el-form-item>
                <el-form-item label="自媒体来源" prop="media_source">
                    <media-source-select
                        v-model="formData.media_source"
                        :options="mediaSourceOptions"
                        :loading="mediaSourceLoading"
                        placeholder="请选择自媒体来源"
                        :allow-create="false"
                        :disabled="mode === 'edit'"
                        select-class="w-full"
                    />
                </el-form-item>
                <el-row :gutter="16">
                    <el-col :span="12">
                        <el-form-item label="加粉数" prop="add_fans_count">
                            <el-input-number
                                v-model="formData.add_fans_count"
                                :min="0"
                                :step="1"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="总开口" prop="total_open_count">
                            <el-input-number
                                v-model="formData.total_open_count"
                                :min="0"
                                :step="1"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="未回复" prop="unreplied_count">
                            <el-input-number
                                v-model="formData.unreplied_count"
                                :min="0"
                                :step="1"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="付费挂号" prop="paid_appointment_count">
                            <el-input-number
                                v-model="formData.paid_appointment_count"
                                :min="0"
                                :step="1"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="免费挂号" prop="free_appointment_count">
                            <el-input-number
                                v-model="formData.free_appointment_count"
                                :min="0"
                                :step="1"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="面诊" prop="interview_count">
                            <el-input-number
                                v-model="formData.interview_count"
                                :min="0"
                                :step="1"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="订单金额" prop="order_amount">
                            <el-input-number
                                v-model="formData.order_amount"
                                :min="0"
                                :step="0.01"
                                :precision="2"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="接诊诊单" prop="completed_order_count">
                            <el-input-number
                                v-model="formData.completed_order_count"
                                :min="0"
                                :step="1"
                                class="w-full"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="备注" prop="remark">
                    <el-input
                        v-model="formData.remark"
                        type="textarea"
                        :autosize="{ minRows: 3, maxRows: 5 }"
                        maxlength="255"
                        show-word-limit
                        placeholder="选填"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'

import { personalYejiAdd, personalYejiEdit } from '@/api/self_input_stats'
import Popup from '@/components/popup/index.vue'

import MediaSourceSelect from './MediaSourceSelect.vue'
import type { MediaSourceOption } from './useMediaSourceOptions'

withDefaults(
    defineProps<{
        mediaSourceOptions?: MediaSourceOption[]
        mediaSourceLoading?: boolean
    }>(),
    {
        mediaSourceOptions: () => [],
        mediaSourceLoading: false,
    }
)

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref<'add' | 'edit'>('add')

const popupTitle = computed(() => (mode.value === 'edit' ? '编辑业绩' : '新增业绩'))

const formData = reactive({
    id: '',
    yeji_date: '',
    media_source: '',
    add_fans_count: 0,
    total_open_count: 0,
    unreplied_count: 0,
    paid_appointment_count: 0,
    free_appointment_count: 0,
    interview_count: 0,
    order_amount: 0,
    completed_order_count: 0,
    remark: '',
})

const formRules = {
    yeji_date: [{ required: true, message: '请选择业绩日期', trigger: ['change'] }],
    media_source: [{ required: true, message: '请填写自媒体来源', trigger: ['blur'] }],
    add_fans_count: [{ required: true, message: '请填写加粉数', trigger: ['blur', 'change'] }],
}

const resetForm = () => {
    formData.id = ''
    formData.yeji_date = ''
    formData.media_source = ''
    formData.add_fans_count = 0
    formData.total_open_count = 0
    formData.unreplied_count = 0
    formData.paid_appointment_count = 0
    formData.free_appointment_count = 0
    formData.interview_count = 0
    formData.order_amount = 0
    formData.completed_order_count = 0
    formData.remark = ''
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    if (mode.value === 'edit') {
        await personalYejiEdit(formData)
    } else {
        await personalYejiAdd(formData)
    }
    popupRef.value?.close()
    emit('success')
}

const open = (type: 'add' | 'edit' = 'add') => {
    mode.value = type
    resetForm()
    popupRef.value?.open()
}

const setFormData = (data: Record<string, any>) => {
    formData.id = data.id ?? ''
    formData.yeji_date = data.yeji_date ?? ''
    formData.media_source = data.media_source ?? ''
    formData.add_fans_count = Number(data.add_fans_count ?? 0)
    formData.total_open_count = Number(data.total_open_count ?? 0)
    formData.unreplied_count = Number(data.unreplied_count ?? 0)
    formData.paid_appointment_count = Number(data.paid_appointment_count ?? 0)
    formData.free_appointment_count = Number(data.free_appointment_count ?? 0)
    formData.interview_count = Number(data.interview_count ?? 0)
    formData.order_amount = Number(data.order_amount ?? 0)
    formData.completed_order_count = Number(data.completed_order_count ?? 0)
    formData.remark = data.remark ?? ''
}

const handleClose = () => {
    emit('close')
}

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