<template>
    <div class="edit-popup">
        <popup ref="popupRef" title="售后工单处理" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">

            <el-descriptions :column="3" title="基本信息" border>
                <el-descriptions-item label="项目名称" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.project_name }}
                </el-descriptions-item>
                <el-descriptions-item label="项目编码" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.project_code }}
                </el-descriptions-item>
                <el-descriptions-item label="客户名称" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.custom_name }}
                </el-descriptions-item>
                <el-descriptions-item label="合同号" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.contract_code }}
                </el-descriptions-item>
                <el-descriptions-item label="联系人" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.custom_master_phone }}
                </el-descriptions-item>
                <el-descriptions-item label="联系电话" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.custom_master_phone }}
                </el-descriptions-item>
                <el-descriptions-item label="日期" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.date }}
                </el-descriptions-item>
                <el-descriptions-item label="分类" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.classification_text }}
                </el-descriptions-item>
                <el-descriptions-item label="紧急程度" label-align="left" align="left" label-class-name="my-label">
                    <el-radio-group v-model="formData.urgency" readonly placeholder="请选择紧急程度">
                        <el-radio v-for="(item, index) in dictData.urgency" :key="index" :label="parseInt(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-descriptions-item>
                <el-descriptions-item label="接待人" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.receiver }}
                </el-descriptions-item>

                <el-descriptions-item label="指定处理人" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.processed_user_name }}
                </el-descriptions-item>
                <el-descriptions-item label="问题描述" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.description }}
                </el-descriptions-item>
                <el-descriptions-item label="处理过程" label-align="left" v-if="formData.processing_process" align="left"
                    label-class-name="my-label">
                    {{ formData.processing_process }}
                </el-descriptions-item>
                <el-descriptions-item label="完成日期" v-if="formData.done_date" label-align="left" align="left"
                    label-class-name="my-label">
                    {{ formData.done_date }}
                </el-descriptions-item>
                <el-descriptions-item label="花费工时" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.processing_hours }}
                </el-descriptions-item>
                <el-descriptions-item label="备注" label-align="left" align="left" label-class-name="my-label">
                    {{ formData.notes }}
                </el-descriptions-item>
                <el-descriptions-item label="附件" label-align="left" align="left" label-class-name="my-label">
                    <div v-if="formData.annex && formData.annex.length > 0">
                        <div v-for="(item, index) in formData.annex" style="margin-left: 5px; display: block">
                            <el-link style="
                      margin-left: 10px;
                      color: #4a5dff;
                      align-self: flex-start;
                    " :href="item" target="_blank">文件{{ index + 1 }}查看</el-link>
                        </div>
                    </div>
                </el-descriptions-item>
            </el-descriptions>


            <el-card class="mt-1">
                <el-form ref="formRef1" :model="formData1" label-width="auto" :rules="formRules">
                    <el-row :gutter="10">
                        <el-col :span="8" v-if="!formData.processing_result_text">
                            <el-form-item label="处理结果" prop="processing_result">
                                <el-radio-group v-model="formData1.processing_result" placeholder="请选择处理结果">
                                    <el-radio v-for="(item, index) in dictData.custom_service_solv_result" :key="index"
                                        :label="parseInt(item.value)">
                                        {{ item.name }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="处理结果" prop="processing_result_text">
                                <el-select v-model="formData1.processing_result" clearable placeholder="请选择处理结果"
                                    class="flex-1">
                                    <el-option v-for="(item, index) in dictData.custom_service_solve_result"
                                        :key="index" :label="item.name" :value="item.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="处理过程" prop="processing_process">
                                <el-input v-model="formData1.processing_process" clearable placeholder="请输入处理过程" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="花费工时" prop="processing_hours">
                                <el-input v-model="formData1.processing_hours" type="number" clearable
                                    placeholder="请输入花费工时" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="完成日期" prop="done_date">
                                <el-date-picker class="flex-1 !flex" v-model="formData1.done_date" clearable type="date"
                                    value-format="YYYY-MM-DD" placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="整体评价" prop="score">
                                <el-rate v-model="formData1.score" size="large" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="是否解决" prop="is_solve">
                                <el-radio-group v-model="formData1.is_solve" placeholder="问题是否解决">
                                    <el-radio v-for="(item, index) in dictData.is_solve" :key="index"
                                        :label="parseInt(item.value)">
                                        {{ item.name }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="反馈意见" prop="feedback">
                                <el-input v-model="formData1.feedback" type="textarea" clearable
                                    placeholder="请输入反馈意见" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-card>
        </popup>
    </div>
</template>

<script lang="ts" setup name="customServiceEdit">
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { apiCustomServiceSolve } from "@/api/custom_service";
import type { PropType } from "vue";
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({}),
    },
});
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const formRef1 = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
// 表单数据
const formData = reactive({});

const formData1 = reactive({
    id: "",
    processing_result: "",
    processing_process: "",
    processing_hours: "",
    done_date: "",
    score: 0,
    is_solve: "",
    feedback: "",
});

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

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData1) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData1[key] = data[key];
        }
    }
    Object.assign(formData, data);

    formData1.processing_result = String(formData1.processing_result)
};

// 提交按钮
const handleSubmit = async () => {
    await formRef1.value?.validate();
    const data = { ...formData1 };

    await apiCustomServiceSolve(data);
    popupRef.value?.close();
    emit("success");
};

//打开弹窗
const open = () => {
    popupRef.value?.open();
};

// 关闭回调
const handleClose = () => {
    emit("close");
};

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