<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-spin :spinning="spining">
            <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="customerName">
                                <a-input :placeholder="'请输入用户名'" v-model:value="formData.customerName"
                                    ></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'工单分类'" name="label">
                                <a-input :placeholder="'请输入工单分类'" v-model:value="formData.label" ></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'工单内容'" name="content">
                                <a-textarea :placeholder="'请输入工单内容'" v-model:value="formData.content"
                                    :auto-size="{ minRows: 2, maxRows: 5 }" ></a-textarea>
                            </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="createAt">
                                <a-input :placeholder="'请输入创建时间'" v-model:value="formData.createAt" ></a-input>
                            </a-form-item>
                        </a-col>
                        <a-col :span="24">
                            <a-form-item :label="'上门时间'" name="startAt">
                                <a-input :placeholder="'请输入上门时间'" v-model:value="formData.startAt" ></a-input>
                            </a-form-item>
                        </a-col>
                        
                       
                    </a-row>
                </a-card>
            </a-form>
        </a-spin>

    </a-modal>
</template>

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

import { spliceUrl } from '@/utils/util'
import { readonly } from 'vue'
const areaFormRef = ref()
const emit = defineEmits(['ok'])
const { t } = useI18n() // 解构出t方法
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const { spining, showSpining, hideSpining } = useSpining()
const cancelText = ref(t('button.cancel'))
const okText = ref(t('button.confirm'))
const treeData = ref([])
const status=ref(2)
const remark=ref('')
formRules.value = {
    label: { required: true, message: '请输入产品类别名称' },
    sequence: { required: true, message: '请输入排序' }
}

/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '产品分类',
    })
    // 初始化所有字段
    initData()
}

/**
 * 编辑
 */
async function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑分类',
    })
}
/**
 * 确定
 */
function handleOk() {
    formRef.value.validateFields().then(async (values) => {
        try {
            showLoading()
            const params = {
                ...values,
                status:status.value,
                remark:status.remark
            }
            let result = null
            switch (modal.value.type) {
                case 'create':
                    result = await apis.orderList.createProductsItem(params).catch((error) => {
                        throw new Error(error)
                    })
                    break
                case 'edit':
                    result = await apis.orderList.updateItem(formData.value.id, params).catch(() => {
                        throw new Error(error)
                    })
                    break
            }
            hideLoading()
            if (config('http.code.success') === result?.success) {
                hideModal()
                emit('ok')
            }
        } catch (error) {
            message.error({ content: error.message })
            hideLoading()
        }
    })
        .catch((e) => {
            console.log(e)
            hideLoading()
        })
}


/**
 * 取消
 */
function handleCancel() {
    hideModal()
}

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

defineExpose({
    handleCreate,
    handleEdit,
})


</script>

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