<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="checkStatus"
            :rules="{
                        required: true,
                        message: '请选择核查情况',
                        trigger: ['blur'],
                    }"
        >
          <el-radio-group
              v-model="form.checkStatus"
              @change="(val) => handleChangeCheckStatus(val, item)"
          >
            <el-radio
                v-for="item in dict.inspectCase"
                :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="isRectifyCode"
            :rules="{
                        required: true,
                        message: '请选择是否整改',
                        trigger: ['blur'],
                    }"
            v-if="form.checkStatus !== InspectCase.FALSE"
        >
          <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="negative.problemSourcesCode !== ProblemSources.JWDC"
      >
        <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="
                    form.checkStatus !== InspectCase.FALSE &&
                    form.isRectifyCode === IsRectify.NOT
                "
      >
        <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"
      />
    </el-form-item>
    <el-form-item
        label="问题整改情况"
        prop="rectifyDesc"
        v-if="form.checkStatus !== InspectCase.FALSE && form.isRectifyCode === InspectCase.TRUE"
        :rules="{
                required: true,
                message: '请输入问题整改情况',
                trigger: ['blur'],
            }"
    >
      <el-input
          type="textarea"
          placeholder="请输入问题整改情况"
          v-model="form.rectifyDesc"
      />
    </el-form-item>

    <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="handleRemovePersonal(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.handleSecondDepartId
                                            : negative.handleThreeDepartId
                                    "
                    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="form.checkStatus !== InspectCase.FALSE"
              style="margin-bottom: 0"
          >
            <el-row style="width: 98%">
              <el-col
                  :span="24"
                  v-for="(problem, j) in item.problems"
                  :key="j"
              >
                <el-form-item
                    :label="`问题类型${j + 1}`"
                    :prop="`blames.${index}.problems.${j}.threeLevelCode`"
                    :rules="{
                                        required: true,
                                        message: '请选择问题类型',
                                        trigger: ['blur'],
                                    }"
                >
                  <div
                      class="flex between"
                      style="width: 90%"
                  >
                    <problem-type-select
                        v-model="problem.threeLevelCode"
                        @change="
                                                (node) =>
                                                    handleChangeProblem(
                                                        node,
                                                        problem
                                                    )
                                            "
                    />
                    <div
                        class="flex center v-center"
                        style="width: 90px"
                    >
                      <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>
              </el-col>
            </el-row>
          </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="form.checkStatus !== InspectCase.FALSE"
              >
                <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="form.checkStatus !== InspectCase.FALSE"
            >
              <el-form-item
                  label="主观方面"
                  :prop="`blames.${index}.subjectiveAspectCode`"
                  :rules="{
                                    required: true,
                                    message: '请选择主观方面',
                                    trigger: ['blur'],
                                }"
              >
                <div class="flex gap">
                  <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
                                        "
                  >
                    <el-input
                        v-model="item.subjectiveAspectName"
                        placeholder="请输入其他主观方面"
                        style="width: 120px"
                    />
                  </el-form-item>
                </div>
              </el-form-item>
            </el-col>

            <el-col
                :span="12"
                v-if="form.checkStatus !== InspectCase.FALSE"
            >
              <el-form-item
                  label="处理结果"
                  :prop="`blames.${index}.handleResultCode`"
                  :rules="{
                                    required: true,
                                    message: '请选择处理结果',
                                    trigger: ['blur'],
                                }"
              >
                <el-select
                    v-model="item.handleResultCode"
                    style="width: 240px"
                    @change="
                                        (val) =>
                                            (item.handleResultName =
                                                dict.handleResult.filter(
                                                    (item) =>
                                                        item.dictValue === val
                                                )[0].dictLabel)
                                    "
                >
                  <el-option
                      v-for="item in dict.handleResult"
                      :key="item.dictCode"
                      :value="item.dictValue"
                      :label="item.dictLabel"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <template v-if="form.checkStatus !== InspectCase.FALSE">
              <el-col :span="12">
                <el-form-item
                    label="督察措施"
                    :prop="`blames[${index}].superviseMeasuresCode`"
                >
                  <el-radio-group
                      v-model="item.superviseMeasuresCode"
                      clearabled
                  >
                    <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-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>
            </template>
          </el-row>
        </div>
        <div class="flex center" style="width: 100%">
          <el-button type="primary" plain @click="handleAddPersomal"
          >
            <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: true,
                    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: true,
                                    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: true,
                                    message: '请选择涉及领导',
                                    trigger: ['blur'],
                                }"
              >
                <police-select
                    :host-level="negative.hostLevel"
                    :depart-id="
                                        negative.hostLevel === HostLevel.SECOND
                                            ? negative.handleSecondDepartId
                                            : negative.handleThreeDepartId
                                    "
                    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: true,
                                    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: true,
                                    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: true,
                                    message: '请选择责任类别',
                                    trigger: ['blur'],
                                }"
                  v-if="form.checkStatus !== InspectCase.FALSE"
              >
                <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: true,
                                    message: '请选择处理结果',
                                    trigger: ['blur'],
                                }"
                  v-if="form.checkStatus !== InspectCase.FALSE"
              >
                <el-select
                    v-model="item.leadHandleResultCode"
                    style="width: 240px"
                    @change="
                                        (val) =>
                                            (item.leadHandleResultName =
                                                dict.handleResult.filter(
                                                    (item) =>
                                                        item.dictValue === val
                                                )[0].dictLabel)
                                    "
                >
                  <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="12"
                v-if="form.checkStatus !== InspectCase.FALSE"
            >
              <el-form-item
                  label="督察措施"
                  :prop="`blameLeaders.${index}.leadMeasuresCode`"
              >
                <el-radio-group v-model="item.leadMeasuresCode">
                  <el-radio
                      v-for="dictItem in dict.superviseMeasures"
                      :key="dictItem.dictCode"
                      :value="dictItem.dictValue"
                      @click.native.prevent="
                                            () => {
                                                if (
                                                    item.leadMeasuresCode ===
                                                    dictItem.dictValue
                                                ) {
                                                    item.leadMeasuresCode = '';
                                                    item.leadMeasuresName = '';
                                                } else {
                                                    item.leadMeasuresCode =
                                                        dictItem.dictValue;
                                                    item.leadMeasuresName =
                                                        dictItem.dictLabel;
                                                }
                                            }
                                        "
                  >{{ dictItem.dictLabel }}
                  </el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col
                :span="12"
                v-if="
                                negative.problemSourcesCode !==
                                ProblemSources.JWDC
                            "
            >
              <el-form-item
                  label="维权容错"
                  :prop="`blameLeaders.${index}.leadProtectRightsCode`"
              >
                <el-radio-group
                    v-model="item.leadProtectRightsCode"
                    @change="
                                        (val) =>
                                            (item.leadProtectRightsName =
                                                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-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>
      <el-divider/>
    </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="form.checkStatus !== InspectCase.FALSE"
            >
              <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="item in dict.superviseMeasures"
                      :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="form.checkStatus !== InspectCase.FALSE"
              style="margin-bottom: 0"
          >
            <el-row style="width: 80%">
              <el-col
                  :span="24"
                  v-for="(problem, j) in item.problems"
                  :key="j"
              >
                <el-form-item
                    :label="`问题类型${j + 1}`"
                    :prop="`blames.${index}.problems.${j}.threeLevelCode`"
                    :rules="{
                                        required: true,
                                        message: '请选择问题类型',
                                        trigger: ['blur'],
                                    }"
                >
                  <div
                      class="flex between"
                      style="width: 90%"
                  >
                    <problem-type-select
                        v-model="problem.threeLevelCode"
                        @change="
                                                (node) =>
                                                    handleChangeProblem(
                                                        node,
                                                        problem
                                                    )
                                            "
                    />
                    <div
                        class="flex center v-center"
                        style="width: 90px"
                    >
                      <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>
              </el-col>
            </el-row>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item
                  label="主观方面"
                  :prop="`blames.${index}.subjectiveAspectCode`"
                  :rules="{
                                    required: true,
                                    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="form.checkStatus !== InspectCase.FALSE"
              >
                <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="form.checkStatus !== InspectCase.FALSE"
              >
                <el-select
                    v-model="item.handleResultCode"
                    style="width: 240px"
                    @change="
                                        (val) =>
                                            (item.handleResultName =
                                                dict.handleResult.filter(
                                                    (item) =>
                                                        item.dictValue === val
                                                )[0].dictLabel)
                                    "
                >
                  <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-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>
</template>
<script lang="ts" setup>
import {BASE_PATH} from "@/api/request";
import {
  InspectCase,
  IsRectify,
  AccountabilityTarget,
  BlameType,
  AssistCase,
  ProblemSources,
  PersonType,
  HostLevel,
  SubjectiveAspect,
} from "@/enums/dictEnums";
import useCatchStore from "@/stores/modules/catch";

const catchSotre = useCatchStore();
const dict = catchSotre.getDicts([
  "inspectCase",
  "isRectify",
  "personType",
  "superviseMeasures",
  "subjectiveAspect",
  "responsibilityType",
  "handleResult",
  "protectRights",
  "assistCase",
  "accountabilityTarget",
  "leadResponsibilityType",
]);

const negative = inject("negative");
const form = ref({});

getFormData();

watch(negative, () => {
  getFormData();
});

function getFormData() {
  form.value = {
    involveDepartId: negative.value.involveDepartId,
    caseNumber: negative.value.caseNumber,
    checkStatus: negative.value.checkStatus,
    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: "",
    blames: negative.value.blames,
    blameLeaders: negative.value.blameLeaders,
    files: negative.value.files || [],
  };
  if (negative.value.accountabilityTarget != undefined) {
    form.value.accountabilityTarget = negative.value.accountabilityTarget;
    return;
  }
  if (negative.value.problemSourcesCode === ProblemSources.JWDC) {
    form.value.accountabilityTarget = AccountabilityTarget.PERSONAL;
  }
}

function handleAddPersomal() {
  form.value.blames.push({
    type: BlameType.PERSONAL,
    problems: [{}],
  });
}

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

function handleAddProblem(blame) {
  blame.problems.push({});
}

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

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

function handleChangeBlame(val, blame) {
  const police = polices.value.filter((item) => item.empNo === val)[0];
  blame.blameName = police.name;
  blame.blameIdCode = police.idCode;
}

function handleChangeLead(val, blame) {
  const police = polices.value.filter((item) => item.empNo === val)[0];
  blame.leadName = police.name;
  blame.leadIdCode = police.idCode;
}

// 更改追责对象
watch(() => form.value.accountabilityTarget, (val) => {
  if (
      val === AccountabilityTarget.PERSONAL &&
      !form.value.blames.filter((item) => item.type === BlameType.PERSONAL)
          .length
  ) {
    handleAddPersomal();
    form.value.blames.forEach((item) => {
      if (item.type === BlameType.DEPARTMENT) {
        handleRemovePersonal(item);
      }
    });
    
  }
  if (val === AccountabilityTarget.DEPARTMENT) {
    form.value.blames.forEach((item) => {
      if (item.type === BlameType.PERSONAL) {
        handleRemovePersonal(item);
      }
    });
    if (
        !form.value.blames.filter(
            (item) => item.type === BlameType.DEPARTMENT
        ).length
    ) {
      form.value.blames.push({
        type: BlameType.DEPARTMENT,
        problems: [{}],
      });
    }
    form.value.blameLeaders = []
  }
  if (val === AccountabilityTarget.PERSONAL_AND_DEPARTMENT) {
    if (
        !form.value.blames.filter(
            (item) => item.type === BlameType.PERSONAL
        ).length
    ) {
      handleAddPersomal();
    }
    if (
        !form.value.blames.filter(
            (item) => item.type === BlameType.DEPARTMENT
        ).length
    ) {
      form.value.blames.push({
        type: BlameType.DEPARTMENT,
        problems: [{}],
      });
    }
  }
})

function handleChangeCheckStatus(val, item) {
  form.value.checkStatusName = dict.inspectCase.filter(
      (item) => item.dictValue === val
  )[0].dictLabel;
  if (
      form.value.accountabilityTarget !== AccountabilityTarget.DEPARTMENT &&
      form.value.blames.length === 0
  ) {
    handleAddPersomal();
  }
}

function handleChangePolice(police, item) {
  console.log(police);
  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
    );
  }
}

const formRef = ref(null);

async function validate() {
  const flag = await formRef.value.validate();
  if (flag) {
    if (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) {
        throw new Error(
            `涉及人员【${blame
                .map((item) => item.blameName)
                .join("、")}】未关联领导`
        );
      }
    }
    return form.value;
  }
}

function getData() {
  return form.value;
}

defineExpose({
  validate,
  getData,
});
</script>
<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;
}
</style>
