<template>
  <div id="">
    <a-layout>
      <a-layout-header>
        <a-form
          layout="inline"
          :model="searchParams"
          class="demo-form-inline"
          style="display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px 24px; margin-bottom: 1%;">
          <a-form-item label="督查对象名称" style="white-space: nowrap; flex-shrink: 0; margin-bottom: 0;">
            <a-input v-model="searchParams.objname" placeholder="督查对象名称" allow-clear />
          </a-form-item>
          <a-form-item label="是否隐藏" style="white-space: nowrap; flex-shrink: 0; margin-bottom: 0;">
            <a-select v-model:value="searchParams.display" placeholder="是否隐藏" allow-clear style="width: 200px;">
              <a-select-option value="0">显示</a-select-option>
              <a-select-option value="1">隐藏</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item style="margin-bottom: 0;">
            <a-button type="primary" @click="getPageList">查询</a-button>
          </a-form-item>
          <a-form-item v-if="readOnlyRole == 'display'" style="margin-bottom: 0;">
            <a-button type="primary" @click="addPageListFlag = true">添加</a-button>
          </a-form-item>
        </a-form>
      </a-layout-header>
      <a-layout-content>
        <a-table
          :columns="columns"
          :data-source="pageList"
          row-key="id"
          style="width: 100%;"
          :style="{ height: tableHeight + 'px' }"
          :scroll="{ y: tableHeight - 60}"
          :pagination="false"
          bordered>
          <template #bodyCell="{ column, record, text }">
            <template v-if="column.dataIndex === 'objname'">
              <a-button v-if="readOnlyRole == 'display'" type="link" size="small" @click="getPblms(record)">{{ record.objname }}</a-button>
              <span v-else>{{ record.objname }}</span>
            </template>
            <template v-else-if="column.dataIndex === 'action'">
              <template v-if="readOnlyRole == 'display'">
                <a-button v-if="record.display == '0'" type="primary" size="small" @click="displayClick(record)">隐藏</a-button>
                <a-button v-if="record.display == '1'" type="primary" size="small" @click="displayClick(record)">取消隐藏</a-button>
                <a-button v-if="record.optionalStatus == '0'" type="danger" size="small" @click="deletePblms(record)" style="margin-left: 10px;">删除</a-button>
              </template>
            </template>
            <template v-else-if="column.slots?.customRender === 'ellipsisTooltip'">
              <a-tooltip placement="topLeft" :title="text">
                <span>{{ text }}</span>
              </a-tooltip>
            </template>
            <template v-else>
              {{ text }}
            </template>
          </template>
        </a-table>
        <a-pagination
          show-size-changer
          v-model:pageSize="searchParams.pageSize"
          :total="total"
          :page-size-options="['10', '20', '30', '40']"
          :show-total="total => `共${total}条`"
          @showSizeChange="handleSizeChange"
          @change="handleCurrentChange"
          style="margin-top: 10px;" />
      </a-layout-content>
    </a-layout>
    <a-modal title="添加督查对象" :open="addPageListFlag" @cancel="addPageListFlag = false">
      <a-form
        :model="addSearchParams"
        :rules="addSearchRules"
        ref="addSearchRef"
        class="demo-form-inline"
        :label-col="{span: 8}"
        style="max-width: 100%;">
        <a-form-item label="督查对象类型名称" prop="objname">
          <a-input v-model="addSearchParams.objname" placeholder="督查对象类型名称" allow-clear />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button type="primary" @click="addPageList">确定</a-button>
        <a-button @click="addPageListFlag =false">取消</a-button>
      </template>
    </a-modal>
    <a-modal
      :title="title + '督查对象列表'"
      :open="objectListFlag"
      @cancel="closeObj"
      :footer="null"
      width="70%">
      <a-layout-header>
        <a-form
          layout="inline"
          :model="objListParams"
          class="demo-form-inline"
          style="display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px 24px; margin-bottom: 1%;">
          <a-form-item label="督查对象名称" style="white-space: nowrap; flex-shrink: 0; margin-bottom: 0;">
            <a-input v-model="objListParams.nm" placeholder="督查对象名称" allow-clear />
          </a-form-item>
          <a-form-item label="行政区划" style="white-space: nowrap; flex-shrink: 0; margin-bottom: 0;">
            <a-tree-select
              v-model="objListParams.adCode"
              :tree-data="rectifyUnits"
              show-search
              placeholder="请选择行政区划"
              search-placeholder="请输入行政区划"
              :replaceFields="{children: 'children', label: 'label', key: 'id', value: 'id'}"
              treeNodeFilterProp="label"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              allow-clear />
          </a-form-item>
          <a-form-item v-if="isSend" label="下发状态" style="white-space: nowrap; flex-shrink: 0; margin-bottom: 0;">
            <a-select v-model="objListParams.isStatrt" placeholder="请选择下发状态" allow-clear style="width: 200px;">
              <a-select-option value="0">未下发</a-select-option>
              <a-select-option value="1">已下发</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item style="margin-bottom: 0;">
            <a-button type="primary" @click="getObjList">查询</a-button>
          </a-form-item>
          <a-form-item v-if="isAdd" style="margin-bottom: 0;">
            <a-button type="primary" @click="showAddObjList">添加</a-button>
          </a-form-item>
          <a-form-item v-if="isFile" style="margin-bottom: 0;">
            <a-upload
              name="file"
              :multiple="false"
              action="/modular-source/6ace62b8e2bb467d98286d260aebcef2/pdcApi/att/gnrl/base/importListOfIssues"
              :data="{ countNumRelevancy: countNumRelevancy }"
              :accept="'.xls,.xlsx'"
              :headers="header"
              @change="handleChange"
              :show-upload-list="false">
              <a-button type="primary">文件导入</a-button>
            </a-upload>
          </a-form-item>
          <a-form-item v-if="isSend">
            <a-button type="primary" @click="sendObjBatch">批量下发</a-button>
          </a-form-item>
          <a-form-item v-if="isJoggle">
            <a-button type="primary" @click="showInterfaceImport">接口导入</a-button>
          </a-form-item>
          <a-form-item v-if="!isAdd && !isFile && !isJoggle">
            <a-button type="primary" @click="isInterfaceInfo = true">接口信息</a-button>
          </a-form-item>
        </a-form>
      </a-layout-header>
      <a-layout-content>
        <a-table
          :columns="objColumns"
          :data-source="objList"
          row-key="id"
          style="height:450px"
          :scroll="{x: '100%', y: 380}"
          :loading="isLoading"
          :pagination="false"
          bordered
          :row-selection="{
            selectedRowKeys: selectedRowKeys,
            onChange: handleSelectionChange,
          }"
          >
          <template #bodyCell="{ column, record, text, index }">
            <template v-if="column.dataIndex === 'index'">
              <span>{{ (objListParams.pageNum - 1) * objListParams.pageSize + (index + 1) }}</span>
            </template>
            <template v-else-if="column.dataIndex === 'action'">
              <a-button v-if="isSend && '0' === record.isStatrt " size="small" type="primary" @click="sendObj(record)">下发</a-button>
              <a-button size="small" type="primary" @click="showEditObjList(record)" style="margin-left:3px !important;">编辑</a-button>
              <a-button size="small" type="primary" @click="showRecord(record)" style="margin-left:3px !important;">记录</a-button>
            </template>
            <template v-else-if="column.slots?.customRender === 'ellipsisTooltip'">
              <a-tooltip placement="topLeft" :title="text">
                <span>{{ text }}</span>
              </a-tooltip>
            </template>
            <template v-else>
              {{ text }}
            </template>
          </template>
        </a-table>
        <a-pagination
          show-size-changer
          v-model="objListParams.pageNum"
          v-model:pageSize="objListParams.pageSize"
          :total="objTotal"
          :page-size-options="['5', '10', '20', '30', '40']"
          :show-total="objTotal => `共${objTotal}条`"
          @showSizeChange="handleSizeChangeObj"
          @change="handleCurrentChangeObj"
          style="margin-top: 10px;" />
      </a-layout-content>
    </a-modal>
    <a-modal :title="`${addObjTitle}督查对象列表对象`" :open="addObjFlag" @cancel="addObjFlag = false">
      <a-form
        :model="addObjParams"
        class="demo-form-inline"
        :label-col="{span: 8}"
        style="max-width: 100%;"
        :rules="addObjRules"
        ref="addObjRef">
        <a-form-item label="督查对象名称" prop="nm">
          <a-input v-model="addObjParams.nm" placeholder="督查对象名称" allow-clear />
        </a-form-item>
        <a-form-item v-if="addObjTitle !== '修改'" label="地市" prop="adCity">
          <a-select v-model="addObjParams.adCity" @change="cityChange" placeholder="选择地市" style="min-width: 200px;">
            <a-select-option v-for="item in cityList" :key="item.adCode" :value="item.adCode">{{ item.adName
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-if="addObjTitle !== '修改'" label="区县" prop="adCode">
          <a-select v-model="addObjParams.adCode" @change="countryName" placeholder="选择区县" style="min-width: 200px;">
            <a-select-option v-for="item in countryList" :key="item.adCode" :value="item.adCode">{{ item.adName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="工程位置">
          <a-input v-model="addObjParams.loc" placeholder="工程位置" allow-clear />
        </a-form-item>
        <a-form-item label="高德经度">
          <a-input v-model="addObjParams.gdX" placeholder="经度" allow-clear />
        </a-form-item>
        <a-form-item label="高德纬度">
          <a-input v-model="addObjParams.gdY" placeholder="纬度" allow-clear />
        </a-form-item>
        <a-form-item label="百度经度">
          <a-input v-model="addObjParams.bdX" placeholder="经度" allow-clear />
        </a-form-item>
        <a-form-item label="百度纬度">
          <a-input v-model="addObjParams.bdY" placeholder="经度" allow-clear />
        </a-form-item>
        <a-form-item label="工程编码">
          <a-input v-model="addObjParams.projectCode" placeholder="工程编码" allow-clear />
        </a-form-item>
        <a-form-item label="联系人">
          <a-input v-model="addObjParams.contacts" placeholder="联系人" allow-clear />
        </a-form-item>
        <a-form-item label="联系电话">
          <a-input v-model="addObjParams.telephone" placeholder="联系电话" allow-clear />
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea v-model="addObjParams.note" placeholder="备注" allow-clear />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button type="primary" @click="addObjList">保存</a-button>
        <a-button @click="addObjFlag=false">取消</a-button>
      </template>
    </a-modal>

    <a-modal :title="editTitle + '数据调整记录'" :open="isEditRecordShow" width="40%" @cancel="isEditRecordShow = false" :footer="null">
      <span style="display: block;max-height:500px;overflow:auto;padding: 20px">
        <a-timeline v-if="activities && activities.length>0">
          <a-timeline-item v-for="(activity, index) in activities" :key="index" color="#3fa4ff">
            <p style="font-size: 18px;font-weight: bold;">{{ activity.timestamp }}</p>
            <p style="font-size: 18px;">{{ activity.title }}</p>
            <p style="font-size: 15px;">{{ activity.detail }}</p>
          </a-timeline-item>
        </a-timeline>
        <div
          style="width:100%;height:100%;display: flex;align-items: center;justify-content: center;font-size: 20px;font-weight: bold"
          v-else>无记录</div>
      </span>
    </a-modal>

    <a-modal
      title="接口导入"
      :open="isInterfaceImport"
      width="40%"
      @cancel="isInterfaceImport = false">
      <a-form
        :model="interfaceImportParams"
        class="demo-form-inline"
        :label-col="{ span: 8 }"
        style="max-width: 100%;"
        :rules="interfaceImportParamsRules"
        ref="interfaceImportParamsRef">
        <a-form-item label="链接" prop="objUrl">
          <a-input v-model="interfaceImportParams.objUrl" placeholder="链接" allow-clear />
        </a-form-item>
        <a-form-item label="秘钥" prop="apiKey">
          <a-input v-model="interfaceImportParams.apiKey" placeholder="秘钥" allow-clear />
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button type="primary" @click="getInterfaceImport" :loading="isLoading">确定</a-button>
        <a-button @click="isInterfaceImport = false">取消</a-button>
      </template>
    </a-modal>

    <a-modal title="接口信息" :open="isInterfaceInfo" with="50%" @cancel="isInterfaceInfo = false">
      <a-form style="max-width: 100%; gap: 16px 24px;">
        <a-form-item label="链接">
          <span>{{ objUrl }}</span>
        </a-form-item>
        <a-form-item label="秘钥">
          <span>{{ apiKey }}</span>
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button @click="isInterfaceInfo = false">关闭</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import request from '../../utils/gw_ajax_request';
import {
  addObjList,
  addPageList,
  deletePblms,
  displayUpdate,
  getAdData,
  getByObjNum,
  getDataAnalysis,
  getObjList,
  getOrgTreeJson,
  getPageList,
  getStatus,
  importByUrlAdd,
  importByUrlUpdate,
  sendObjList,
  update2ObjList,
} from './Common/jcxx_api';
export default {
  data() {
    return {
      header: {
        persId: localStorage.getItem('persId'),
        accessToken: localStorage.getItem('accessToken'),
        orgId: localStorage.getItem('orgId'),
      },
      tableHeight: window.innerHeight - 260,
      searchParams: {
        objname: '',
        display: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      pageList: [],
      columns: [
        {
          title: '督查对象名称',
          dataIndex: 'objname',
          slots: { customRender: 'objname' },
          width: 100,
          ellipsis: true,
        },
        {
          title: '督查对象数量',
          dataIndex: 'countNum1',
          width: 100,
          ellipsis: true,
        },
        {
          title: '关联全覆盖督查事项',
          dataIndex: 'nm',
          width: 100,
          ellipsis: true,
        },
        {
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          width: 100,
        },
      ],
      addPageListFlag: false,
      addSearchParams: {
        objname: '',
      },
      addSearchRules: {
        objname: [
          {
            required: true,
            message: '请输入督查对象类型名称',
            trigger: 'blur',
          },
        ],
      },
      title: '',
      objectListFlag: false,
      countNumRelevancy: '',
      objListParams: {
        nm: '',
        isStatrt: '',
        adCode: '',
        pageNum: 1,
        pageSize: 10,
      },
      rectifyUnits: [],
      objTotal: 0,
      objList: [],
      objColumns: [
        {
          title: '序号',
          dataIndex: 'index',
          slots: { customRender: 'index' },
          width: 70,
          align: 'center',
          fixed: 'left',
        },
        {
          title: '督查对象名称',
          dataIndex: 'nm',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '地市',
          dataIndex: 'adFullName',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '区县',
          dataIndex: 'adName',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '工程位置',
          dataIndex: 'loc',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '经度',
          dataIndex: 'gdX',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '纬度',
          dataIndex: 'gdY',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '工程编码',
          dataIndex: 'projectCode',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '联系人',
          dataIndex: 'contacts',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '联系电话',
          dataIndex: 'telephone',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '备注',
          dataIndex: 'note',
          width: 200,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '对象状态',
          dataIndex: 'dataStatName',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '下发状态',
          dataIndex: 'isStatrtName',
          width: 100,
          align: 'center',

          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '数据来源',
          dataIndex: 'sourceNumName',
          width: 100,
          align: 'center',
          slots: { customRender: 'ellipsisTooltip' },
          customCell: () => {
            return {
              style: {
                maxWidth: '100px',
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
              },
            };
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
          width: 200,
          align: 'center',
        },
      ],
      addObjFlag: false,
      addObjParams: {
        nm: '',
        adCity: undefined,
        adCode: undefined,
        gdX: '',
        gdY: '',
        note: '',
        adName: '',
        projectCode: '',
        loc: '',
        contacts: '',
        telephone: '',
      },
      addObjRules: {
        nm: [
          { required: true, message: '请输入督查对象名称', trigger: 'blur' },
        ],
        adCity: [{ required: true, message: '请选择地市', trigger: 'change' }],
        adCode: [{ required: true, message: '请选择区县', trigger: 'change' }],
      },
      cityList: [],
      countryList: [],
      editTitle: '',
      activities: [],
      isEditRecordShow: false,
      isInterfaceImport: false,
      interfaceImportParams: {
        objUrl: '', // https://database.jxwrd.cn/services/1234567890ABCDEFGHIJKLMN/std_serv/dim_res_base_info/api
        apiKey: '', // 7F3A386BC9273477D3AC8405C7303DADC1859CB782CC80C0F994A3B4EC33D6447F0F94BF67685060E9788D90D64959D35623E2F1B178A01402DB0C9614AD623C9420C7C994CD1E31335C8C64085468E6
        id: '',
      },
      interfaceImportParamsRules: {
        objUrl: [{ required: true, message: '请输入链接', trigger: 'blur' }],
        apiKey: [{ required: true, message: '请输入秘钥', trigger: 'blur' }],
      },
      isAdd: false, // 新增按钮
      isFile: false, // 文件导入按钮
      isSend: false, // 下发按钮
      isJoggle: false, // 接口导入按钮
      isLoading: false,
      isRelation: '',
      isInterfaceInfo: false,
      objUrl: '',
      apiKey: '',
      addObjTitle: '添加',
      readOnlyRole: localStorage.getItem('readOnlyRole'),
      selectedRowKeys: [],
      multipleSelection: [], // 已选
    };
  },
  created() {
    this.getPageList();
    this.rectifyUnits = this.getOrgTreeJson();
    this.getAdData();
  },
  methods: {
    showRecord(row) {
      this.editTitle = row.nm;
      this.isEditRecordShow = true;
      this.activities = [];
      var params = {
        code: row.id,
      };
      request
        .post('/bis/insp/gnrlRgstrAdjust/findDtoList', params)
        .then((res) => {
          if (res.success) {
            if (res.data && res.data.length > 0) {
              var data = res.data;
              var dataList = [];
              data.forEach((item) => {
                var updateContent = item.detail;
                item.auditList.forEach((item1) => {
                  var date = new Date(item1.intm);
                  var timestamp =
                    date.getFullYear() +
                    '年' +
                    (date.getMonth() + 1) +
                    '月' +
                    date.getDate() +
                    '日';
                  dataList.push({
                    title: item1.roleName + '-' + item1.auditPersName,
                    timestamp: timestamp,
                    detail: item1.opStatus + ',' + updateContent,
                  });
                });
                // var date = new Date(item.time);
                // item.timestamp = date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日";
                // if(item.auditWay){
                //     item.title = "业务处室审核";
                //     item.detail = item.auditWay=="1"?("同意,"+item.detail):("驳回,"+item.detail)
                // }else{
                //     item.title = item.adjustWay=="1"?"督查单位提交申请-信息调整":"督查单位提交申请-删除";
                // }
              });
              this.activities = dataList;
            }
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((res) => {
          this.$message.error('请求错误' + res.message);
        });
    },
    getPageList() {
      const { objname, pageNum, pageSize, display } = this.searchParams;
      const params = {
        objname,
        type: 'other',
        display,
        pageNum,
        pageSize,
      };
      getPageList(params)
        .then((res) => {
          if (res.success) {
            this.total = res.data.total;
            this.pageList = res.data.list;
          }
        })
        .catch(() => {
          this.$message.error('请联系管理员!');
        });
    },
    addPageList() {
      this.$refs.addSearchRef.validate((valid) => {
        if (valid) {
          const { objname } = this.addSearchParams;
          const params = {
            objname,
          };
          addPageList(params)
            .then((res) => {
              if (res.success) {
                this.$message.success('添加成功!');
                this.getPageList();
                this.addPageListFlag = false;
                this.addSearchParams.objname = '';
              } else {
                this.$message.error(res.message);
              }
            })
            .catch(() => {
              this.$message.error('请联系管理员!');
            });
        } else {
          return false;
        }
      });
    },
    handleSizeChange(size, val) {
      this.searchParams.pageSize = val;
      this.getPageList();
    },
    handleCurrentChange(val) {
      this.searchParams.pageNum = val;
      this.getPageList();
    },
    displayClick(row) {
      const { id, display } = row;
      const params = {
        id,
        display: display == '0' ? '1' : '0',
      };
      this.$confirm({
        title: '提示',
        content: `确认将该条数据${display == '0' ? '隐藏' : '取消隐藏'}吗？`,
        okType: 'warning',
        onOk: () => {
          displayUpdate(params)
            .then((res) => {
              if (res.success) {
                this.$message.success(
                  `${display == '0' ? '隐藏' : '取消隐藏'}成功!`,
                );
                this.getPageList();
              } else {
                this.$message.error(res.message);
              }
            })
            .catch(() => {
              this.$message.error('请联系管理员!');
            });
        },
      });
    },
    getPblms(row) {
      this.objList = [];
      this.title = row.objname;
      this.countNumRelevancy = row.objNum;
      this.objectListFlag = true;
      this.isSend = 'other' === row.type;
      this.isRelation = row.relation;
      this.getRelation(row.relation);
      this.objListParams.pageNum = 1;
      this.objListParams.nm = '';
      this.objListParams.isStatrt = '';
      this.objListParams.adCode = undefined;
      this.getObjList();
      this.getByObjNum();
    },
    getObjList() {
      const {
        objListParams: { nm, isStatrt, adCode, pageNum, pageSize },
        countNumRelevancy,
      } = this;
      const params = {
        nm,
        isStatrt,
        adCode,
        countNumRelevancy,
        pageNum,
        pageSize,
      };
      getObjList(params)
        .then((res) => {
          if (res.success) {
            this.objList = res.data.list;
            this.objTotal = res.data.total;
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {
          this.$message.error('请联系管理员!');
        });
    },
    closeObj() {
      this.getPageList();
      this.objectListFlag = false;
      this.isSend = false;
      // 清除选中的数据
      this.multipleSelection = [];
      this.selectedRowKeys = [];
    },
    handleSizeChangeObj(size, val) {
      this.objListParams.pageSize = val;
      this.getObjList();
    },
    handleCurrentChangeObj(val) {
      this.objListParams.pageNum = val;
      this.getObjList();
    },
    addObjList() {
      this.$refs.addObjRef.validate((valid) => {
        if (valid) {
          const {
            addObjParams: {
              nm,
              adCode,
              gdX,
              gdY,
              note,
              adName,
              loc,
              projectCode,
              telephone,
              contacts,
              id,
            },
            countNumRelevancy,
          } = this;
          const params = {
            nm,
            adCode,
            gdX,
            gdY,
            note,
            countNumRelevancy,
            adName,
            loc,
            projectCode,
            telephone,
            contacts,
            id,
          };

          if (this.addObjTitle == '修改') {
            update2ObjList(params)
              .then((res) => {
                if (res.success) {
                  this.$message.success('修改成功!');
                  this.getObjList();
                  this.getByObjNum();
                  this.getStatus();
                  this.addObjFlag = false;
                  this.addObjParams.nm = '';
                  this.addObjParams.adCity = '';
                  this.addObjParams.adCode = '';
                  this.addObjParams.loc = '';
                  this.addObjParams.gdX = '';
                  this.addObjParams.gdY = '';
                  this.addObjParams.note = '';
                  this.addObjParams.projectCode = '';
                  this.addObjParams.telephone = '';
                  this.addObjParams.contacts = '';
                } else {
                  this.$message.error(res.message);
                }
              })
              .catch(() => {
                this.$message.error('请联系管理员!');
              });
          } else {
            addObjList(params)
              .then((res) => {
                if (res.success) {
                  this.$message.success('添加成功!');
                  this.getObjList();
                  this.getByObjNum();
                  this.getStatus();
                  this.addObjFlag = false;
                  this.addObjParams.nm = '';
                  this.addObjParams.adCity = '';
                  this.addObjParams.adCode = '';
                  this.addObjParams.loc = '';
                  this.addObjParams.gdX = '';
                  this.addObjParams.gdY = '';
                  this.addObjParams.note = '';
                  this.addObjParams.projectCode = '';
                  this.addObjParams.telephone = '';
                  this.addObjParams.contacts = '';

                  // this.$refs.city.setCheck('', '')
                  // this.$refs.country.setCheck('', '')
                } else {
                  this.$message.error(res.message);
                }
              })
              .catch(() => {
                this.$message.error('请联系管理员!');
              });
          }
        } else {
          return false;
        }
      });
    },
    getOrgTreeJson() {
      getOrgTreeJson().then((res) => {
        const jsonArray = [];
        const jsonStr = JSON.stringify(res.data);
        jsonArray.push(res.data);
        this.rectifyUnits = jsonArray;
      });
    },
    getAdData() {
      request
        .get('/dc/ad/base/getAdData', { params: { adCode: '360000000000' } })
        .then((res) => {
          if (res.success) {
            const strLength = localStorage
              .getItem('currentAdCode')
              .replace(/00/g, '').length;
            if (strLength === 2) {
              this.cityList = res.data;
            }
            if (strLength === 4) {
              res.data.forEach((item) => {
                if (item.adCode === localStorage.getItem('currentAdCode')) {
                  this.cityList = [
                    { adName: item.adName, adCode: item.adCode },
                  ];
                }
              });
            }
            if (strLength === 6) {
              res.data.forEach((item) => {
                if (
                  item.adCode ===
                  localStorage.getItem('currentAdCode').substring(0, 4) +
                    '00000000'
                ) {
                  this.cityList = [
                    { adName: item.adName, adCode: item.adCode },
                  ];
                }
              });
            }
          }
        });
    },
    cityChange(value, option) {
      this.countryList = [];
      this.addObjParams.adCode = '';
      request
        .get('/dc/ad/base/getAdData', { params: { adCode: value } })
        .then((res) => {
          if (res.success) {
            const strLength = localStorage
              .getItem('currentAdCode')
              .replace(/00/g, '').length;
            if (strLength === 6) {
              res.data.forEach((item) => {
                if (item.adCode === localStorage.getItem('currentAdCode')) {
                  this.countryList = [
                    { adName: item.adName, adCode: item.adCode },
                  ];
                }
              });
            } else {
              this.countryList = res.data;
            }
          }
        });
    },
    countryName(value, option) {
      this.countryList.forEach((item) => {
        if (value == item.adCode) {
          this.addObjParams.adName = item.adName;
          // this.addObjParams.loc = item.adFullName
        }
      });
    },
    handleChange(info) {
      if (info.file.status === 'done') {
        if (info.file.response.success) {
          this.$message.success(`${info.file.name} 导入成功!`);
          this.getObjList();
          this.getByObjNum();
          this.getStatus();
        } else {
          this.$message.error(info.file.response.message);
        }
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} 导入失败!`);
      }
    },
    deletePblms(text) {
      this.$confirm({
        title: '提示',
        content: '此操作将永久删除，确认删除该条数据吗？',
        okType: 'warning',
        onOk: () => {
          deletePblms(text.id)
            .then((res) => {
              if (res.success) {
                this.$message.success('删除成功!');
                this.getPageList();
              } else {
                this.$message.error(res.message);
              }
            })
            .catch(() => {
              this.$message.error('删除失败,请联系管理员!');
            });
        },
      });
    },
    showInterfaceImport() {
      this.isInterfaceImport = true;
    },
    getRelation(relation) {
      if (relation == '0') {
        this.isAdd = false;
        this.isFile = false;
        this.isJoggle = false;
      } else if (relation == '1') {
        this.isAdd = false;
        this.isFile = false;
        this.isJoggle = true;
      } else if (relation == '2') {
        this.isAdd = true;
        this.isFile = true;
        this.isJoggle = false;
      } else {
        this.isAdd = true;
        this.isFile = true;
        this.isJoggle = true;
      }
    },
    getByObjNum() {
      const { countNumRelevancy } = this;
      getByObjNum(countNumRelevancy).then((res) => {
        this.interfaceImportParams.objUrl = res.data ? res.data.objUrl : '';
        this.interfaceImportParams.apiKey = res.data ? res.data.apiKey : '';
        this.interfaceImportParams.id = res.data ? res.data.id : '';
        this.objUrl = res.data ? res.data.objUrl : '';
        this.apiKey = res.data ? res.data.apiKey : '';
      });
    },
    getStatus() {
      const { countNumRelevancy } = this;
      getStatus(countNumRelevancy).then((res) => {
        this.isRelation = res.data.relation;
        this.getRelation(res.data.relation);
      });
    },
    getInterfaceImport() {
      this.$refs.interfaceImportParamsRef.validate((valid) => {
        if (valid) {
          const params = {
            dictNum: this.countNumRelevancy,
            objUrl: this.interfaceImportParams.objUrl,
            apiKey: this.interfaceImportParams.apiKey,
            id: this.interfaceImportParams.id,
          };
          this.isLoading = true;
          if (this.isRelation == '1') {
            importByUrlUpdate(params).then((res) => {
              if (res.success) {
                this.$message.warning('导入成功,正在解析中...');
                const data = {
                  dictNum: res.data.dictNum,
                  objUrl: res.data.objUrl,
                  apiKey: res.data.apiKey,
                };
                this.getDataAnalysis(data);
              } else {
                this.isLoading = false;
                this.$message.error(res.message);
              }
            });
          } else {
            importByUrlAdd(params).then((res) => {
              if (res.success) {
                this.$message.warning('导入成功,正在解析中...');
                const data = {
                  dictNum: res.data.dictNum,
                  objUrl: res.data.objUrl,
                  apiKey: res.data.apiKey,
                };
                this.getDataAnalysis(data);
              } else {
                this.isLoading = false;
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 解析数据
    getDataAnalysis(data) {
      getDataAnalysis(data).then((res) => {
        if (res.success) {
          this.$message.success('解析成功!');
          this.isLoading = false;
          this.isInterfaceImport = false;
          this.getObjList();
          this.getByObjNum();
          this.getStatus();
        } else {
          this.$message.error('解析失败!');
          this.isLoading = false;
          this.isInterfaceImport = false;
          this.getObjList();
          this.getByObjNum();
          this.getStatus();
        }
      });
    },
    showAddObjList() {
      this.addObjTitle = '添加';
      this.addObjFlag = true;
      this.addObjParams.nm = '';
      this.addObjParams.adCity = '';
      this.addObjParams.adCode = '';
      this.addObjParams.loc = '';
      this.addObjParams.gdX = '';
      this.addObjParams.gdY = '';
      this.addObjParams.note = '';
      this.addObjParams.projectCode = '';
      this.addObjParams.telephone = '';
      this.addObjParams.contacts = '';
      this.addObjParams.id = '';
      this.addObjParams.adName = '';
    },
    // 修改督察对象列表信息
    showEditObjList(text) {
      this.addObjTitle = '修改';
      this.addObjParams.adCode = text.adCode;
      this.addObjParams.adName = text.adName;
      this.addObjParams.nm = text.nm;
      this.addObjParams.loc = text.loc;
      this.addObjParams.gdX = text.gdX;
      this.addObjParams.gdY = text.gdY;
      this.addObjParams.projectCode = text.projectCode;
      this.addObjParams.contacts = text.contacts;
      this.addObjParams.telephone = text.telephone;
      this.addObjParams.note = text.note;
      this.addObjParams.id = text.id;
      this.addObjFlag = true;
    },
    // 单个督查对象下发
    sendObj(text) {
      const idArr = [];
      idArr.push(text.id);
      const params = {
        countNumRelevancy: this.countNumRelevancy,
        idList: idArr,
      };
      sendObjList(params)
        .then((res) => {
          if (res.success) {
            this.$message.success('下发成功!');
            this.getObjList();
            this.getByObjNum();
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(() => {
          this.$message.error('请联系管理员!');
        });
    },
    // 已选择项
    handleSelectionChange(val, row) {
      this.selectedRowKeys = val;
      this.multipleSelection = row;
    },
    // 多个督查对象批量下发
    sendObjBatch() {
      if (this.selectedRowKeys.length > 0) {
        const idArr = [];
        for (var i = 0; i < this.multipleSelection.length; i++) {
          const isStatrt = this.multipleSelection[i].isStatrt;
          if ('0' === isStatrt) {
            idArr.push(this.multipleSelection[i].id);
          }
        }
        if (idArr.length > 0) {
          const params = {
            countNumRelevancy: this.countNumRelevancy,
            idList: idArr,
          };
          this.isLoading = true;
          sendObjList(params)
            .then((res) => {
              if (res.success) {
                this.isLoading = false;
                this.$message.success('下发成功!');
                // 重要：清空之前的选择
                this.multipleSelection = [];
                // 清除选中的数据
                this.selectedRowKeys = [];
                this.getObjList();
                this.getByObjNum();
              } else {
                this.$message.error(res.message);
              }
            })
            .catch(() => {
              this.$message.error('请联系管理员!');
            });
        } else {
          this.$message.warning('请选择需要的未下发对象!');
        }
      } else {
        this.$message.warning('请选择需要下发的对象!');
      }
    },
  },
};
</script>

<style lang='less' scoped>
:deep(.ant-layout-sider),
:deep(.ant-layout-header),
:deep(.ant-layout-content) {
  background: #fff;
}
</style>
