<template>
  <div class="main-content market m20">
    <el-tabs v-if="isAreaPresident" v-model="type" class="tab-card" type="border-card" @tab-click="loadPage()">
      <el-tab-pane label="区公共片区" name="0" />
      <el-tab-pane label="市公共片区" name="1" />
    </el-tabs>
    <el-tabs v-if="isSuoPresident" v-model="type" class="tab-card" type="border-card" @tab-click="loadPage()">
      <el-tab-pane label="已认领业户" name="0" @click="loadPage()" />
      <el-tab-pane label="区公共片区" name="1" />
    </el-tabs>
    <div class="search-box">
      <el-row class="mb10" :gutter="20">
        <el-col :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">企业名称:</label>
          <div class="search-input-box" style="display: inline-block;">
            <el-input v-model="searchForm.entName" placeholder="请输入企业名称" clearable @keyup.native.enter="search(1)" />
          </div>
        </el-col>
        <el-col :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">注册码/统一社会信用代码:</label>
          <div class="search-input-box" style="display: inline-block;">
            <el-input
              v-model="searchForm.uniscid"
              placeholder="请输入注册码/统一社会信用代码"
              clearable
              @keyup.native.enter="search(1)"
            />
          </div>
        </el-col>

      </el-row>
      <el-row :gutter="20" style="margin:10px 0">
        <el-col :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">法定代表人/负责人:</label>
          <div class="search-input-box" style="display: inline-block;">
            <el-input v-model="searchForm.name" placeholder="请输入企业名称" clearable @keyup.native.enter="search(1)" />
          </div>
        </el-col>
        <el-col :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">登记机关:</label>
          <div class="search-input-box" style="display: inline-block;">
            <el-select v-model="searchForm.regOrg" placeholder="请选择登记机关" style="width:100%" clearable>
              <el-option v-for="each in regOrgOptions" :key="each.id" :label="each.name" :value="each.code" />
            </el-select>
          </div>
        </el-col>
      </el-row>
      <el-row class="mb10" :gutter="20">
        <el-col :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">主体类型:</label>
          <div class="search-input-box" style="display: inline-block;">
            <el-select v-model="searchForm.entTypeCn" placeholder="请选择主体类型" style="width:100%" clearable>
              <el-option v-for="each in entTypeOptions" :key="'searchForm1Enttype'+each.value" :label="each.label" :value="each.value" />
            </el-select>
          </div>
        </el-col>
        <el-col :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">经营地址:</label>
          <div class="search-input-box" style="display: inline-block;">
            <el-input v-model="searchForm.address" placeholder="请输入经营地址" clearable @keyup.native.enter="search(1)" />
          </div>
        </el-col>
      </el-row>
      <el-row class="mb10" :gutter="20">
        <el-col v-if="isSuoPresident&&type==0" :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">所在片区:</label>
          <div class="search-input-box" style="display: inline-block;">
            <el-select v-model="searchForm.sliceId" placeholder="请选择所在片区" style="width:100%" clearable filterable>
              <el-option v-for="each in areaSliceOptions" :key="'searchForm2AreaSlice'+each.sliceId" :value="each.sliceId" :label="each.sliceName" />
            </el-select>
          </div>
        </el-col>
        <!--        <el-col :span="12" class="display-flex">
          <label class="label-name" style="display: inline-block;">是否虚假地址:</label>
          <el-radio-group v-model="searchForm.isDraftRevoke" size="mini">
            <el-radio label="1" border>是</el-radio>
          </el-radio-group>
        </el-col>-->
      </el-row>
      <el-row :gutter="20">
        <el-col :span="5">
          <div class="btn-box">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="search()">查询</el-button>
            <el-button type="info" icon="el-icon-refresh-left" size="mini" @click="resetSearchForm()">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="main-body ">
      <div class="content">
        <el-row style="margin-bottom: 10px">
          <el-col :offset="0">
            <el-button v-if="isShowAssignBtn&&type==0" type="primary" icon="el-icon-search" size="mini" @click="batchAssign()">批量指派</el-button>
            <el-button v-if="isShowRemoveBtn&&type==0" type="primary" icon="el-icon-search" size="mini" @click="openDialog3()">批量退还</el-button>
            <el-button v-if="isSuoPresident&&type==0" type="primary" icon="el-icon-search" size="mini" @click="openDialog4()">批量调整</el-button>
            <el-button v-if="type==1" type="primary" icon="el-icon-search" size="mini" @click="batchClaim()">批量认领</el-button>
            <el-button v-if="isShowAddOrFileAssign" type="primary" icon="el-icon-search" size="mini" @click="fileImportDialog = true">导入认领名单</el-button>
            <el-button v-if="isShowAddOrFileAssign" type="primary" icon="el-icon-search" size="mini" @click="addressAssignDialog = true">设置识别地址</el-button>
            <el-button v-show="tableData.length > 0" type="primary" icon="el-icon-search" size="mini" @click="exportFile()">导出</el-button>
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="tableData"
          class="table-style"
          :row-class-name="tableRowClassName"
          :header-cell-style="{background:'#8cc3fb',color:'#fff'}"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" align="center" />
          <el-table-column
            prop="index"
            label="序号"
            min-width="50"
          />
          <el-table-column
            prop="uniscid"
            label="统一社会信用代码/注册号"
            min-width="100"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <router-link
                style="color: #409EFF"
                :to="{path:'/comprehensive/details',query:{ pripid: scope.row.pripid }}"
              >
                {{ scope.row.uniscid }}
              </router-link>
            </template>
          </el-table-column>
          <el-table-column
            prop="entName"
            label="主体名称"
            min-width="100"
            show-overflow-tooltip
          />
          <el-table-column
            prop="lerep"
            label="法定代表人/负责人"
            min-width="80"
            show-overflow-tooltip
          />
          <el-table-column
            prop="entType"
            label="主体类型"
            min-width="80"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{ scope.row.entType }} &nbsp;
              <el-tag v-show="scope.row.isDraftTab === '1'" type="danger">虚假地址</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="address"
            label="经营地址"
            min-width="150"
            show-overflow-tooltip
          />
          <el-table-column
            prop="sliceName"
            label="所在片区"
            min-width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="操作"
            min-width="100"
          >
            <template slot-scope="scope">
              <!--              <el-link v-show="isShowAssignBtn && scope.row.isDraftTab !== '1'" type="primary" @click="draftRevoke(scope.row)">虚假地址标记</el-link> &nbsp; &nbsp;-->
              <el-link v-if="isShowAssignBtn&&type==0" type="primary" @click="openAssignDialog(scope.row)">指派</el-link> &nbsp; &nbsp;
              <el-link v-if="isSuoPresident&&type==0" type="primary" @click="changeAssign(scope.row)">调整</el-link> &nbsp; &nbsp;
              <el-link v-if="isShowRemoveBtn&&type==0" type="primary" @click="openDialog2(scope.row)">退还</el-link> &nbsp; &nbsp;
              <el-link v-if="type==1" type="primary" @click="claim(scope.row)">认领</el-link> &nbsp; &nbsp;
              <el-link
                v-if="scope.row.removeAssignId"
                type="primary"
                @click="openRemoveAssignDialog(scope.row)"
              >退还原因
              </el-link>
            </template>
          </el-table-column>
        </el-table>

        <div class="page-box">
          <el-row>

            <el-col :span="24" style="text-align:right">
              <el-pagination
                :disabled="loading"
                :page-size="pageParam.pageSize"
                :total="pageParam.total"
                :page-sizes="[10, 20, 30, 40,50,100,200]"
                layout="slot,total, sizes, prev, pager, next, jumper"
                @size-change="pageSizeChange"
                @current-change="pageIndexChange"
              >
                <span>
                  第 {{ pageParam.curPage }} 页 /
                  共 {{ (parseInt((pageParam.total + pageParam.pageSize - 1) / pageParam.pageSize)) }} 页
                </span>
              </el-pagination>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>

    <el-dialog
      title="输入地址"
      :visible.sync="newAddressDialog"
      width="50%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <div class="search-field">
        <el-row style="margin-bottom:20px">
          <span style="color:red">*</span><label style="text-align: left; display: inline-block;">地址输入方式：</label>
          <el-radio v-model="addressAssignType" label="1" @change="">直接输入</el-radio>
          <el-radio v-model="addressAssignType" label="2" @change="">区分单双数门牌号</el-radio>
        </el-row>
        <el-row style="margin-bottom:20px">
          <el-col :span="4">
            <span style="color:red">*</span><label style="text-align: left; display: inline-block;">请输入识别地址：</label>
          </el-col>
          <el-col :span="20">
            <div v-if="addressAssignType === '1' " style="text-align: left;">
              <el-input v-model="address" style="width: 50%;" clearable size="mini" class="search-input" type="text" placeholder="请输入地址如xxx市xxx区xx路/街xxx号xxx房/室" />
            </div>
            <div v-if="addressAssignType === '2' " style="text-align: left;">
              <el-input @change="address2=addressS+addressQ+addressJ+'-'+addressH" style="width: 15%;text-align: left;" v-model="addressS" clearable size="mini" type="text" placeholder="xx市" />
              <el-select @change="address2=addressS+addressQ+addressJ+'-'+addressH" style="width: 15%;text-align: left;" v-model="addressQ" clearable size="mini" placeholder="请选择">
                <el-option
                  v-for="item in regionOptions"
                  :key="item.label"
                  :label="item.label"
                  :value="item.label"
                />
              </el-select>
              <el-input @change="address2=addressS+addressQ+addressJ+'-'+addressH" style="width: 50%;text-align: left;" v-model="addressJ" clearable size="mini" type="text" placeholder="xxx路/街" />
              <el-select @change="address2=addressS+addressQ+addressJ+'-'+addressH" style="width: 15%;text-align: left;" v-model="addressH" clearable size="mini" placeholder="请选择">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.label"
                  :label="item.label"
                  :value="item.label"
                />
              </el-select>
            </div>
          </el-col>
        </el-row>
        <el-row style="margin-bottom:20px">
          <label style="text-align: left; display: inline-block;">识别地址详情：</label>
          <label v-if="addressAssignType === '1'" style="text-align: left; display: inline-block;">{{ address }}</label>
          <label v-if="addressAssignType === '2'" style="text-align: left; display: inline-block;">{{ address2 }}</label>
        </el-row>
        <el-row v-if="isShowAssignBtn" style="margin-bottom:20px">
          <span style="color:red">*</span><label style="text-align: left; display: inline-block;">所属监管所：</label>
          <el-select v-model="addressAssignOrg" filterable style="width: 50%;">
            <el-option
              v-for="each in assignSliceOptions"
              :key="'areaSliceOptions1'+each.sliceId"
              :value="each.orgNumber"
              :label="each.sliceName"
            />
          </el-select>
        </el-row>
        <el-row style="margin-bottom:20px">
          <div style="margin-top:20px; width:80%; text-align:center;">
            <span style="color:red;margin-right: 10px;">提示：新增识别地址后，如果地图认领定位不到，系统自动将该识别地址的主体认领到该监管所。</span>
          </div>
        </el-row>
        <el-row style="margin-bottom:20px">
          <el-col :span="24" style="text-align: center;">
            <el-button
              size="mini"
              style="margin-left: 10px;"
              @click=""
            >取消</el-button>
            <el-button
              :loading="addressLoading"
              size="mini"
              style="margin-left: 10px;"
              type="primary"
              icon="el-icon-search"
              @click="addAddress"
            >新增</el-button>
          </el-col>
        </el-row>
      </div>
    </el-dialog>

    <el-dialog
      title="识别地址信息"
      :visible.sync="addressAssignDialog"
      width="90%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-row style="margin-bottom:20px">
        <el-col :span="7">
          <label style="text-align: left; display: inline-block;">识别地址(模糊)：</label>
          <div style="display: inline-block;width: calc(100% - 220px);">
            <el-input v-model="pageParamAddress.paramMap.address" clearable size="mini" class="search-input" type="text" placeholder="请输入识别地址" />
          </div>
        </el-col>
        <el-col :span="7">
          <label style="text-align: left; display: inline-block;">处理人：</label>
          <div style="display: inline-block;width: calc(100% - 220px);">
            <el-input v-model="pageParamAddress.paramMap.operator" clearable size="mini" class="search-input" type="text" placeholder="请输入处理人" />
          </div>
        </el-col>
        <el-col :span="10">
          <div>
            <label style="text-align: left; display: inline-block;">处理时间：</label>
            <el-date-picker
              v-model="searchDate"
              size="mini"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width:80%"
            />
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" style="text-align: right;">
          <el-button
            size="mini"
            style="margin-left: 10px;"
            type="primary"
            icon="el-icon-search"
            @click="getAddressList"
          >查询</el-button>
          <el-button
            size="mini"
            style="margin-left: 10px;"
            @click="pageParamAddress.paramMap.address='';pageParamAddress.paramMap.operator='';searchDate=''"
          >重置</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20" style="text-align: left">
          <el-button size="mini" type="primary" @click="newAddressDialog=true">新增识别地址</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="addressTableData"
        :header-cell-style="{background:'#8cc3fb',color:'#fff'}"
        tooltip-effect="dark"
        style="width: 100%;padding: 0;margin:10px auto"
        :fit="true"
        @selection-change=""
      >
        <el-table-column class="no" type="selection" min-width="30" align="center" />
        <el-table-column prop="index" label="序号" min-width="40" align="center" />
        <el-table-column prop="address" label="识别地址" min-width="200" align="left" />
        <el-table-column prop="orgNumber" :show-overflow-tooltip="true" label="所属监管所" min-width="200" align="left" />
        <el-table-column prop="operator" :show-overflow-tooltip="true" label="处理人" min-width="80" align="left" />
        <el-table-column prop="insertTime" :show-overflow-tooltip="true" label="处理时间" min-width="100" align="left" />
        <el-table-column prop="" :show-overflow-tooltip="true" label="操作" min-width="50" align="left" >
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="delAddress(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-row class="bl0">
          <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
            <el-pagination
              :page-size="pageParamAddress.pageSize"
              :total="pageParamAddress.total"
              :page-sizes="[10, 20, 30, 40,50,100,200]"
              layout="slot,total, sizes, prev, pager, next, jumper"
              @size-change="pageParamAddressSize"
              @current-change="pageParamAddressIndex"
            >
              <span>
                第 {{ pageParamAddress.curPage }} 页 /
                共 {{ (pageParamAddress.total !== 0 ? parseInt((pageParamAddress.total + pageParamAddress.pageSize - 1)/pageParamAddress.pageSize) : 1) }} 页
              </span>
            </el-pagination>
          </el-col>
        </el-row>
      </span>
    </el-dialog>

    <el-dialog
      title="导入认领名单"
      :visible.sync="fileImportDialog"
      width="30%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <div>
        <el-input
          v-model="batchFileName"
          placeholder="点击选择模版文件"
          style="display:inline-block;width:calc(100% - 120px)"
          :readonly="true"
          @click.native="$refs.batchFile.click()"
        />
        <el-button style="width:100px;margin-left:10px" type="primary" @click="$refs.batchFile.click()">浏览</el-button>
        <input :ref="'batchFile'" type="file" style="display:none" accept=".xls,.xlsx" @change="fileChange">
      </div>

      <FakeProgress v-if="showFakeProgress" ref="FakeProgress" style="margin-top: 10px;" />
      <span slot="footer" class="dialog-footer">
        <el-row>
          <el-col :span="8" align="left">
            <el-button type="primary" @click="downloadFile">下载模版</el-button>
          </el-col>
          <el-col :span="16">
            <el-button :loading="loading" @click="fileImportDialog = false">取 消</el-button>
            <el-button type="primary" :loading="loading" @click="assignByFile">新 增</el-button>
          </el-col>
        </el-row>
      </span>
    </el-dialog>

    <el-dialog
      title="地址发起校验结果"
      :visible.sync="fileResultDialog"
      width="20%"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <div style="margin-bottom:20px; display:inline-block; width:80%">
        <span style="font-size: 20px;">可发起数量：{{claimNum}}</span>
      </div>
      <div style="margin-bottom:20px; display:inline-block; width:80%">
        <span style="font-size: 20px;">不可发起数量：{{noClaimNum}}</span>
        <el-link
          style="margin-left:20px; font-size: medium"
          type="primary"
          @click="exportNoClaimExcel"
        >下载名单</el-link>
      </div>
      <div style="">
        <el-button :loading="loading" @click="fileResultDialog = false">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="fileResultDialog = false">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="业户指派" :visible.sync="dialogVisible1" width="30%">
      <el-form ref="submitForm1" :model="submitForm1" label-width="100px" label-suffix="：" :rules="submitForm1Rules">
        <el-form-item label="指派片区" prop="sliceId">
          <el-tooltip placement="top">
            <div slot="content">{{submitForm1.sliceName}}</div>
            <el-select v-model="submitForm1.sliceName" filterable style="width: 100%" @change="setSliceName1">
              <el-option
                v-for="each in assignSliceOptions"
                :key="'areaSliceOptions1'+each.sliceId"
                :value="each"
                :label="each.sliceName"
              />
            </el-select>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="submitAssignForm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="批量业户指派" :visible.sync="BatchAssignWindows" width="30%">
      <el-form ref="submitForm4" :model="submitForm4" label-width="100px" label-suffix="：" :rules="submitForm1Rules">
        <el-form-item label="指派片区" prop="sliceId">
          <el-tooltip placement="top">
            <div slot="content">{{submitForm4.sliceName}}</div>
            <el-select v-model="submitForm4.sliceName" filterable style="width: 100%" @change="setSliceName4">
              <el-option
                v-for="each in assignSliceOptions"
                :key="'areaSliceOptions1'+each.sliceId"
                :value="each"
                :label="each.sliceName"
              />
            </el-select>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="BatchAssignWindows = false">取 消</el-button>
        <el-button type="primary" @click="BatchsubmitAssignForm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="退还信息" :visible.sync="dialogVisible1_1" width="30%">
      <el-form label-suffix="：">
        <el-form-item label="退还机关">{{ removeAssignInfo.RemoveOrgName }}</el-form-item>
        <el-form-item label="退还原因">{{ removeAssignInfo.remReason }}</el-form-item>
        <el-form-item label="联系人">{{ removeAssignInfo.remOperatorName }}</el-form-item>
        <el-form-item label="联系电话">{{ removeAssignInfo.phone }}</el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="退还原因" :visible.sync="dialogVisible2" width="30%">
      <el-form ref="submitForm2" :model="submitForm2" label-width="100px" label-suffix="：">
        <el-form-item label="退还原因">
          <el-input v-model="submitForm2.remReason" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="submitForm2.phone" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="removeAssign()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="批量退还原因" :visible.sync="BatchRemoveOwner" width="30%">
      <el-form ref="submitForm2" :model="submitForm2" label-width="100px" label-suffix="：">
        <el-form-item label="退还原因">
          <el-input v-model="submitForm2.remReason" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="submitForm2.phone" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="BatchRemoveOwner = false">取 消</el-button>
        <el-button type="primary" @click="BatchremoveOwner()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="片区调整" :visible.sync="dialogVisible3" width="30%">
      <el-form ref="submitForm3" :model="submitForm3" label-width="100px" label-suffix="：" :rules="submitForm1Rules">
        <el-form-item label="调整片区" prop="sliceId">
          <el-tooltip placement="top">
            <div slot="content">{{submitForm3.sliceName}}</div>
            <el-select v-model="submitForm3.sliceName" filterable  @change="setSliceName3">
              <el-option
                v-for="each in changeSliceOptions"
                :key="'areaSliceOptions3'+each.sliceId"
                :value="each"
                :label="each.sliceName"
              />
            </el-select>
          </el-tooltip>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="submitChangeForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { areaSliceAllList, getChangeableSlice, addAddress, getAddressList, delAddress } from '@/api/areaSlice'
import FakeProgress from '@/components/FakeProgress'
import { exportNoClaimExcel, assignByFile, assignOwner, assignOwner1, BatchAssignOwner, BatchAssignOwner1, changeOwner, changeOwnerBatch, entPage, exportEntOwner, getAssignableSliceList, getAssignInfo, removeOwner, BatchRemoveOwner, inDraftRevoke } from '@/api/marketAssign'
import { getDataType } from '@/api/constant'
import { listByPripids } from '../../../api/company'

export default {
  components: {
    FakeProgress
  },
  data() {
    return {
      isBatchAdjust: false,
      searchDate: '',
      addressTableData: [],
      pageParamAddress: {
        curPage: 1,
        pageSize: 10,
        paramMap: {
          address: '',
          operator: ''
        },
        total: 0
      },
      addressLoading: false,
      addressAssignOrg: '',
      address2: '',
      address: '',
      addressS: '',
      addressQ: '',
      addressJ: '',
      addressH: '',
      typeOptions: [{
        value: '1',
        label: '单数号'
      },{
        value: '2',
        label: '双数号'
      }],
      regionOptions: [{
        label: '天河区'
      }, {
        label: '黄埔区'
      }, {
        label: '增城区'
      }, {
        label: '越秀区'
      }, {
        label: '荔湾区'
      }, {
        label: '番禺区'
      }, {
        label: '南沙区'
      }, {
        label: '白云区'
      }, {
        label: '花都区'
      }, {
        label: '从化区'
      }, {
        label: '海珠区'
      }],
      addressAssignType: '1',
      newAddressDialog: false,
      searchForm2: {
        address: '',
        operator: ''
      },
      pageParam2: {
        customParamMap: {},
        curPage: 1,
        total: 0,
        pageSize: 10
      },
      addressAssignDialog: false,
      noClaimList: [],
      claimNum: 0,
      noClaimNum: 0,
      fileResultDialog: false,
      batchFileName: '',
      fileImportDialog: false,
      showFakeProgress: false,
      tooltipsName:'',
      entName: '',
      user: JSON.parse(sessionStorage.getItem('user')),
      tableData: [],
      dialogVisible1: false,
      dialogVisible1_1: false,
      dialogVisible2: false,
      dialogVisible3: false,
      BatchRemoveOwner: false,
      BatchAssignWindows: false,
      regOrgOptions: [],
      type: 0,
      searchForm: {
        regOrg: ''
      },
      entTypeOptions: [
        { value: '企业', label: '企业' },
        { value: '个体户', label: '个体户' },
        { value: '农合', label: '农合' }
      ],
      pageParam: {
        curPage: 1,
        pageSize: 10,
        paramMap: {
          isAssign: 0
        },
        total: 0
      },
      loading: false,
      submitForm1: {
        pripid: '',
        sliceId: '',
        sliceName: ''
      },
      removeAssignInfo: {
        assignId: '',
        pripid: '',
        remReason: '',
        phone: '',
        RemoveOrgName: ''
      },
      submitForm2: {
        assignId: 0,
        pripid: '',
        remReason: '',
        phone: ''
      },
      submitForm3: {
        assignId: '',
        sliceId: '',
        sliceName: ''
      },
      submitForm4: {
        sliceId: '',
        pripids: [],
        sliceName: ''
      },
      submitForm5: [],
      submitForm1Rules: {
        sliceId: [
          { required: true, message: '请选择片区', trigger: 'blur' }
        ]
      },
      areaSliceOptions: [],
      assignSliceOptions: [],
      changeSliceOptions: [],
      assignType: '', // 可认领类型,
      assignEntOption: [],
      removeSclice: ''
    }
  },
  computed: {
    isShowAddOrFileAssign(){
      return this.user.orgCode !== '440101'
    },
    isShowsliceId() {
      return this.user.orgCode.length === 8 && this.type === 0
    },
    // 是否展示认领、虚假地址按钮
    isShowAssignBtn() {
      return this.user.orgCode.length === 6
    },
    isSuoPresident() {
      this.removeSclice = '区公共片区'
      return this.user.orgCode.length === 8
    },
    isAreaPresident() {
      this.removeSclice = '市公共片区'
      return this.user.orgCode !== '440101' && this.user.orgCode.length === 6
    },
    // 是否展示退还按钮
    isShowRemoveBtn() {
      return this.user.orgCode !== '440101'
    }
  },
  mounted() {
    if(this.user.orgCode.length === 6 && this.user.orgCode != '440101'){
      getAssignableSliceList('1').then(res => {
        if (res.code === 0 && res.data && res.data.length) {
          this.assignSliceOptions = res.data
        } else {
          this.$message.info('加载监管所失败')
        }
      })
    }
    this.removeSclice = this.user.orgCode.length === 8 ? '区公共片区' :  (this.user.orgCode !== '440101' && this.user.orgCode.length === 6 ? '市公共片区' : '')
    this.assignType = this.user.orgCode === '440101' ? '0' : '1'
    // 加载登记机关
    getDataType('registration').then(res => {
      if (res.code === 0) {
        this.regOrgOptions = res.data
      }
    })
    this.loadPage()
    // 加载片区
    areaSliceAllList().then(res => {
      if (res.code === 0) {
        this.areaSliceOptions = res.data
        const sliceIds = []
        this.areaSliceOptions.forEach(each => {
          sliceIds.push(each.sliceId)
        })
      }
    })
  },
  methods: {
    delAddress(id){
      delAddress({assignId:id}).then(res => {
        if(res.code == 0){
          this.$message.success('操作成功')
          this.getAddressList()
        }
      })
    },
    pageParamAddressSize(pageSize){
      this.pageParamAddress.pageSize = pageSize
      this.getAddressList(1)
    },
    pageParamAddressIndex(curPage){
      this.pageParamAddress.curPage = curPage
      this.getAddressList(1)
    },
    getAddressList(){
      const pageParam = this['pageParamAddress']
      pageParam.paramMap.searchDate = this.searchDate
      this['loading'] = true
      getAddressList(pageParam).then(res => {
        this['loading'] = false
        if(res.code == 0){
          let list = res.data.records
          this.pageParamAddress.total = res.data.total
          let idx = (pageParam.curPage - 1) * pageParam.pageSize
          for (const i in list) {
            const each = list[i]
            each.index = ++idx
          }
          this.addressTableData = list
        }
      })
    },
    addAddress(){
      this.addressLoading = true
      if(this.addressAssignType == "2" && this.addressH == ''){
        this.$message.warning('区分单双号时，请选择单双号')
        this.addressLoading = false
        return
      }
      if(this.user.orgCode.length == 6 && this.addressAssignOrg == ''){
        this.$message.warning('区级账号需要指定所属监管所')
        this.addressLoading = false
        return
      }
      let param = {
        address : this.addressAssignType == "1" ? this.address : (this.addressAssignType == "2" ? this.address2 : ''),
        org_number : this.user.orgCode.length == 6 ? this.addressAssignOrg : this.user.orgCode,
        type : this.addressAssignType == "1" ? '0' : (this.addressH == '单数号' ? '1' : '2')
      }
      addAddress(param).then(res => {
        if (res.code !== 0) {
          this.$message.error(res.data.message)
        }else{
          this.newAddressDialog = false
          this.$message.success('操作成功')
          this.getAddressList()
        }
      }).finally(() => this.addressLoading = false)
    },
    exportNoClaimExcel() {
      let param = {noClaimList : this.noClaimList}
      console.log(param,"???")
      exportNoClaimExcel(param).then(res => {
        const url = window.URL.createObjectURL(res, { type: 'application/vnd.ms-excel;charset=utf-8' })
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '不可指派名单.xlsx')
        link.click()
      })
    },
    // 市场主体批量搜索
    async assignByFile() {
      const file = this.$refs.batchFile.files
      const form = new FormData()
      form.append('file', file[0])
      this.loading = true
      this.showFakeProgress = true
      return assignByFile(form).then(async({ code, data, msg }) => {
        if (code !== 0) {
          this.$refs.FakeProgress.error()
          this.$message.error(data.message)
          this.loading = false
          this.showFakeProgress = false
        } else {
          this.claimNum = data.claimNum
          this.noClaimNum = data.noClaimNum
          this.noClaimList = data.noClaimList
          this.$refs.FakeProgress.success()
          this.showFakeProgress = false
          this.loading = false
          this.fileImportDialog = false
          this.fileResultDialog = true
        }
        this.$refs.batchFile.value = ''
        this.batchFileName = ''
      }).catch(() => {
        this.loading = false
        this.showFakeProgress = false
      })
    },
    // 下载模版
    downloadFile() {
      const prev = process.env.NODE_ENV === 'development' ? '' : '/aiccs'
      window.open(prev + '/导入认领名单模板.xls')
    },
    fileChange() {
      this.batchFileName = this.$refs.batchFile.files[0].name
    },
    setSliceName1(e){
      this.submitForm1.sliceId = e.sliceId
      this.submitForm1.sliceName = e.sliceName
    },
    setSliceName3(e){
      this.submitForm3.sliceId = e.sliceId
      this.submitForm3.sliceName = e.sliceName
    },
    setSliceName4(e){
      this.submitForm4.sliceId = e.sliceId
      this.submitForm4.sliceName = e.sliceName
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'warning-row'
      }
    },
    // 发起搜索
    search() {
      const form = this['searchForm']
      const pageParam = this['pageParam']
      pageParam.curPage = 1
      pageParam.paramMap = {
        ...form
      }
      this.loadPage()
    },
    // 导出结果
    exportFile() {
      const pageParam = this.pageParam
      pageParam.paramMap = {
        ...this.searchForm
      }
      this.loading = true
      exportEntOwner(pageParam).then(res => {
        const url = window.URL.createObjectURL(res, { type: 'application/vnd.ms-excel;charset=utf-8' })
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '公共库管理主体名单.xlsx')
        link.click()
      }).finally(() => this.loading = false)
    },
    // 重置搜索表单
    resetSearchForm() {
      this['searchForm'] = {}
    },
    // 加载分页
    loadPage() {
      if (this.type === '0') {
        this.pageParam.paramMap.isAssign = 1
      } else {
        this.pageParam.paramMap.isAssign = 0
      }
      const pageParam = this['pageParam']
      this['loading'] = true
      entPage(pageParam).then(res => {
        this['loading'] = false
        if (res.code === 0) {
          let idx = (pageParam.curPage - 1) * pageParam.pageSize
          const list = res.data.records || []
          for (const i in list) {
            const each = list[i]
            each.index = ++idx
            each.uniscid = each.uniscid && each.uniscid.length > 0 ? each.uniscid : each.regno
            each.dom = this.$util.isNotEmpty(each.oploc) ? each.oploc : each.dom
          }
          this['tableData'] = list
          // this.pageParam = {
          //   curPage: 1,
          //   pageSize: 10,
          //   paramMap: {
          //     isAssign: 0
          //   },
          //   total: res.data.total
          // }
          this.pageParam.total = res.data.total
        }
      })
      // this.searchForm = {
      //   regOrg: ''
      // }
    },
    // 当前分页改变
    pageIndexChange(curPage) {
      this.pageParam.curPage = curPage
      this.loadPage(1)
    },
    // 分页大小改变
    pageSizeChange(pageSize) {
      this.pageParam.pageSize = pageSize
      this.loadPage(1)
    },
    // 打开认领对话框
    openAssignDialog(row) {
      this.submitForm1.pripid = row.pripid
      getAssignableSliceList(this.assignType).then(res => {
        if (res.code === 0 && res.data && res.data.length) {
          this.assignSliceOptions = res.data
          this.dialogVisible1 = true
        } else {
          this.$message.info('没有权限认领该主体')
        }
      })
    },
    // 打开退还信息框
    openRemoveAssignDialog(row) {
      getAssignInfo({ assignId: row.removeAssignId }).then(res => {
        if (res.code === 0) {
          this.removeAssignInfo = res.data.bizOwnerAssign
          this.removeAssignInfo.RemoveOrgName = res.data.removeOrgName
          this.dialogVisible1_1 = true
        }
      })
    },
    // 提交认领表单
    submitAssignForm() {
      this.$refs.submitForm1.validate(valid => {
        if (!valid) return
        this.$confirm('是否确认提交认领').then(_ => {
          assignOwner(this.submitForm1.sliceId, this.submitForm1.pripid).then(res => {
            if (res.code === 0) {
              this.$message.success('操作成功')
              this.dialogVisible1 = false
              this.submitForm1.sliceId = ''
              this.submitForm1.pripid = ''
              this.loadPage(1)
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(_ => {
        })
      })
    },
    // 打开退还窗口
    openDialog2(row) {
      this.entName = row.entName
      this.submitForm2 = {
        assignId: row.assignId,
        pripid: row.pripid
      }
      this.dialogVisible2 = true
    },
    // 退还
    removeAssign() {
      if (!this.submitForm2.remReason) {
        this.$message.warning('请填写退还原因')
        return
      }
      if (!this.submitForm2.phone) {
        this.$message.warning('请填写联系电话')
        return
      }
      if (this.PresidentType === 1) {
        const scli = '市公共片区'
      }
      this.$confirm('请确认是否将 ' + this.entName + '业户退还至 ' + this.removeSclice + ' ！').then(_ => {
        removeOwner(this.submitForm2).then(res => {
          if (res.code === 0) {
            this.$message.success('操作成功')
            this.submitForm2 = {
              pripid: '',
              assignId: '',
              remReason: '',
              phone: ''
            }
            this.loadPage()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).finally(() => this.dialogVisible2 = false)
    },
    // 虚假地址标记
    draftRevoke(row) {
      this.$confirm('是否确认标记为虚假地址主体?').then(_ => {
        inDraftRevoke({ pripid: row.pripid }).then(res => {
          if (res.code === 0) {
            this.$message.success('操作成功')
            this.loadPage()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(_ => {
      })
    },
    // 提交片区调整
    submitChangeForm() {
      if(this.isBatchAdjust){
        this.$confirm('是否确认批量调整为该片区').then(_ => {
          let params = {
            ent: this.assignEntOption,
            sliceId: this.submitForm3.sliceId
          }
          changeOwnerBatch(params).then(res => {
            if (res.code === 0) {
              this.$message.success('操作成功')
              this.dialogVisible3 = false
              this.isBatchAdjust = false
              this.loadPage()
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(_ => {
        })
      }else{
        this.$confirm('是否确认调整为该片区').then(_ => {
          changeOwner(this.submitForm3.sliceId, this.submitForm3.assignId).then(res => {
            if (res.code === 0) {
              this.$message.success('操作成功')
              this.dialogVisible3 = false
              this.loadPage()
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(_ => {
        })
      }
    },
    // 调整片区对话框打开
    changeAssign(row) {
      getChangeableSlice(row.sliceId).then(res => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (res.data.length === 0) {
          return this.$message.info('没有找到可调整的片区')
        }
        this.changeSliceOptions = res.data
        this.submitForm3.assignId = row.assignId
        this.submitForm3.sliceId = row.sliceId
        this.dialogVisible3 = true
      })
    },
    handleSelectionChange(val) {
      this.assignEntOption = val
    },
    // 获取可认领片区列表
    batchAssign() {
      if (this.assignEntOption.length === 0) {
        this.$message.error('请选择被指派业户')
      } else {
        this.submitForm4.pripids = []
        this.assignEntOption.forEach((v) => {
          this.submitForm4.pripids.push(v.pripid)
        })
        getAssignableSliceList(this.assignType).then(res => {
          if (res.code === 0 && res.data && res.data.length) {
            this.assignSliceOptions = res.data
            this.BatchAssignWindows = true
          } else {
            this.$message.info('没有权限认领该主体')
          }
        })
      }
    },
    // 打开批量退还窗口
    openDialog3() {
      if (this.assignEntOption.length === 0) {
        this.$message.error('请选择被退还业户')
      } else {
        this.BatchRemoveOwner = true
      }
    },
    // 打开批量调整窗口
    openDialog4() {
      if (this.assignEntOption.length === 0) {
        this.$message.error('请选择被调整业户')
      } else {
        getChangeableSlice(this.assignEntOption[0].sliceId).then(res => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          if (res.data.length === 0) {
            return this.$message.info('没有找到可调整的片区')
          }
          this.changeSliceOptions = res.data
          // this.submitForm3.assignId = row.assignId
          // this.submitForm3.sliceId = row.sliceId
          this.isBatchAdjust = true
          this.dialogVisible3 = true
        })
      }
    },
    // 提交批量退还表单
    BatchremoveOwner() {
      this.submitForm5 = []
      this.assignEntOption.forEach((v) => {
        const submitForm9 = {
          pripid: v.pripid,
          assignId: v.assignId,
          remReason: this.submitForm2.remReason,
          phone: this.submitForm2.phone
        }
        this.submitForm5.push(submitForm9)
      })
      this.$confirm('是否确认将' + this.assignEntOption.length + '户业户退还至' + this.removeSclice + ' !').then(_ => {
        BatchRemoveOwner(this.submitForm5).then(res => {
          this.submitForm5 = []
          if (res.code === 0) {
            this.$message.success('操作成功')
            this.BatchRemoveOwner = false
            this.submitForm2 = {
              pripid: '',
              assignId: '',
              remReason: '',
              phone: ''
            }
            this.loadPage(1)
          } else {
            this.$message.error(res.msg)
          }
        })
      }).finally(() => this.BatchRemoveOwner = false)
    },
    // 提交批量指派表单
    BatchsubmitAssignForm() {
      this.$refs.submitForm4.validate(valid => {
        if (!valid) return
        this.$confirm('是否确认提交认领').then(_ => {
          BatchAssignOwner(this.submitForm4.sliceId, this.submitForm4.pripids).then(res => {
            if (res.code === 0) {
              this.$message.success('操作成功')
              this.BatchAssignWindows = false
              this.submitForm4.sliceId = ''
              this.submitForm4.pripids = []
              this.loadPage(1)
            } else {
              this.$message.error(res.msg)
            }
          })
        }).catch(_ => {
        })
      })
    },
    // 区局认领市公共片区操作,所局认领区局公共片区操作
    claim(row) {
      this.$confirm('请确认是否将   ' + row.entName + ' 业户认领至 ' + this.user.orgName).then(_ => {
        assignOwner1(this.user.orgCode, row.pripid).then(res => {
          if (res.code === 0) {
            this.$message.success('操作成功')
            this.dialogVisible1 = false
            this.submitForm1.sliceId = ''
            this.submitForm1.pripid = ''
            this.loadPage(1)
          } else {
            this.$message.error(res.msg)
          }
        })
      }).finally(() => this.dialogVisible2 = false)
    },
    batchClaim() {
      if (this.assignEntOption.length === 0) {
        this.$message.error('请选择被认领业户')
      } else {
        this.submitForm4.pripids = []
        this.assignEntOption.forEach((v) => {
          this.submitForm4.pripids.push(v.pripid)
        })
        this.$confirm('您已选择 ' + this.assignEntOption.length + ' 户业户,请确认是否将其认领至 ' + this.user.orgName).then(_ => {
          BatchAssignOwner1(this.user.orgCode, this.submitForm4.pripids).then(res => {
            if (res.code === 0) {
              this.$message.success('操作成功')
              this.submitForm4.pripids = []
              this.loadPage(1)
            } else {
              this.$message.error(res.msg)
            }
          })
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.main-content {
  padding: 0;

  > div:not(:first-child) {
    margin-top: 16px;
  }

  .btn-box {
    text-align: right;
  }

  .main-body {
    padding: 16px 0;

    .header {
      .header-label {
        font-size: 20PX;
        font-family: Microsoft YaHei, Microsoft YaHei-Bold;
        font-weight: bold;
        color: #243668;
        line-height: 32px;
      }
    }
  }
}

.search-box {
  padding: 20px;
  margin-bottom: 40px;
  /* background:#f9f9f9; */
  /* border: 1px solid $color-border; */
  /* padding:15px 50px; */
  .search-field {
    display: inline-block;
    width: 100%;
    display: flex;
    align-items: center;

    > p {
      width: 190px;
      text-align: right;
      display: inline-block;
      margin: 0 0 10px 0;
      font-family: Microsoft YaHei, Microsoft YaHei-Bold;
      color: #5e5e7a;
      font-size: 16PX;
    }
  }
}

.mb10 {
  margin-bottom: 10px;
}

/deep/ .el-input__inner {
  height: 28px;
}

/deep/ .label-name {
  font-size: 16px !important;
  text-align: right;
  width: 190px;
}

.search-input-box {
  width: 70% !important;
}

.search-field {
  .el-input {
    width: 70%;
  }
}

.content {
  background: #fff;

  /deep/ .el-table .warning-row {
    background: #eaf4fe !important;
  }
}
</style>
