<template>
    <h5>核查办理</h5>
    <el-form :label-width="146" :model="form" ref="formRef">
        <el-row>
            <el-col :span="12">
                <el-form-item label="涉及案件/警情编号" prop="caseNumber">
                    <el-input
                        v-model="form.caseNumber"
                        placeholder="请输入"
                        style="width: 280px"
                    />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item
                    label="涉及单位"
                    prop="involveDepartId"
                    :rules="{
                        required: true,
                        message: '请选择涉及单位',
                        trigger: ['blur'],
                    }"
                >
                    <div style="width: 280px">
                        <depart-tree-select
                            v-model="form.involveDepartId"
                            :auth="false"
                        />
                    </div>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item
                    label="核查结论"
                    prop="checkStatusCode"
                    :rules="{
                        required: true,
                        message: '请选择核查结论',
                        trigger: ['blur'],
                    }"
                >
                    
                    <el-select v-model="form.checkStatusCode" @change="(val) => handleChangeCheckStatus(val, item)" clearable style="width: 280px">
                        <el-option v-for="item in dict.checkStatus" :value="item.dictValue" :label="item.dictLabel"></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col
                :span="12"
            >
                <el-form-item
                    label="涉及对象"
                    prop="accountabilityTarget"
                    :rules="{
                        required: true,
                        message: '请选择追责对象',
                        trigger: ['blur'],
                    }"
                >
                    <el-radio-group v-model="form.accountabilityTarget">
                        <el-radio
                            v-for="item in dict.accountabilityTarget"
                            :key="item.dictCode"
                            :value="item.dictValue"
                            >{{ item.dictLabel }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
            <el-col :span="12" v-if="problemIsTrue">
                <el-form-item
                    label="是否整改"
                    prop="isRectifyCode"
                    :rules="{
                        required: true,
                        message: '请选择是否整改',
                        trigger: ['blur'],
                    }"
                    
                >
                    <el-radio-group
                        v-model="form.isRectifyCode"
                        @change="
                            (val) =>
                                (form.isRectifyName = dict.isRectify.filter(
                                    (item) => item.dictValue === val
                                )[0].dictLabel)
                        "
                    >
                        <el-radio
                            v-for="item in dict.isRectify"
                            :key="item.dictCode"
                            :value="item.dictValue"
                            >{{ item.dictLabel }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
            <el-col
                :span="12"
                v-if="
                    problemIsTrue &&
                    form.isRectifyCode === IsRectify.NOT &&
                    problemIsTrue
                "
            >
                <el-form-item
                    label="整改限制"
                    prop="rectifyRestrictionDays"
                    :rules="{
                        required: true,
                        message: '请选择整改限制',
                        trigger: ['blur'],
                    }"
                >
                    <el-input-number v-model="form.rectifyRestrictionDays" />
                    <span class="ml-8">天</span>
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item
            label="问题核查情况"
            prop="checkStatusDesc"
            :rules="{
                required: true,
                message: '请输入问题核查情况',
                trigger: ['blur'],
            }"
        >
            <el-input
                type="textarea"
                placeholder="请输入问题核查情况"
                v-model="form.checkStatusDesc"
                :autosize="{ minRows: 4 }"
            />
        </el-form-item>
        <el-form-item
            label="未整改原因"
            prop="unrectifyReason"
            :rules="{
                required: true,
                message: '请输入未整改原因',
                trigger: ['blur'],
            }"
            v-if="problemIsTrue &&
                (form.isRectifyCode === '0' || form.isRectifyCode === '2')
            "
        >
            <el-input
                type="textarea"
                placeholder="请输入未整改原因"
                v-model="form.unrectifyReason"
                :autosize="{ minRows: 4 }"
            />
        </el-form-item>
        <el-form-item
            label="问题整改情况"
            prop="rectifyDesc"
            v-if="problemIsTrue && form.isRectifyCode === InspectCase.TRUE"
            :rules="{
                required: true,
                message: '请输入问题整改情况',
                trigger: ['blur'],
            }"
        >
            <el-input
                type="textarea"
                placeholder="请输入问题整改情况"
                v-model="form.rectifyDesc"
                :autosize="{ minRows: 4 }"
            />
        </el-form-item>
        <template
            v-if="
                negative.problemSourcesCode === ProblemSources.GJXFPT ||
                negative.problemSourcesCode === ProblemSources.GABXF ||
                negative.problemSourcesCode === ProblemSources.JZXX ||
                negative.problemSourcesCode === ProblemSources.XF12337
            "
        >
            <h5>信访办理</h5>
            <template v-if="negative.problemSourcesCode === ProblemSources.XF12337">
                <el-form-item
                    prop="verifiedIsLeader"
                    :rules="{
                        required: true,
                        message: '请选择涉及人员是否属于领导班子成员',
                        trigger: ['blur'],
                    }"
                >
                    <template #label>
                        <div style="line-height: 1">涉及人员是否属于领导班子成员</div>
                    </template>
                    <el-select
                        v-model="form.verifiedIsLeader"
                        clearable
                        style="width: 280px"
                    >
                        <el-option value="非领导班子成员">非领导班子成员</el-option>
                        <el-option value="县级单位班子成员">县级单位班子成员</el-option>
                        <el-option value="市级单位班子成员">市级单位班子成员</el-option>
                        <el-option value="省级单位班子成员">省级单位班子成员</el-option>
                        <el-option value="中央单位班子成员">中央单位班子成员</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    prop="processResult"
                    label="办理结果"
                    :rules="{
                        required: true,
                        message: '请选择办理结果',
                        trigger: ['blur'],
                    }"
                >
                    <el-select
                        v-model="form.processResult"
                        clearable
                        style="width: 280px"
                    >
                        <el-option value="具有违纪违法情况">具有违纪违法情况</el-option>
                        <el-option value="无违纪违法情况">无违纪违法情况</el-option>
                        <el-option value="其他">其他</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    label="处理结论结果"
                    prop="handleResult12337"
                    :rules="{
                        required: true,
                        message: '请选择12337处理结论结果',
                        trigger: ['blur'],
                    }"
                    v-if="form.processResult === '具有违纪违法情况'"
                >
                    <handle-result12337-select
                        v-model="form.handleResult12337"
                        style="width: 280px"
                        @select="handleResult12337"
                    />
                    <span class="ml-20">{{ form.handleResult12337Group }}</span>
                </el-form-item>
                <el-form-item label="处分处理情况" prop="disciplinaryActionDesc" :rules="{
                    required: true,
                    message: '请输入处分处理情况',
                    trigger: ['blur']
                }" v-if="form.processResult === '具有违纪违法情况' && form.handleResult12337 !== '免予处分' && form.handleResult12337 !== '不予处分'">
                    <el-input
                            type="textarea"
                            placeholder="请输入处分处理情况"
                            v-model="form.disciplinaryActionDesc"
                            :autosize="{ minRows: 4 }"
                        />
                </el-form-item>
            </template>

            <el-form-item
                prop="handlePolices"
                label="经办人"
                :rules="{
                    required: true,
                    validator: validateHandlePolices,
                }"
            >
                <div
                    class="mb-8 flex gap v-center"
                    v-for="(item, index) in form.handlePolices"
                    :key="index"
                    style="width: 100%"
                >
                    <police-select
                        :depart-id="negative.handleSecondDepartId"
                        v-model="item.empNo"
                        @change="
                            (police) => {
                                item.name = police.name;
                                item.idCode = police.idCode;
                                item.mobile = police.mobile;
                            }
                        "
                    />
                    <span class="ml-20 text-right" style="width: 80px">联系电话：</span>
                    <el-input
                        placeholder="联系方式"
                        v-model="item.mobile"
                        style="width: 240px"
                    />
                    <el-button
                        plain
                        type="primary"
                        v-if="index === 0"
                        @click="form.handlePolices.push({})"
                    >
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        添加经办人
                    </el-button>
                    <el-button
                        plain
                        type="danger"
                        v-else
                        @click="form.handlePolices.splice(index, 1)"
                    >
                        <template #icon>
                            <icon name="el-icon-Delete" />
                        </template>
                        删除经办人
                    </el-button>
                </div>
            </el-form-item>
            <el-form-item>
                <p class="tips" style="font-size: 13px; color: #666">经办人不能是涉及人员</p>
            </el-form-item>
            <el-form-item
                prop="visitingLeaderName"
                label="接访领导"
                :rules="{
                    required: true,
                    message: '请选择接访领导',
                    trigger: ['blur'],
                }"
                v-if="
                    negative.problemSourcesCode === ProblemSources.GJXFPT ||
                    negative.problemSourcesCode === ProblemSources.GABXF
                "
            >
                <div class="flex gap" style="width: 100%">
                    <police-select
                        :host-level="negative.hostLevel"
                        :depart-id="
                            negative.hostLevel === HostLevel.SECOND
                                ? negative.handleSecondDepartId
                                : negative.handleThreeDepartId
                        "
                        v-model="form.visitingLeaderEmpNo"
                        @change="(police) => (form.visitingLeaderName = police.name)"
                        :isLeader="true"
                    />
                    <span class="ml-20 text-right" style="width: 80px">警号：</span>
                    <el-input
                        v-model="form.visitingLeaderEmpNo"
                        placeholder="接访领导警号"
                        style="width: 240px"
                    />
                </div>
            </el-form-item>
            <el-form-item
                label="化解情况"
                prop="resolveSituation"
                v-if="
                    negative.problemSourcesCode === ProblemSources.GJXFPT ||
                    negative.problemSourcesCode === ProblemSources.GABXF
                "
                :rules="{
                    required: true,
                    message: '请选择信访化解情况',
                    trigger: ['blur'],
                }"
            >
                <el-select
                    v-model="form.resolveSituation"
                    clearable
                    style="width: 240px"
                    placeholder="请选择信访化解情况"
                >
                    <el-option
                        v-for="item in dict.resolveSituation"
                        :key="item.id"
                        :label="item.dictLabel"
                        :value="item.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-form-item
                prop="resolveStatus"
                label="当前状态"
                :rules="{
                    required: true,
                    message: '请选择信访当前状态',
                    trigger: ['blur'],
                }"
                v-if="
                    negative.problemSourcesCode === ProblemSources.GJXFPT ||
                    negative.problemSourcesCode === ProblemSources.GABXF
                "
            >
                <el-select
                    v-model="form.resolveStatus"
                    clearable
                    style="width: 240px"
                    placeholder="请选择信访化当前状态"
                >
                    <el-option
                        v-for="item in dict.resolveStatus"
                        :key="item.id"
                        :label="item.dictLabel"
                        :value="item.dictValue"
                    />
                </el-select>
            </el-form-item>
            <el-divider />
        </template>

        
        <div v-if="form.accountabilityTarget !== AccountabilityTarget.DEPARTMENT">
            
            <el-form-item
                label-position="top"
                prop="blames"
                :rules="{
                    required: true,
                    message: '请添加涉及人员',
                    trigger: ['change'],
                }"
            >
                <div
                    v-for="(item, index) in form.blames.filter(
                        (obj) => obj.type === BlameType.PERSONAL
                    )"
                    :key="index"
                >
                    <div class="flex v-center">
                        <h5>
                            涉及人员{{
                                form.blames
                                    .filter(
                                        (blame) => blame.type === BlameType.PERSONAL
                                    )
                                    .indexOf(item) + 1
                            }}
                        </h5>
                        <el-button
                            type="danger"
                            plain
                            size="small"
                            @click="handleRemoveBlame(item)"
                            text
                            class="ml-10"
                            >删除
                        </el-button>
                    </div>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="涉及人员姓名"
                                :prop="`blames.${index}.blameEmpNo`"
                                :rules="{
                                    required: true,
                                    message: '请选择涉及人员',
                                    trigger: ['blur'],
                                }"
                            >
                                <police-select
                                    :host-level="negative.hostLevel"
                                    :depart-id="negative.hostLevel === HostLevel.SECOND
                                            ? negative.secondInvolveDepartId
                                            : negative.threeInvolveDepartId"
                                    v-model="item.blameEmpNo"
                                    :disabldKeys="
                                        form.blames
                                            .filter((obj) => obj.blameEmpNo)
                                            .map((obj) => obj.blameEmpNo)
                                    "
                                    @change="
                                        (police) => handleChangePolice(police, item)
                                    "
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="警号"
                                :prop="`blames.${index}.blameEmpNo`"
                                :rules="{
                                    required: true,
                                    message: '请输入警号',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-input
                                    placeholder="请输入警号"
                                    v-model="item.blameEmpNo"
                                    style="width: 240px"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="身份证号码"
                                :prop="`blames.${index}.blameIdCode`"
                                :rules="{
                                    required: true,
                                    message: '请输入身份证号码',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-input
                                    placeholder="请输入身份证号码"
                                    v-model="item.blameIdCode"
                                    style="width: 240px"
                                />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item
                                label="人员属性"
                                :prop="`blames.${index}.ivPersonTypeCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择人员属性',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-radio-group
                                    v-model="item.ivPersonTypeCode"
                                    @change="
                                        (val) =>
                                            (item.ivPersonType = dict.personType.filter(
                                                (item) => item.dictValue === val
                                            )[0].dictLabel)
                                    "
                                >
                                    <el-radio
                                        v-for="item in dict.personType"
                                        :key="item.dictCode"
                                        :value="item.dictValue"
                                        >{{ item.dictLabel }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item
                        :prop="`blames.${index}.problems`"
                        :rules="{
                            required: true,
                            message: '请添加问题类型',
                            trigger: ['blur'],
                        }"
                        v-if="problemIsTrue"
                        style="margin-bottom: 0"
                    >
                        <div
                            v-for="(problem, j) in item.problems"
                            :key="j"
                            style="width: 98%"
                        >
                            <el-form-item
                                :label="`问题类型${j + 1}`"
                                :prop="`blames.${index}.problems.${j}.threeLevelCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择问题类型',
                                    trigger: ['blur'],
                                }"
                            >
                                <div class="flex between" style="width: 100%">
                                    <div class="flex gap">
                                        <problem-type-select
                                            v-model="problem.threeLevelCode"
                                            @change="
                                                (node) =>
                                                    handleChangeProblem(node, problem)
                                            "
                                        />
                                        <div style="width: 140px">
                                            <el-form-item
                                                label-position="top"
                                                :prop="`blames.${index}.problems.${j}.threeLevelContentOther`"
                                                :rules="{
                                                    required: true,
                                                    message: '请输入',
                                                    trigger: ['blur'],
                                                }"
                                                style="margin-bottom: 0"
                                                v-if="
                                                    problem.threeLevelContent ===
                                                    '其他'
                                                "
                                            >
                                                <el-input
                                                    placeholder="其他类型详细描述"
                                                    v-model="
                                                        problem.threeLevelContentOther
                                                    "
                                                />
                                            </el-form-item>
                                        </div>
                                        <div>
                                            <span>{{
                                                problem.oneLevelContent
                                                    ? problem.oneLevelContent + " / "
                                                    : ""
                                            }}</span>
                                            <span>{{
                                                problem.twoLevelContent
                                                    ? problem.twoLevelContent + " / "
                                                    : ""
                                            }}</span>
                                            <span>{{
                                                problem.threeLevelContent
                                            }}</span>
                                        </div>
                                    </div>

                                    <div class="flex center v-center">
                                        <el-button
                                            type="danger"
                                            plain
                                            @click="item.problems.splice(j, 1)"
                                            v-if="j !== 0"
                                            size="small"
                                        >
                                            <template #icon>
                                                <icon name="el-icon-Delete" />
                                            </template>
                                            删除问题
                                        </el-button>
                                        <el-button
                                            @click="handleAddProblem(item)"
                                            plain
                                            type="primary"
                                            v-if="j === 0"
                                            size="small"
                                        >
                                            <template #icon>
                                                <icon name="el-icon-Plus" />
                                            </template>
                                            添加问题
                                        </el-button>
                                    </div>
                                </div>
                            </el-form-item>
                        </div>
                        <div
                            class="flex center"
                            style="width: 100%"
                            v-if="!item.problems || item.problems.length === 0"
                        >
                            <el-button
                                @click="handleAddProblem(item)"
                                plain
                                type="primary"
                                size="small"
                            >
                                <template #icon>
                                    <icon name="el-icon-Plus" />
                                </template>
                                添加问题
                            </el-button>
                        </div>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="责任类别"
                                :prop="`blames.${index}.responsibilityTypeCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择责任类别',
                                    trigger: ['blur'],
                                }"
                                v-if="problemIsTrue"
                            >
                                <el-radio-group
                                    v-model="item.responsibilityTypeCode"
                                    @change="
                                        (val) =>
                                            (item.responsibilityTypeName = dict.responsibilityType.filter(
                                                (item) => item.dictValue === val
                                            )[0].dictLabel)
                                    "
                                >
                                    <el-radio
                                        v-for="item in dict.responsibilityType"
                                        :key="item.dictCode"
                                        :value="item.dictValue"
                                        >{{ item.dictLabel }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12" v-if="problemIsTrue">
                            <el-form-item
                                label="主观方面"
                                :prop="`blames.${index}.subjectiveAspectCode`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请选择主观方面',
                                    trigger: ['blur'],
                                }"
                            >
                                <div class="flex gap v-center">
                                    <el-radio-group
                                        v-model="item.subjectiveAspectCode"
                                        @change="
                                            (val) =>
                                                handleChangeSubjectiveAspect(
                                                    val,
                                                    item
                                                )
                                        "
                                    >
                                        <el-radio
                                            v-for="item in dict.subjectiveAspect"
                                            :key="item.dictCode"
                                            :value="item.dictValue"
                                            >{{ item.dictLabel }}
                                        </el-radio>
                                    </el-radio-group>
                                    <el-form-item
                                        :prop="`blames.${index}.subjectiveAspectName`"
                                        :rules="{
                                            required: true,
                                            message: '其他主观方面不能为空',
                                            trigger: ['blur'],
                                        }"
                                        v-if="
                                            item.subjectiveAspectCode ===
                                            SubjectiveAspect.OTHER
                                        "
                                        style="margin-bottom: 0"
                                        class="ml-20"
                                    >
                                        <el-input
                                            v-model="item.subjectiveAspectName"
                                            placeholder="请输入其他主观方面"
                                            style="width: 160px"
                                        />
                                    </el-form-item>
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12" v-if="problemIsTrue">
                            <el-form-item
                                label="处理结果"
                                :prop="`blames.${index}.handleResultCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择处理结果',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-select
                                    v-model="item.handleResultCode"
                                    style="width: 240px"
                                    multiple
                                    @change="
                                        (val) => handleChangeResultCode(item, val)
                                    "
                                    clearable
                                >
                                    <el-option
                                        v-for="dictItem in dict.handleResult"
                                        :key="dictItem.dictCode"
                                        :value="dictItem.dictValue"
                                        :label="dictItem.dictLabel"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col
                            :span="12"
                            v-if="item.handleResultCode?.includes('11')"
                        >
                            <el-form-item
                                label="其他处理结果"
                                :prop="`blames.${index}.handleResultCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择处理结果',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-input
                                    v-model="item.handleResultNameOther"
                                    placeholder="其他类型详细描述"
                                />
                            </el-form-item>
                        </el-col>
                        <template v-if="problemIsTrue">
                            <el-col :span="12">
                                <el-form-item
                                    label="维权容错"
                                    :prop="`blames.${index}.protectRightsCode`"
                                    v-if="
                                        negative.problemSourcesCode !==
                                        ProblemSources.JWDC
                                    "
                                >
                                    <el-radio-group
                                        v-model="item.protectRightsCode"
                                        @change="
                                            (val) =>
                                                (item.protectRightsName = dict.protectRights.filter(
                                                    (item) => item.dictValue === val
                                                )[0].dictLabel)
                                        "
                                    >
                                        <el-radio
                                            v-for="item in dict.protectRights"
                                            :key="item.dictCode"
                                            :value="item.dictValue"
                                            >{{ item.dictLabel }}
                                        </el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item
                                    label="督察措施"
                                    :prop="`blames[${index}].superviseMeasuresCode`"
                                >
                                    <el-radio-group
                                        v-model="item.superviseMeasuresCode"
                                    >
                                        <el-radio
                                            v-for="dictItem in dict.superviseMeasures"
                                            :key="dictItem.dictCode"
                                            :value="dictItem.dictValue"
                                            @click.native.prevent="
                                                () => {
                                                    if (
                                                        item.superviseMeasuresCode ===
                                                        dictItem.dictValue
                                                    ) {
                                                        item.superviseMeasuresCode =
                                                            '';
                                                        item.superviseMeasuresName =
                                                            '';
                                                        item.confinementId = null;
                                                    } else {
                                                        item.superviseMeasuresCode =
                                                            dictItem.dictValue;
                                                        item.superviseMeasuresName =
                                                            dictItem.dictLabel;

                                                        if (dictItem.dictValue != 1) {
                                                            item.confinementId = null;
                                                        }
                                                    }
                                                }
                                            "
                                            >{{ dictItem.dictLabel }}
                                        </el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </template>
                    </el-row>
                    <el-row v-if="item.superviseMeasuresCode == 1">
                        <el-col>
                            <el-form-item label="禁闭关联">
                                <!--                {{item.superviseMeasuresCode}}-->
                                <el-select
                                    v-model="item.confinementId"
                                    filterable
                                    remote
                                    clearable
                                    :remote-method="fileConfinement"
                                    popper-class="widthClass"
                                >
                                    <template #header>
                                        <el-row>
                                            <el-col :span="2">姓名</el-col>
                                            <el-col :span="2">警号</el-col>
                                            <el-col :span="3">部门</el-col>
                                            <el-col :span="8">事由</el-col>
                                            <el-col :span="4">禁闭时间</el-col>
                                        </el-row>
                                    </template>
                                    <el-option
                                        v-for="(val, key) in confinementOpens"
                                        :key="key"
                                        :label="val.matter"
                                        :value="val.id"
                                    >
                                        <el-row>
                                            <el-col :span="2">{{ val.name }}</el-col>
                                            <el-col :span="2">{{ val.empNo }}</el-col>
                                            <el-col :span="3">
                                                <div class="flex gap-4">
                                                    <span>{{
                                                        val.departShortName || "/"
                                                    }}</span>
                                                </div>
                                            </el-col>
                                            <el-col :span="8">
                                                <el-tooltip
                                                    :content="val.matter"
                                                    placement="top"
                                                >
                                                    <el-text truncated>
                                                        {{ val.matter }}</el-text
                                                    >
                                                </el-tooltip>
                                            </el-col>
                                            <el-col :span="4">
                                                {{
                                                    val.startTime + "-" + val.endTime
                                                }}
                                            </el-col>
                                        </el-row>
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div class="flex center" style="width: 100%">
                    <el-button type="primary" plain @click="handleAddPersonalBlame">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        添加涉及人员
                    </el-button>
                </div>
            </el-form-item>
            <el-divider />
            <el-form-item
                label-position="top"
                prop="blameLeaders"
                :rules="{
                    required: !isUntrue,
                    message: '请添加涉及领导',
                }"
            >
                <div v-for="(item, index) in form.blameLeaders" :key="index">
                    <div class="flex v-center">
                        <h5>涉及领导{{ index + 1 }}</h5>
                        <el-button
                            type="danger"
                            plain
                            size="small"
                            @click="handleRemoveBlameLeader(index)"
                            text
                            class="ml-10"
                            >删除
                        </el-button>
                    </div>

                    <el-row style="background: #f9faff">
                        <el-col :span="24">
                            <el-form-item
                                label="关联涉及人员"
                                :prop="`blameLeaders.${index}.blameIdCodes`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请选择涉及领导关联的涉及人员',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-checkbox
                                    border
                                    v-for="(obj, j) in form.blames.filter(
                                        (obj) =>
                                            obj.type === BlameType.PERSONAL &&
                                            obj.blameIdCode
                                    )"
                                    :key="obj.blameIdCode"
                                    :label="obj.blameName"
                                    :value="obj.blameIdCode"
                                    :checked="
                                        item.blameIdCodes.includes(obj.blameIdCode)
                                    "
                                    @change="
                                        (bool) =>
                                            handleCheckboxChange(
                                                item,
                                                bool,
                                                obj.blameIdCode
                                            )
                                    "
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="涉及领导姓名"
                                :prop="`blameLeaders.${index}.leadName`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请选择涉及领导',
                                    trigger: ['blur'],
                                }"
                            >
                                <police-select
                                    :host-level="negative.hostLevel"
                                    :depart-id="negative.hostLevel === HostLevel.SECOND
                                            ? negative.secondInvolveDepartId
                                            : negative.threeInvolveDepartId"
                                    v-model="item.leadEmpNo"
                                    @change="
                                        (police) => {
                                            item.leadName = police.name;
                                            item.leadIdCode = police.idCode;
                                        }
                                    "
                                    :isLeader="true"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="警号"
                                :prop="`blameLeaders.${index}.leadEmpNo`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请输入警号',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-input
                                    placeholder="请输入警号"
                                    v-model="item.leadEmpNo"
                                    style="width: 240px"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="身份证号码"
                                :prop="`blameLeaders.${index}.leadIdCode`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请输入身份证号码',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-input
                                    placeholder="请输入身份证号码"
                                    style="width: 240px"
                                    v-model="item.leadIdCode"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="责任类别"
                                :prop="`blameLeaders.${index}.leadResponsibilityTypeCode`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请选择责任类别',
                                    trigger: ['blur'],
                                }"
                                v-if="problemIsTrue"
                            >
                                <el-radio-group
                                    v-model="item.leadResponsibilityTypeCode"
                                    @change="
                                        (val) =>
                                            (item.leadResponsibilityTypeName = dict.leadResponsibilityType.filter(
                                                (item) => item.dictValue === val
                                            )[0].dictLabel)
                                    "
                                >
                                    <el-radio
                                        v-for="item in dict.leadResponsibilityType"
                                        :key="item.dictCode"
                                        :value="item.dictValue"
                                        >{{ item.dictLabel }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item
                                label="处理结果"
                                :prop="`blameLeaders.${index}.leadHandleResultCode`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请选择处理结果',
                                    trigger: ['blur'],
                                }"
                                v-if="problemIsTrue"
                            >
                                <el-select
                                    v-model="item.leadHandleResultCode"
                                    style="width: 240px"
                                    @change="
                                        (val) => handleChangeLeadResultCode(item, val)
                                    "
                                    clearable
                                    multiple
                                >
                                    <el-option
                                        v-for="item in dict.handleResult"
                                        :key="item.dictCode"
                                        :value="item.dictValue"
                                        :label="item.dictLabel"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <!--              <el-col :span="24">-->
                        <!--                  <el-form-item label="禁闭关联">-->
                        <!--                    <el-select-->
                        <!--                        filterable-->
                        <!--                        remote-->
                        <!--                        :remote-method="fileConfinement"-->
                        <!--                         v-model="item.leadConfinementId"-->
                        <!--                         popper-class="widthClass" >-->
                        <!--                      <template #header>-->
                        <!--                        <el-row>-->
                        <!--                          <el-col :span="2">姓名</el-col>-->
                        <!--                          <el-col :span="2">警号</el-col>-->
                        <!--                          <el-col :span="3">部门</el-col>-->
                        <!--                          <el-col :span="8">事由</el-col>-->
                        <!--                          <el-col :span="4">禁闭时间</el-col>-->
                        <!--                        </el-row>-->
                        <!--                      </template>-->
                        <!--                      <el-option v-for="(val,key) in confinementOpens" :key="key" :label="val.matter" :value="val.id">-->
                        <!--                        <el-row>-->
                        <!--                          <el-col :span="2">{{ val.name }}</el-col>-->
                        <!--                          <el-col :span="2">{{ val.empNo }}</el-col>-->
                        <!--                          <el-col :span="3">-->
                        <!--                            <div class="flex gap-4">-->
                        <!--                              <span>{{ val.departShortName ||'/' }}</span>-->
                        <!--                            </div>-->
                        <!--                          </el-col>-->
                        <!--                          <el-col :span="8">-->
                        <!--                            <el-tooltip :content="val.matter" placement="top">-->
                        <!--                              <el-text truncated> {{val.matter}}</el-text>-->
                        <!--                            </el-tooltip>-->
                        <!--                          </el-col>-->
                        <!--                          <el-col :span="4">-->
                        <!--                            {{val.startTime + "-" +val.endTime}}-->
                        <!--                          </el-col>-->
                        <!--                        </el-row>-->
                        <!--                      </el-option>-->

                        <!--                    </el-select>-->
                        <!--                  </el-form-item>-->
                        <!--              </el-col>-->
                        <el-col
                            :span="12"
                            v-if="item.leadHandleResultCode?.includes('11')"
                        >
                            <el-form-item
                                label="其他处理结果"
                                :prop="`blames.${index}.leadHandleResultNameOther`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请选择处理结果',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-input
                                    v-model="item.leadHandleResultNameOther"
                                    placeholder="其他类型详细描述"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div
                    class="flex center"
                    style="width: 100%"
                    v-if="
                        form.accountabilityTarget !== AccountabilityTarget.DEPARTMENT
                    "
                >
                    <el-button type="primary" plain @click="handleAddBlameLeader">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        添加涉及领导
                    </el-button>
                </div>
            </el-form-item>
        </div>
        <el-form-item
            label-position="top"
            v-if="form.accountabilityTarget !== AccountabilityTarget.PERSONAL"
        >
            <div v-for="(item, index) in form.blames" :key="index">
                <template v-if="item.type === BlameType.DEPARTMENT">
                    <div class="flex between v-center">
                        <h5>涉及单位</h5>
                    </div>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="涉及班子成员"
                                :prop="`blames.${index}.blameEmpNo`"
                                :rules="{
                                    required: true,
                                    message: '请选择涉及班子成员',
                                    trigger: ['blur'],
                                }"
                            >
                                <police-select
                                    :host-level="negative.hostLevel"
                                    :depart-id="
                                        negative.hostLevel === HostLevel.SECOND
                                            ? negative.handleSecondDepartId
                                            : negative.handleThreeDepartId
                                    "
                                    v-model="item.blameEmpNo"
                                    @change="
                                        (police) => {
                                            item.blameName = police.name;
                                            item.blameIdCode = police.idCode;
                                        }
                                    "
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="身份证号码"
                                :prop="`blames.${index}.blameIdCode`"
                                :rules="{
                                    required: true,
                                    message: '请输入身份证号码',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-input
                                    placeholder="请输入身份证号码"
                                    v-model="item.blameIdCode"
                                    style="width: 240px"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="人员属性"
                                :prop="`blames.${index}.ivPersonTypeCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择人员属性',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-radio-group v-model="item.ivPersonTypeCode">
                                    <el-radio :value="PersonType.POLICE"
                                        >民警
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12" v-if="problemIsTrue">
                            <el-form-item
                                label="督察措施"
                                :prop="`blames.${index}.superviseMeasuresCode`"
                            >
                                <el-radio-group
                                    v-model="item.superviseMeasuresCode"
                                    @change="
                                        (val) =>
                                            (item.superviseMeasuresName = dict.superviseMeasures.filter(
                                                (item) => item.dictValue === val
                                            )[0].dictLabel)
                                    "
                                >
                                    <el-radio
                                        v-for="dictItem in dict.superviseMeasures"
                                        :key="dictItem.dictCode"
                                        :value="dictItem.dictValue"
                                        @click.native.prevent="
                                            () => {
                                                if (
                                                    item.superviseMeasuresCode ===
                                                    dictItem.dictValue
                                                ) {
                                                    item.superviseMeasuresCode = '';
                                                    item.superviseMeasuresName = '';
                                                } else {
                                                    item.superviseMeasuresCode =
                                                        dictItem.dictValue;
                                                    item.superviseMeasuresName =
                                                        dictItem.dictLabel;
                                                }
                                            }
                                        "
                                        >{{ dictItem.dictLabel }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item
                        :prop="`blames.${index}.problems`"
                        :rules="{
                            required: true,
                            message: '请添加问题类型',
                            trigger: ['blur'],
                        }"
                        v-if="problemIsTrue"
                        style="margin-bottom: 0"
                    >
                        <div
                            v-for="(problem, j) in item.problems"
                            :key="j"
                            style="width: 98%"
                        >
                            <el-form-item
                                :label="`问题类型${j + 1}`"
                                :prop="`blames.${index}.problems.${j}.threeLevelCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择问题类型',
                                    trigger: ['blur'],
                                }"
                            >
                                <div class="flex between" style="width: 100%">
                                    <div class="flex gap">
                                        <problem-type-select
                                            v-model="problem.threeLevelCode"
                                            @change="
                                                (node) =>
                                                    handleChangeProblem(node, problem)
                                            "
                                        />
                                        <div style="width: 140px">
                                            <el-form-item
                                                label-position="top"
                                                :prop="`blames.${index}.problems.${j}.threeLevelContentOther`"
                                                :rules="{
                                                    required: true,
                                                    message: '请输入',
                                                    trigger: ['blur'],
                                                }"
                                                style="margin-bottom: 0"
                                                v-if="
                                                    problem.threeLevelContent ===
                                                    '其他'
                                                "
                                            >
                                                <el-input
                                                    placeholder="其他类型详细描述"
                                                    v-model="
                                                        problem.threeLevelContentOther
                                                    "
                                                />
                                            </el-form-item>
                                        </div>
                                        <div>
                                            <span>{{
                                                problem.oneLevelContent
                                                    ? problem.oneLevelContent + " / "
                                                    : ""
                                            }}</span>
                                            <span>{{
                                                problem.twoLevelContent
                                                    ? problem.twoLevelContent + " / "
                                                    : ""
                                            }}</span>
                                            <span>{{
                                                problem.threeLevelContent
                                            }}</span>
                                        </div>
                                    </div>

                                    <div class="flex center v-center">
                                        <el-button
                                            type="danger"
                                            plain
                                            @click="item.problems.splice(j, 1)"
                                            v-if="j !== 0"
                                            size="small"
                                        >
                                            <template #icon>
                                                <icon name="el-icon-Delete" />
                                            </template>
                                            删除问题
                                        </el-button>
                                        <el-button
                                            @click="handleAddProblem(item)"
                                            plain
                                            type="primary"
                                            v-if="j === 0"
                                            size="small"
                                        >
                                            <template #icon>
                                                <icon name="el-icon-Plus" />
                                            </template>
                                            添加问题
                                        </el-button>
                                    </div>
                                </div>
                                <div
                                    class="flex center"
                                    style="width: 100%"
                                    v-if="
                                        !item.problems || item.problems.length === 0
                                    "
                                >
                                    <el-button
                                        @click="handleAddProblem(item)"
                                        plain
                                        type="primary"
                                        size="small"
                                    >
                                        <template #icon>
                                            <icon name="el-icon-Plus" />
                                        </template>
                                        添加问题
                                    </el-button>
                                </div>
                            </el-form-item>
                        </div>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item
                                label="主观方面"
                                :prop="`blames.${index}.subjectiveAspectCode`"
                                :rules="{
                                    required: !isUntrue,
                                    message: '请选择主观方面',
                                    trigger: ['blur'],
                                }"
                            >
                                <el-radio-group
                                    v-model="item.subjectiveAspectCode"
                                    @change="
                                        (val) =>
                                            (item.subjectiveAspectName = dict.subjectiveAspect.filter(
                                                (item) => item.dictValue === val
                                            )[0].dictLabel)
                                    "
                                >
                                    <el-radio
                                        v-for="item in dict.subjectiveAspect"
                                        :key="item.dictCode"
                                        :value="item.dictValue"
                                        >{{ item.dictLabel }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item
                                label="责任类别"
                                :prop="`blames.${index}.responsibilityTypeCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择责任类别',
                                    trigger: ['blur'],
                                }"
                                v-if="problemIsTrue"
                            >
                                <el-radio-group
                                    v-model="item.responsibilityTypeCode"
                                    @change="
                                        (val) =>
                                            (item.responsibilityTypeName = dict.responsibilityType.filter(
                                                (item) => item.dictValue === val
                                            )[0].dictLabel)
                                    "
                                >
                                    <el-radio
                                        v-for="item in dict.responsibilityType"
                                        :key="item.dictCode"
                                        :value="item.dictValue"
                                        >{{ item.dictLabel }}
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="处理结果"
                                :prop="`blames.${index}.handleResultCode`"
                                :rules="{
                                    required: true,
                                    message: '请选择处理结果',
                                    trigger: ['blur'],
                                }"
                                v-if="problemIsTrue"
                            >
                                <el-select
                                    v-model="item.handleResultCode"
                                    style="width: 240px"
                                    @change="
                                        (val) =>
                                            handleChangeDepartResultCode(item, val)
                                    "
                                    clearable
                                    multiple
                                >
                                    <el-option
                                        v-for="item in dict.departHandleResult"
                                        :key="item.dictCode"
                                        :value="item.dictValue"
                                        :label="item.dictLabel"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </template>
            </div>
        </el-form-item>
        
        <el-form-item
            prop="files"
            label-position="top"
            :rules="{
                required: true,
                message: '请上传佐证材料',
            }"
        >
            <template #label>
                <h5 class="inline-block mb-0" style="text-align: left">佐证材料</h5>
            </template>
            <div v-if="negative.problemSourcesCode === ProblemSources.JWDC">
                请提供以下佐证材料：
                <p>
                    １、《处理反馈表》（必提供）
                    <a
                        class="link"
                        :href="`${BASE_PATH}/templates/《处理反馈表》.doc`"
                        target="__blank"
                        >下载链接</a
                    >
                </p>
                <p>
                    ２、如申请容错免责，请提供《容错免责申请表》
                    <a
                        class="link"
                        :href="`${BASE_PATH}/templates/《警务评议容错免责申请表》.docx`"
                        target="__blank"
                        >下载链接</a
                    >
                </p>
                <p>
                    ３、针对群众不满意的原因，请提交无过错的音视频、微信或短信截图等证明材料；
                </p>
                <p>
                    ４、针对110、122接处警情况，请提供处警开始、结束及处置过程中的视频截图；
                </p>
                <p>５、自动回访结果不满意时，请提供所队的回访录音；</p>
                <p>６、单位或个人所开展的其他相关工作证明材料。</p>
            </div>
            <div v-if="negative.problemSourcesCode === ProblemSources.JDGL"></div>
            <div style="width: 100%" class="mb-10 mt-10">
                <file-upload-group
                    v-model:files="form.files"
                    :problemSourcesCode="negative.problemSourcesCode"
                />
            </div>
            <file-list v-model:files="form.files" :removeEnable="true" />
        </el-form-item>
    </el-form>

<!--  <el-dialog-->
<!--      v-model="showSamePersonDialog"-->
<!--      title="提示"-->
<!--      width="420px"-->
<!--      align-center-->
<!--      :close-on-click-modal="false"-->
<!--  >-->
<!--    <div class="same-person-dialog">-->
<!--      <div class="icon-wrap">-->
<!--        <el-icon size="48">-->
<!--          <Warning />-->
<!--        </el-icon>-->
<!--      </div>-->

<!--      <div class="content">-->
<!--        <p class="title">人员选择冲突</p>-->
<!--        <p class="desc">-->
<!--          涉及人员与涉及领导不能为同一人，请重新选择。-->
<!--        </p>-->
<!--      </div>-->
<!--    </div>-->

<!--    <template #footer>-->
<!--      <el-button type="primary" @click="showSamePersonDialog = false">-->
<!--        我知道了-->
<!--      </el-button>-->
<!--    </template>-->
<!--  </el-dialog>-->


</template>
<script lang="ts" setup>
import { BASE_PATH } from "@/api/request";
import {
    InspectCase,
    IsRectify,
    AccountabilityTarget,
    BlameType,
    ProblemSources,
    PersonType,
    HostLevel,
    SubjectiveAspect,
} from "@/enums/dictEnums";
import useCatchStore from "@/stores/modules/catch";
import { getConfinementListAll } from "@/api/work/confinement";
import {Warning} from "@element-plus/icons-vue";
import feedback from "@/utils/feedback";
//获取数据
const catchSotre = useCatchStore();
const dict = catchSotre.getDicts([
    "isRectify",
    "personType",
    "superviseMeasures",
    "subjectiveAspect",
    "responsibilityType",
    "handleResult",
    "departHandleResult",
    "protectRights",
    "accountabilityTarget",
    "leadResponsibilityType",
    "resolveSituation",
    "resolveStatus",
    "checkStatus"
]);

const negative = inject("negative");
const form = ref({
    handlePolices: [{}],
});

//禁闭信息数组

let confinementOpens = ref([]);
/**
 * 禁闭信息入参
 * */
let confinementQuery = ref({
    current: 1,
    size: 100,
    departBranch: false,
});

/**
 * 下拉搜索过滤数据(姓名，警号)
 * @param query 输入参数
 *
 * */
const fileConfinement = async (query, type = "All") => {
    confinementQuery.value = {
        current: 1,
        size: 100,
        departBranch: false,
    };
    if (query && type == "All") {
        confinementQuery.value.id = query;
        confinementQuery.value.name = query;
        confinementQuery.value.empNo = query;
    }
    if (query && type == "Id") {
        confinementQuery.value.id = query;
    }
    const data = await getConfinementListAll(confinementQuery.value);
    confinementOpens.value = data.records;
};

const problemIsTrue = computed(() => {
    return form.value.checkStatusCode === '1' || form.value.checkStatusCode === '2' || form.value.checkStatusCode === '3';
});

getFormData();

async function getFormData() {
    form.value = {
        involveDepartId: negative.value.involveDepartId,
        caseNumber: negative.value.caseNumber,
        checkStatus: negative.value.checkStatus,
        checkStatusCode: negative.value.checkStatusCode,
        checkStatusName: negative.value.checkStatusName,
        isRectifyName: negative.value.isRectifyName,
        isRectifyCode: negative.value.isRectifyCode,
        checkStatusDesc: negative.value.checkStatusDesc,
        rectifyDesc: negative.value.rectifyDesc,
        rectifyRestrictionDays: negative.value.rectifyRestrictionDays,
        accountabilityTarget: negative.value.accountabilityTarget,
        unrectifyReason: negative.value.unrectifyReason,
        blames: negative.value.blames,
        blameLeaders: negative.value.blameLeaders,
        files: negative.value.files || [],
        handlePolices:
            !negative.value.handlePolices || negative.value.handlePolices.length === 0
                ? [{}]
                : negative.value.handlePolices,
        // 化解情况
        resolveSituation: negative.value.resolveSituation,
        resolveStatus: negative.value.resolveStatus,
        visitingLeaderName: negative.value.visitingLeaderName,
        visitingLeaderEmpNo: negative.value.visitingLeaderEmpNo,
        processResult: negative.value.processResult,
        handleResult12337: negative.value.handleResult12337,
        handleResult12337Group: negative.value.handleResult12337Group,
        verifiedIsLeader: negative.value.verifiedIsLeader,
    };
    if (negative.value.blames) {
        negative.value.blames.forEach((item) => {
            fileConfinement(item.confinementId, "Id");
        });

    }
    if (negative.value.blameLeaders) {
        negative.value.blameLeaders.forEach((item) => {
            fileConfinement(item.leadConfinementId, "Id");
        });
    }
    if (negative.value.accountabilityTarget != undefined) {
        form.value.accountabilityTarget = negative.value.accountabilityTarget;
        return;
    }
    if (negative.value.problemSourcesCode === ProblemSources.JWDC) {
        form.value.accountabilityTarget = AccountabilityTarget.PERSONAL;
    }
    console.log(form.value);
}

function handleSupervise(val, index) {
    console.log("chang事件触发");
    console.log(`val${index}`, val);
}

function handleAddBlame(type) {
    form.value.blames.push({
        type,
        problems: negative.value.problems.length === 0 ? [{}] : negative.value.problems,
    });
}

function handleAddDepartBlame() {
    handleAddBlame(BlameType.DEPARTMENT);
}

function handleAddPersonalBlame() {
    handleAddBlame(BlameType.PERSONAL);
    if (form.value.blameLeaders.length == 0) {
        handleAddBlameLeader();
    }
}

function handleRemoveBlame(item) {
    form.value.blames.splice(form.value.blames.indexOf(item), 1);
}

function handleAddProblem(blame) {
    if (!blame.problems) {
        blame.problems = [];
    }
    blame.problems.push({});
}

function handleAddBlameLeader() {
    form.value.blameLeaders.push({
        blameIdCodes: [],
    });
}

function handleRemoveBlameLeader(index) {
    form.value.blameLeaders.splice(index, 1);
}

// 更改追责对象
watch(
    () => form.value.accountabilityTarget,
    (val) => {
        if (val === AccountabilityTarget.PERSONAL) {
            if (
                form.value.blames.filter((item) => item.type === BlameType.PERSONAL)
                    .length === 0
            ) {
                handleAddPersonalBlame();
            }
            form.value.blames.forEach((item) => {
                if (item.type === BlameType.DEPARTMENT) {
                    handleRemoveBlame(item);
                }
            });
        }
        if (val === AccountabilityTarget.DEPARTMENT) {
            form.value.blames.forEach((item) => {
                if (item.type === BlameType.PERSONAL) {
                    handleRemoveBlame(item);
                }
            });
            if (
                form.value.blames.filter((item) => item.type === BlameType.DEPARTMENT)
                    .length === 0
            ) {
                handleAddDepartBlame();
            }
            form.value.blameLeaders = [];
        }
        if (val === AccountabilityTarget.PERSONAL_AND_DEPARTMENT) {
            if (
                form.value.blames.filter((item) => item.type === BlameType.PERSONAL)
                    .length === 0
            ) {
                handleAddPersonalBlame();
            }
            if (
                form.value.blames.filter((item) => item.type === BlameType.DEPARTMENT)
                    .length === 0
            ) {
                handleAddDepartBlame();
            }
        }
    }
);

function handleChangeCheckStatus(val, item) {
    form.value.checkStatusName = dict.checkStatus.filter(
        (item) => item.dictValue === val
    )[0].dictLabel;
    if (val) {
        if (val === '1') {
            form.value.checkStatus = '1';
        } else if (val === '2' || val === '3') {
            form.value.checkStatus = '2';
        } else if (val === '4'|| val === '5') {
            form.value.checkStatus = '3';
        }
    } else {
        form.value.checkStatus = '';
    }
}

function handleChangePolice(police, item) {
    item.blameName = police.name;
    item.blameIdCode = police.idCode;
    item.ivPersonTypeCode = police.personType;
}

function handleChangeSubjectiveAspect(val, item) {
    if (val === SubjectiveAspect.OTHER) {
        item.subjectiveAspectName = "";
        return;
    }
    item.subjectiveAspectName = dict.subjectiveAspect.filter(
        (item) => item.dictValue === val
    )[0].dictLabel;
}

function handleChangeProblem(node, problem) {
    if (node.level === 3) {
        problem.threeLevelContent = node.label;
        problem.oneLevelCode = node.parent.parent.key;
        problem.oneLevelContent = node.parent.parent.label;
        problem.twoLevelCode = node.parent.key;
        problem.twoLevelContent = node.parent.label;
    }
}

function handleCheckboxChange(blameLeader, bool, val) {
    if (bool) {
        blameLeader.blameIdCodes.push(val);
    } else {
        blameLeader.blameIdCodes.splice(blameLeader.blameIdCodes.indexOf(val), 1);
    }
}

function handleChangeResultCode(item) {
    if (item.handleResultCode && item.handleResultCode.length > 0) {
        item.handleResultName = dict.handleResult
            .filter((obj) => item.handleResultCode.includes(obj.dictValue))
            .map((obj) => obj.dictLabel)
            .join("、");
    } else {
        item.handleResultName = "";
    }
}

function handleChangeLeadResultCode(item) {
    if (item.leadHandleResultCode && item.leadHandleResultCode.length > 0) {
        item.leadHandleResultName = dict.handleResult
            .filter((obj) => item.leadHandleResultCode.includes(obj.dictValue))
            .map((obj) => obj.dictLabel)
            .join("、");
    } else {
        item.leadHandleResultName = "";
    }
}

function handleChangeDepartResultCode(item) {
    if (item.handleResultCode && item.handleResultCode.length > 0) {
        item.handleResultName = dict.departHandleResult
            .filter((obj) => item.handleResultCode.includes(obj.dictValue))
            .map((obj) => obj.dictLabel)
            .join("、");
    } else {
        item.handleResultName = "";
    }
}

function validateHandlePolices(rule, value, cb) {
    console.log(value);
    if (value.length === 0) {
        cb(new Error("请选择经办人"));
    } else if (!value[0].name) {
        cb(new Error("请选择经办人"));
    } else if (!value[0].mobile) {
        cb(new Error("请输入经办人联系方式"));
    } else {
        cb();
    }
}

function handleResult12337(item) {
    form.value.handleResult12337Group = item.group;
}

const formRef = ref(null);

const showSamePersonDialog = ref(false);
// 是否属实
const isUntrue = computed(() => ['4', '5'].includes(String(form.value.checkStatusCode)));


async function validate() {
    // 解决核查办理 当涉及对象是涉及个人时，有一条涉及单位的数据，导致问题无法提交的问题。
    if (form.value.accountabilityTarget === AccountabilityTarget.PERSONAL) {
        form.value.blames = form.value.blames.filter(
            (item) => item.type === BlameType.PERSONAL
        );
    }
    await formRef.value.validate();
    if (!isUntrue.value && form.value.accountabilityTarget !== AccountabilityTarget.DEPARTMENT) {
        let arr = [];
        form.value.blameLeaders.forEach((item) => {
            arr = arr.concat(item.blameIdCodes);
        });
        const blame = form.value.blames.filter(
            (item) => item.type === BlameType.PERSONAL && !arr.includes(item.blameIdCode)
        );
        if (blame.length) {
          feedback.msgError(`涉及人员【${blame.map((item) => item.blameName).join("、")}】未关联领导`);
            throw new Error(
                `涉及人员【${blame.map((item) => item.blameName).join("、")}】未关联领导`
            );
        }
    }
    // 检查涉及人员和涉及领导是否为同一人
    const blameIdCodes = form.value.blames
        .filter(item => item.type === BlameType.PERSONAL && item.blameIdCode)
        .map(item => item.blameIdCode);

    const leaderIdCodes = form.value.blameLeaders
        .filter(item => item.leadIdCode)
        .map(item => item.leadIdCode);

    const duplicates = blameIdCodes.filter(idCode =>
        leaderIdCodes.includes(idCode)
    );

    if (duplicates.length > 0) {
      const duplicatePerson = form.value.blames.find(item =>
          item.blameIdCode === duplicates[0]
      );

      if (duplicatePerson) {
        // showSamePersonDialog.value = true;
        feedback.msgError( `涉及人员【${duplicatePerson.blameName}】与涉及领导不能为同一人`);
        throw new Error(`涉及人员【${duplicatePerson.blameName}】与涉及领导不能为同一人`);
      }
    }


  // ========== 处理结果校验逻辑 ==========
  // 1. 涉及人员：处理结果为14（不予追责）时，不能同时选择其他处理结果
  const personalBlames = form.value.blames.filter(item => item.type === BlameType.PERSONAL);
  for (const item of personalBlames) {
    if (item.handleResultCode && item.handleResultCode.length > 0) {
      // 如果包含14（不予追责），则不能同时选择其他处理结果
      if (item.handleResultCode.includes('14')) {
        if (item.handleResultCode.length > 1) {
          feedback.msgError(`涉及人员【${item.blameName}】的处理结果为"不予追责"时，不能同时选择其他处理结果`);
          throw new Error(`涉及人员【${item.blameName}】的处理结果为"不予追责"时，不能同时选择其他处理结果`);
        }
      }
      // 涉及人员不能同时包含1、1-1、1-2
      const has1 = item.handleResultCode.includes('1');
      const has1_1 = item.handleResultCode.includes('1-1');
      const has1_2 = item.handleResultCode.includes('1-2');
      if ((has1 && has1_1) || (has1 && has1_2) || (has1_1 && has1_2)) {
        feedback.msgError(`涉及人员【${item.blameName}】的处理结果不能同时包含"所队通报批评"、"市局通报批评"、"分局通报批评"`);
        throw new Error(`涉及人员【${item.blameName}】的处理结果不能同时包含"所队通报批评"、"市局通报批评"、"分局通报批评"`);
      }
    }
  }

  // 2. 涉及领导：处理结果为14（不予追责）时，不能同时选择其他处理结果
  const leaderBlames = form.value.blameLeaders;
  for (const item of leaderBlames) {
    if (item.leadHandleResultCode && item.leadHandleResultCode.length > 0) {
      // 如果包含14（不予追责），则不能同时选择其他处理结果
      if (item.leadHandleResultCode.includes('14')) {
        if (item.leadHandleResultCode.length > 1) {
          feedback.msgError(`涉及领导【${item.leadName}】的处理结果为"不予追责"时，不能同时选择其他处理结果`);
          throw new Error(`涉及领导【${item.leadName}】的处理结果为"不予追责"时，不能同时选择其他处理结果`);
        }
      }
      // 涉及领导不能同时包含1、1-1、1-2
      const has1 = item.leadHandleResultCode.includes('1');
      const has1_1 = item.leadHandleResultCode.includes('1-1');
      const has1_2 = item.leadHandleResultCode.includes('1-2');
      if ((has1 && has1_1) || (has1 && has1_2) || (has1_1 && has1_2)) {
        feedback.msgError(`涉及领导【${item.leadName}】的处理结果不能同时包含"所队通报批评"、"市局通报批评"、"分局通报批评"`);
        throw new Error(`涉及领导【${item.leadName}】的处理结果不能同时包含"所队通报批评"、"市局通报批评"、"分局通报批评"`);
      }
    }
  }

  // 3. 涉及单位：处理结果为2-1（涉及单位的不与追责）时，不能同时选择其他处理结果
  const departBlames = form.value.blames.filter(item => item.type === BlameType.DEPARTMENT);
  for (const item of departBlames) {
    if (item.handleResultCode && item.handleResultCode.length > 0) {
      // 如果包含2-1（涉及单位的不与追责），则不能同时选择其他处理结果
      if (item.handleResultCode.includes('2-1')) {
        if (item.handleResultCode.length > 1) {
          feedback.msgError(`涉及单位【${item.blameDepartName}】的处理结果为"不与追责"时，不能同时选择其他处理结果`);
          throw new Error(`涉及单位【${item.blameDepartName}】的处理结果为"不与追责"时，不能同时选择其他处理结果`);
        }
      }
    }
  }
  // ========== 处理结果校验逻辑结束 ==========

    return form.value;
}

function getData() {
    return form.value;
}

defineExpose({
    validate,
    getData,
});
</script>
<style>
.widthClass {
    min-width: 50% !important;
}
</style>
<style lang="scss" scoped>
.el-form-item .el-form-item {
    margin-bottom: 18px;
}

:deep() {
    .el-form-item--label-right .el-form-item__label {
        text-align: right;
        line-height: 32px;
        margin-bottom: 0;
    }
}

p {
    margin: 0;
    line-height: 1.4;
}

.same-person-dialog {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0 12px;
}

.icon-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background-color: rgba(230, 162, 60, 0.12);
  color: #e6a23c;
  margin-bottom: 12px;
}

.content {
  text-align: center;
}

.content .title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 6px;
}

.content .desc {
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
}

</style>
