<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="auto">
                <el-row :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="项目名称" prop="project_id" @click="showDialog = true">
                            <el-input v-model="formData.project_name" readonly clearable placeholder="请选择项目" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="项目编码" prop="project_id">
                            <el-input v-model="formData.project_code" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="客户名称" prop="custom_name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.custom_name" readonly clearable placeholder="系统自动填写" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="合同号" prop="contract_id">
                            <el-input v-model="formData.contract_code" clearable readonly placeholder="点击选择合同"
                                @click="showDialog2 = true" class="!flex flex-1" />
                            <toolTip content="可在合同管理-项目合同-项目合同添加" url="/construction/contract/ProjectContracts/contract">
                            </toolTip>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="联系人" prop="custom_master_name">
                            <el-input v-model="formData.custom_master_name" disabled clearable placeholder="请输入联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">

                        <el-form-item label="联系电话" prop="custom_master_phone">
                            <el-input v-model="formData.custom_master_phone" disabled clearable placeholder="请输入联系电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="日期" prop="date"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="分类" prop="classification"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <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"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <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"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" @click="userclick('receiver')">
                            <el-input v-model="formData.receiver_name" clearable placeholder="请输入接待人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="指定处理人" prop="processed_user" @click="userclick('processed_user')"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.processed_user_name" clearable readonly placeholder="请输入指定处理人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="投诉主题" prop="name"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <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">
                        <el-form-item label="附件">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </popup>

        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <peojectDialog @customEvent="customEvent"></peojectDialog>
        </el-dialog>
        <div v-if="showDialog1">
            <el-dialog v-model="showDialog1" title="选择客户" width="70%">
                <customDialog @customEvent="customEvent1"></customDialog>
            </el-dialog>
        </div>
        <div v-if="showDialog2">
            <el-dialog v-model="showDialog2" title="选择项目合同" width="70%">
                <salescontractDialog @customEvent="customEvent2" :project_id="formData.project_id">
                </salescontractDialog>
            </el-dialog>
        </div>
        <personnelselector ref="personnel" @confirm="submituser" type="1"></personnelselector>

    </div>
</template>

<script lang="ts" setup name="customServiceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import customDialog from '@/components/custom-dialog/index.vue'
import salescontractDialog from '@/components/salescontract/index.vue'
import { apiCustomServiceAdd, apiCustomServiceDetail, apiCustomServiceEdit } from '@/api/custom_service'
import personnelselector from '@/components/personnelselector/index.vue'
import type { PropType } from 'vue'
import peojectDialog from '@/components/project/index.vue'

const showDialog = ref(false)
const showDialog1 = ref(false)
const showDialog2 = ref(false)

const customEvent = (e: any) => {
    formData.project_id = e.id
    formData.project_name = e.name
    formData.project_code = e.project_code
    formData.custom_name = e.custom_name
    formData.custom_master_phone = e.custom_master_phone
    formData.custom_master_name = e.custom_master_name
    showDialog.value = false;
};

const customEvent1 = (e: any) => {
    formData.custom_id = e.id
    formData.custom_name = e.name
    showDialog1.value = false;
};

const customEvent2 = (e: any) => {
    formData.contract_id = e.id
    formData.contract_code = e.contract_code
    showDialog2.value = false;
};

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 personnel = ref<any>()
let val = ''
const userclick = (vals) => {
    val = vals
    personnel.value.open()

}
//确认
const submituser = (e: any) => {
    formData[val] = e.id;
    formData[val + '_name'] = e.name
}


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑客户售后' : '新增客户售后'
})


// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    project_code: "",
    contacts: '',
    custom_master_name: "",
    custom_master_phone: "",
    phone: '',
    date: '',
    classification: '',
    urgency: '',
    receiver: '',
    processed_user: '',
    processed_user_name: '',
    name: '',
    description: '',
    notes: '',
    annex: [],
    status: '',
    contract_code: "",
    contract_id: "",
    custom_name: "",
    custom_id: "",
    receiver_name: ""
})


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

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, }
    data.date = data.date.substring(0, 10)
    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>
