<template>
    <el-dialog title="查重" v-model="visible" width="1250px" class="rule-dialog">
        <div class="search_area">
            <div class="flex">
                <el-select v-model="checkType" style="width: 270px;" @change="checkValue=''">
                  <el-option v-for="item in ruleFieldData" :label="item.fieldName" :value="item.fieldCode"></el-option>
                </el-select>
                <el-input v-model="checkValue" placeholder="请输入查询内容" style="margin-right: 20px"/>
                <el-button type="primary" icon="Search" @click="search">搜索</el-button>
                <el-button icon="Refresh" @click="reset">重置</el-button>
            </div>
            <div>为促进团队高效协作避免销售冲突，系统支持实时智能查重功能，通过全面对比系统信息库，精准确认资源的归属权</div>
        </div>
        <el-table
            :data="ruleData"
            height="100%"
            v-loading="buttonLoading"
        >
            <el-table-column align="center" label="名称" prop="customerName">
                <template #default="scope">
                    <el-tag :type="scope.row.resourceType=='10'?'':scope.row.resourceType=='11'?'success':'warning'">{{ scope.row.resourceType=='10'?'客户':scope.row.resourceType=='11'?'线索':'联系人' }}</el-tag>
                    <span style="margin-left: 4px;">{{ scope.row.customerName }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column align="center" label="联系人姓名" prop="name"></el-table-column> -->
            <!-- <el-table-column align="center" label="邮箱" prop="email"></el-table-column> -->
            <el-table-column align="center" label="归属人" prop="ownerByName">
                <template #default="scope">
                    <span>{{ scope.row.ownerByName?scope.row.ownerByName:'公海/无效' }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column align="center" label="最后跟进" prop="lastFollowUpTime"></el-table-column> -->
            <el-table-column align="center" label="未跟进天数" prop="noFollowUpDate"></el-table-column>
            <el-table-column align="center" label="创建时间" prop="createTime"></el-table-column>
            <el-table-column align="center" label="重复字段" prop="duplicateFieldNow">
                <template #default="scope">
                    <span style="color: #f56c6c;">{{ scope.row.duplicateFieldNow }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="重复值" prop="duplicateValue">
                <template #default="scope">
                    <span style="color: #f56c6c;">{{ scope.row.duplicateValue }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="查重字段" prop="errorMsg">
                <template #default="scope">
                    <span style="color: #f56c6c;">{{ scope.row.errorMsg }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="查重值" prop="duplicateValueNow">
                <template #default="scope">
                    <span style="color: #f56c6c;">{{ scope.row.duplicateValueNow }}</span>
                </template>
            </el-table-column>
        </el-table>
        <p v-if="isCreate!=0" class="p" :style='isCreate<3?"color:#f56c6c":"color:#1874FF"'>
            <span v-if="isCreate==1">您查重的信息与以上资料触发查重，若前往创建提交后，以上查重结果归属人将同步收到查重消息提醒</span>
            <span v-if="isCreate==2">您查重的信息与以上资料触发查重，暂无法创建，可与以上查重结果归属人进一步沟通归属问题</span>
            <span v-if="isCreate==3">恭喜您，无相关{{type=='10'?'客户':'线索'}}，可前往创建</span>
        </p>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="visible = false">关闭</el-button>
                <el-button v-if="isCreate!=2" type="primary" @click="addCustomer">前往创建</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
    import { singleCheckCustomer } from '@/api/customer';
    import { getRuleField } from '@/api/system/ruleConfig';
    const props = defineProps({
        resourceType: {
            type: [Number,String],
            default: '10'
        }
    })
    const buttonLoading = ref(false)
    const checkType = ref('customerCompany.website')
    const checkValue = ref()
    const visible = ref(false)

    const type = ref()
    const open = ({ resourceType }: { resourceType: string|number }) => {
        reset();
        type.value = resourceType
        visible.value = true
        getEield()
    }

    const ruleFieldData = ref([])
    const getEield = ()=>{
        getRuleField(type.value).then(res => {
            if(res.success) {
                ruleFieldData.value = res?.result
            }
        })
    }

    const isCreate = ref(0)
    const ruleData = ref([])
    const search = ()=>{
        let params = {
            customerForm:type.value,
            fieldCode:checkType.value,
            blurry:checkValue.value,
        }
        buttonLoading.value = true;
        singleCheckCustomer(params).then(async res => {
            buttonLoading.value = false;
            if(res.success) {
                if(res.result) {  // 有重复可以创建
                    isCreate.value = 1
                    ruleData.value = (res.result || []).map(item => {
                        let obj = { ...item.object,
                            errorMsg: item.errorMsg,
                            duplicateValue: item.duplicateValue,
                            resourceType: item.resourceType,
                            name: item.name,
                            customerName: item.customerName,
                            createTime: item.createTime,
                            lastFollowUpTime: item.lastFollowUpTime,
                            noFollowUpDate: item.noFollowUpDate,
                            duplicateFieldNow: item.duplicateFieldNow,
                            duplicateValueNow: item.duplicateValueNow,
                            ownerByName: item.ownerByName,
                        }
                        return obj
                    })
                }else{  // 没有重复
                    isCreate.value = 3
                    ruleData.value = []
                }
            }else{ //有重复且不能创建
                isCreate.value = 2
                ruleData.value = (res.result || []).map(item => {
                    let obj = { ...item.object,
                        errorMsg: item.errorMsg,
                        duplicateValue: item.duplicateValue,
                        resourceType: item.resourceType,
                        name: item.name,
                        customerName: item.customerName,
                        createTime: item.createTime,
                        lastFollowUpTime: item.lastFollowUpTime,
                        noFollowUpDate: item.noFollowUpDate,
                        duplicateFieldNow: item.duplicateFieldNow,
                        duplicateValueNow: item.duplicateValueNow,
                        ownerByName: item.ownerByName,
                    }
                    return obj
                })
            }

        })
    }
    const reset = ()=>{
        isCreate.value = 0
        ruleData.value = []
        checkType.value= 'customerCompany.website'
        checkValue.value= ''
    }
    const router = useRouter();
    const addCustomer = ()=>{
        let objName = checkType.value.split('.')
        if(type.value =='10'){
            router.push('../customer/addCustomer?type=add&checkFrist='+objName[0]+'&checkSecond='+objName[1]+'&checkValue='+checkValue.value)
        }else{
            router.push('../clue/add?type=add&checkFrist='+objName[0]+'&checkSecond='+objName[1]+'&checkValue='+checkValue.value)
        }

    }

    defineExpose({
        open
    })
</script>
<style lang="scss" scoped>
    .search_area{
        padding: 0 60px 26px;
        .flex{
            margin-bottom: 16px;
        }
    }
    .p{
        margin-bottom: 10px;
        font-size: 16px;
        font-weight: bold;
        padding-top:20px;
    }

</style>
