<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="项目id" prop="project_id">
                            <el-input v-model="formData.project_id" clearable placeholder="请输入项目id" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="客户id" prop="custom_id">
                            <el-input v-model="formData.custom_id" clearable placeholder="请输入客户id" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <el-form-item label="审批id" prop="approve_id">
                            <el-input v-model="formData.approve_id" clearable placeholder="请输入审批id" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <el-form-item label="联系人" prop="contacts">
                            <el-input v-model="formData.contacts" clearable placeholder="请输入联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <el-form-item label="电话" prop="phone">
                            <el-input v-model="formData.phone" clearable placeholder="请输入电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="日期" prop="date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable type="datetime"
                                value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="分类" prop="classification">
                            <el-select class="flex-1" v-model="formData.classification" clearable placeholder="请选择分类">
                                <el-option v-for="(item, index) in dictData.classification" :key="index" :label="item.name"
                                    :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="紧急程度" prop="urgency">
                            <el-radio-group v-model="formData.urgency" placeholder="请选择紧急程度">
                                <el-radio v-for="(item, index) in dictData.urgency" :key="index"
                                    :label="parseInt(item.value)">
                                    {{ item.name }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="接待人" prop="receiver">
                            <el-input v-model="formData.receiver" clearable placeholder="请输入接待人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="指定处理人" prop="processed_admin_id">
                            <el-input v-model="formData.processed_admin_id" clearable placeholder="请输入指定处理人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="投诉主题" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入投诉主题" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="问题描述" prop="description">
                            <el-input v-model="formData.description" clearable placeholder="请输入问题描述" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="备注" prop="notes">
                            <el-input v-model="formData.notes" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <!-- <material-picker v-model="formData.annex" /> -->
                        <el-form-item label="附件" prop="field127">
                            <el-upload accept=".pdf" class="upload-demo" :show-file-list="false" aria-hidden="true"
                                :headers="{ Token: userStore.token }" :action="base_url + '/upload/file'"
                                :on-success="handleAvatarSuccess_four" ref="upload">
                                <el-button type="primary">
                                    上传
                                </el-button>
                            </el-upload>

                            <div>
                                <div v-for="(item, index) in formData.annex" style="margin-left: 5px;display: block;">
                                    <a style="margin-left: 10px; color: #4a5dff; align-self: flex-start" :href="item.uri"
                                        target="_blank">{{ item.name }}</a>
                                    <span style="cursor: pointer;margin-left: 5px;" @click="delFileFn(index)">x</span>
                                </div>
                            </div>



                        </el-form-item>


                    </el-col>
                </el-row>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="customServiceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiCustomServiceAdd, apiCustomServiceEdit, apiCustomServiceDetail } from '@/api/custom_service'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import useUserStore from "@/stores/modules/user";
import configs from "@/config"
const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
// 上传文件
const handleAvatarSuccess_four = (
    response,
    uploadFile
) => {
    if (response.code == 0) {
        ElMessage.error(response.msg);
        return;
    }
    formData.annex.push(
        { uri: response.data.uri, name: response.data.name }

    );
};
// 删除上传的文件
const delFileFn = (index: number) => {
    formData.annex.splice(index, 1)
}

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({
    id: '',
    project_id: '',
    custom_id: '',
    approve_id: '',
    contacts: '',
    phone: '',
    date: '',
    classification: '',
    urgency: '',
    receiver: '',
    processed_admin_id: '',
    name: '',
    description: '',
    notes: '',
    annex: [],
    status: '',
})


// 表单验证
const formRules = reactive<any>({

})


// 获取详情
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]
        }
    }

    //@ts-ignore
    formData.date = timeFormat(formData.date, 'yyyy-mm-dd hh:MM:ss')
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiCustomServiceDetail({
        id: row.id
    })
    setFormData(data)
}


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