<template>
    <a-modal :open="modal.open" :title="modal.title" :width="640" :confirm-loading="modal.confirmLoading"
             :after-close="onAfterClose" :cancel-text="cancelText" :ok-text="okText" @ok="handleOk" @cancel="handleCancel">
        <a-form ref="formRef" :model="formData" :rules="formRules">
            <a-card class="mb-8-2">
                <a-row :gutter="12">
                    <a-col :span="24">
                        <a-form-item :label="'零容忍'" name="reportContent">
                            <a-textarea :placeholder="'请输入零容忍内容'" v-model:value="formData.reportContent"></a-textarea>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'举报邮箱'" name="email">
                            <a-input :placeholder="'请输入举报邮箱'" v-model:value="formData.email"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'经度'" name="lat">
                            <a-input-number :placeholder="'请输入经度'" v-model:value="formData.lat"></a-input-number>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'纬度'" name="lon">
                            <a-input-number :placeholder="'请输入经度'" v-model:value="formData.lon"></a-input-number>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'地址'" name="address">
                            <a-input :placeholder="'请输入地址'" v-model:value="formData.address"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'电话'" name="phone">
                            <a-input :placeholder="'请输入电话'" v-model:value="formData.phone"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'编码'" name="reportNum">
                            <a-input :placeholder="'请输入编码'" v-model:value="formData.reportNum"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'状态'" name="status">
                            <a-radio-group v-model:value="formData.status" :options="[
                                { label: '启用', value: 'enabled' },
                                { label: '停用', value: 'disabled' },
                            ]"></a-radio-group>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item :label="'图片'" name="reportImage">
                            <x-upload-image v-model="reportImage" @imgChange="imgChange" />
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-card>
        </a-form>
    </a-modal>
</template>

<script setup>
import { cloneDeep } from 'lodash-es'
import { ref } from 'vue'
import { config } from '@/config'
import apis from '@/apis'
import { useForm, useModal } from '@/hooks'
import { message } from 'ant-design-vue'
import { useI18n } from 'vue-i18n'
import dayjs from 'dayjs'

const emit = defineEmits(['ok'])
const { t } = useI18n() // 解构出t方法
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const cancelText = ref(t('button.cancel'))
const okText = ref(t('button.confirm'))
const rolesValue = ref([])
const roles = ref([])
const img = ref('')
formRules.value = {
    reportContent: { required: true, message: '请输入零容忍内容' },
    email: { required: true, message: '请输入举报邮箱' },
    lat: { required: true, message: '请输入经度' },
    lon: { required: true, message: '请输入纬度' },
    address: { required: true, message: '请输入地址' },
    phone: { required: true, message: '请输入电话' },
    reportNum: { required: true, message: '请输入编码' },
    status: [{ required: true, message: '请选择状态', trigger: 'change' }],
    sequence: [{ required: true, message: '请选择顺序', trigger: 'change' }],
    reportImage: [{ required: false, message: '请上传图片', trigger: 'change' }],
}
/**
 * select 选择框
 */
const handleChange = (value) => {
    rolesValue.value = value
}
const imgChange = (value) => {
    formData.value.img = value
}
/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '添加公司动态',
    })
    formData.value.status = 'enabled'
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: t('pages.system.user.edit'),
    })
    const { data, success } = await apis.webSite.getMenu(record.id).catch()
    if (!success) {
        hideModal()
        return
    }
    formData.value = { ...data }
    formData.value.pushAt=dayjs(data.pushAt)
    console.log(formData.value)
    img.value = config('http.apiBasic') + data.img
}

/**
 * 确定
 */
function handleOk() {
    if (!formData.value.img) return message.error('请上传图片');
    formRef.value.validateFields().then(async (values) => {
        console.log(values)
        try {
            showLoading()
            const params = {
                ...values,
                reportImage: formData.value.reportImage,
                // pushAt:dayjs(formData.value.pushAt).format("YYYY-MM-DD"),
            }
            let result = null
            console.log(modal.value.type)
            switch (modal.value.type) {
                case 'create':
                    result = await apis.webSite.createMenu(params).catch(() => {
                        throw new Error()
                    })
                    break
                case 'edit':
                    // result = await apis.webSite.updateMenu(formData.value.id, params).catch(() => {
                    result = await apis.webSite.updateMenu(formData.value.id, params).catch(() => {
                        throw new Error()
                    })
                    break
            }
            hideLoading()
            if (config('http.code.success') === result?.success) {
                hideModal()
                emit('ok')
            }
        } catch (error) {
            console.log(error)
            hideLoading()
        }
    })
        .catch((e) => {
            console.log(e)
            hideLoading()
        })
}

/**
 * 对权限组 过数据格式
 */
function formatArr(data, type = '') {
    const rolesArr = []
    data.forEach((item) => {
        roles.value.forEach((r) => {
            if (type === 'edit') {
                if (item.role_id === r.value) {
                    rolesArr.push({
                        value: item.role_id,
                        label: r.label,
                    })
                    return
                }
            } else if (r.value === item) {
                rolesArr.push({
                    role_id: item,
                    role_name: r.label,
                })
                return
            }
        })
    })
    return rolesArr
}

/**
 * 取消
 */
function handleCancel() {
    img.value = ''
    hideModal()
}

/**
 * 关闭后
 */
function onAfterClose() {
    resetForm()
    hideLoading()
}

defineExpose({
    handleCreate,
    handleEdit,
})
</script>

<style lang="less" scoped></style>
