<template>
    <a-modal :open="modal.open" :title="modal.title" :width="750" :confirm-loading="modal.confirmLoading"
        :after-close="onAfterClose" :cancel-text="cancelText" @ok="handleOk" @cancel="handleCancel">
        <a-card>
            <a-form ref="formRef" :model="formData" :rules="formRules">
                <a-row :gutter="24">
                    <a-col :span="24">
                        <a-form-item label="类型" name="categoryType">
                            <a-select v-model:value="formData.categoryType" allowClear>
                                <a-select-option v-for="item in dicsStore.dictOptions.PROJECT_TYPE" :key="item.dval"
                                    :value="item.dval">{{
                                        item.introduction }}</a-select-option>
                            </a-select>
                            <span style="color: #ff4d4f;font-size: 12px;">温馨提示：类型保存后不可修改，请认真选择！</span>
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="服务项目分类" name="categoryId">
                            <a-select v-model:value="formData.categoryId" allowClear>
                                <a-select-option v-for="item in categoryList" :key="item.id" :value="item.id">{{
                                    item.name }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="服务项目名称" name="name">
                            <a-input v-model:value="formData.name"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="单价" name="price">
                            <a-input-number v-model:value="formData.price" style="width: 100%;">
                                <template #addonAfter>
                                    <a-select v-model:value="formData.priceUnit" style="width: 100px">
                                        <a-select-option value="次">元/次</a-select-option>
                                        <a-select-option value="时">元/小时</a-select-option>
                                    </a-select>
                                </template>
                            </a-input-number>
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="单次服务时长" name="duration">
                            <a-input-number v-model:value="formData.duration" style="width: 100%;">
                                <template #addonAfter>
                                    <span>分钟</span>
                                </template>
                            </a-input-number>
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="服务频次" name="frequency">
                            <a-input-number v-model:value="formData.frequency" style="width: 100%;">
                                <template #addonAfter>
                                    <a-select v-model:value="formData.frequencyUnit" style="width: 120px">
                                        <a-select-option value="元/次">小时/次</a-select-option>
                                        <a-select-option value="元/台">小时/台</a-select-option>
                                    </a-select>
                                </template>
                            </a-input-number>
                        </a-form-item>
                    </a-col>
                    <a-col span="12">
                        <a-form-item label="服务形式" name="serviceFormat">
                            <a-select v-model:value="formData.serviceFormat" style="width: 100%;">
                                <a-select-option v-for="item in dicsStore.dictOptions.Service_Format" :key="item.dval"
                                    :value="item.dval">{{
                                        item.introduction }}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="计时标准" name="timeKeeping">
                            <a-input v-model:value="formData.timeKeeping" style="width: 100%;">
                                <template #addonAfter>
                                    <a-select v-model:value="formData.timeKeepingUnit" style="width: 120px">
                                        <a-select-option value="小时/次">小时/次</a-select-option>
                                        <a-select-option value="小时/平方">小时/平方</a-select-option>
                                        <a-select-option value="小时/台">小时/台</a-select-option>
                                        <a-select-option value="小时/组">小时/组</a-select-option>
                                        <a-select-option value="小时/件">小时/件</a-select-option>
                                        <a-select-option value="小时/个">小时/个</a-select-option>
                                        <a-select-option value="小时/张">小时/张</a-select-option>
                                        <a-select-option value="小时/辆">小时/辆</a-select-option>
                                        <a-select-option value="小时/项">小时/项</a-select-option>
                                    </a-select>
                                </template>
                            </a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="服务内容" name="content">
                            <a-textarea v-model:value="formData.content" placeholder="请输入内容/简介，最多500字" :rows="4" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="封面图片" name="imgList">
                            <gx-upload v-model="formData.imgList" accept-types=".jpg,.png,.webp" :fileNumber="1" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="详情图片" name="detailImgList">
                            <gx-upload v-model="formData.detailImgList" accept-types=".jpg,.png,.webp"
                                :fileNumber="20" />
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item label="服务项目视频" name="detailImgList">
                            <a-upload v-model:file-list="fileList"
                                action="https://www.mocky.io/v2/5cc8019d300000980a055e76" accept=".mp4,.3gp,.m3u8">
                                <a-button>
                                    <upload-outlined></upload-outlined>
                                    上传
                                </a-button>
                            </a-upload>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </a-card>

    </a-modal>
</template>

<script setup>
import GxUpload from '@/components/GxUpload/index.vue'
import { cloneDeep } from 'lodash-es'
import { ref, defineProps } from 'vue'
import { config } from '@/config'
import apis from '@/apis'
import { useForm, useModal } from '@/hooks'
import { useDicsStore } from '@/store'
import { message } from 'ant-design-vue'
import { UploadOutlined } from '@ant-design/icons-vue';
const emit = defineEmits(['ok'])
const fileList=ref([])
const { modal, showModal, hideModal, showLoading, hideLoading } = useModal()
const { formRecord, formData, formRef, formRules, resetForm } = useForm()
const cancelText = ref('取消')
const dicsStore = useDicsStore()
formRules.value = {
    categoryType: [{ required: true, message: '请选择类型', trigger: ['blur', 'change'] }],
    categoryId: [{ required: true, message: '请选择类型', trigger: ['blur', 'change'] }],
    name: [{ required: true, message: '请输入项目分类名称', trigger: 'blur' }],
    duration:[{ required: true, message: '请输入项目分类名称', trigger: 'blur' }],
}
const props = defineProps({
    categoryList: {
        type: Array,
        default: []
    }
})
/**
 * 新建
 */
function handleCreate() {
    showModal({
        type: 'create',
        title: '新增',
    })
    formData.value = {
        priceUnit: '元/次',
        frequencyUnit: '元/次',
        timeKeepingUnit: '小时/次'
    }
}
/**
 * 编辑
 */
function handleEdit(record = {}) {
    showModal({
        type: 'edit',
        title: '编辑',
    })
    formRecord.value = record
    formData.value = cloneDeep(record)
}


/**
 * 确定
 */
function handleOk() {
    formRef.value
        .validateFields()
        .then(async (values) => {
            try {
                showLoading()
                const params = {
                    ...formData.value,
                }
                let result = null
                switch (modal.value.type) {
                    case 'create':
                        result = await apis.serviceProject.createItem(params).catch(() => {
                            throw new Error()
                        })
                        console.log(result)
                        break
                    case 'edit':
                        result = await apis.serviceProject.updateItem(params).catch(() => {
                            throw new Error()
                        })
                        break
                }
                hideLoading()
                if (config('http.code.success') === result?.success) {
                    hideModal()
                    emit('ok')
                }
            } catch (error) {
                hideLoading()
            }
        })
        .catch(() => {
            hideLoading()
        })
}

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

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

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

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