<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px">
      <el-form-item label="门店编码" prop="storeCode">
        <el-input
          v-model="queryParams.storeCode"
          placeholder="请输入门店编码"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="门店名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入门店名称"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="门店类型" prop="storeCategoryList">
        <el-select
          v-model="queryParams.storeCategoryList"
          clearable
          placeholder="请选择门店类型"
          clearable
          size="mini"
          multiple
          collapse-tags
          @change="onInput">
          <el-option
            v-for="item1 in storeCategoryOptions"
            :key="item1.dictValue"
            :label="item1.dictLabel"
            :value="item1.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店有效性" prop="validFlag">
        <el-select
          v-model="queryParams.validFlag"
          clearable
          size="mini"
          placeholder="请选择门店有效性"
          @change="onInput">
          <el-option label="有效" value="0"></el-option>
          <el-option label="无效" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否处理" prop="state">
        <el-select
          v-model="queryParams.stateList"
          clearable
          multiple
          size="mini"
          placeholder="请选择是否处理"
          @change="onInput">
          <el-option label="待处理" value="0"></el-option>
          <el-option label="已处理" value="1"></el-option>
          <el-option label="地址待确认" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否跳过校验" prop="skipCheck" label-width="100px">
        <el-select
          v-model="queryParams.skipCheck"
          clearable
          size="mini"
          placeholder="请选择是否跳过校验"
          @change="onInput">
          <el-option label="不跳过校验" value="0"></el-option>
          <el-option label="跳过校验" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="归属人工号" prop="workCode">
        <el-input
          v-model="queryParams.workCode"
          placeholder="请输入归属人工号"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="更新时间" prop="queryTime" @change="onInput()" size="mini">
        <el-date-picker
          v-model="queryTime"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['sfa:storeAddressCheck:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['sfa:storeAddressCheck:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['sfa:storeAddressCheck:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sfa:storeAddressCheck:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5" style="position: absolute; right: 96px">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="initUpdateStore"
          :disabled="multiple"
          v-hasPermi="['sfa:store:edit']"
          >地址规范状态设置为已处理</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="storeAddressCheckList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable="selectable" />
      <!--      <el-table-column label="${comment}" align="center" prop="storeAddressId" />-->
      <el-table-column label="门店编码" width="110" align="center" prop="storeCode" />
      <!--      <el-table-column label="门店ID" align="center" prop="storeId" />-->
      <el-table-column
        label="门店名称"
        width="160"
        align="center"
        prop="storeName"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="门店类型"
        width="160"
        align="center"
        prop="storeCategoryName"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="门店有效性"
        width="160"
        align="center"
        prop="validFlag"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="组织机构"
        width="160"
        align="center"
        prop="orgName"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="更新前地址"
        align="center"
        width="160"
        prop="addressLine"
        :show-overflow-tooltip="true" />
      <el-table-column label="可信度参考" width="110" align="center" prop="reliability" />
      <el-table-column label="解析精度级别" width="110" align="center" prop="level" />
      <el-table-column
        label="异常原因"
        align="center"
        width="160"
        prop="exceptionCause"
        :show-overflow-tooltip="true" />
      <el-table-column label="是否处理" width="110" align="center" prop="stateName" />
      <el-table-column label="是否跳过校验" width="110" align="center" prop="skipCheckName" />
      <el-table-column
        label="更新后地址"
        align="center"
        width="160"
        prop="updateAddressLine"
        :show-overflow-tooltip="true" />
      <el-table-column label="归属人工号" width="110" align="center" prop="workCode" />
      <el-table-column label="省" align="center" prop="provinceName" />
      <el-table-column label="市" align="center" prop="cityName" :show-overflow-tooltip="true" />
      <el-table-column
        label="区"
        align="center"
        prop="districtName"
        :show-overflow-tooltip="true" />
      <el-table-column label="街道" align="center" prop="townName" :show-overflow-tooltip="true" />
      <el-table-column
        label="最后一次更新前地址"
        width="160"
        align="center"
        prop="frontAddressLine"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="最后一次更新前经度"
        width="160"
        align="center"
        prop="frontLon"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="最后一次更新前纬度"
        width="160"
        align="center"
        prop="frontLat"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="门店地址更新总次数"
        width="160"
        align="center"
        prop="updateNum"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remarks" :show-overflow-tooltip="true" />
      <el-table-column
        label="操作"
        align="center"
        width="170px"
        class-name="small-padding fixed-width"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:storeAddressCheck:edit']">
            <span v-if="scope.row.skipCheck == '0'">跳过地址校验</span>
            <span v-else>取消跳过地址校验</span>
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleStoreAddressSate(scope.row)"
            v-hasPermi="['sfa:storeAddressCheck:edit']">
            <span v-if="scope.row.state != '0'">重置为待规范</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改门店地址校验管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门店编码" prop="storeCode">
          <el-input v-model="form.storeCode" placeholder="请输入门店编码" />
        </el-form-item>
        <el-form-item label="门店ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入门店ID" />
        </el-form-item>
        <el-form-item label="门店名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入门店名称" />
        </el-form-item>
        <el-form-item label="组织机构id" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入组织机构id" />
        </el-form-item>
        <el-form-item label="地址" prop="addressLine">
          <el-input v-model="form.addressLine" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="可信度参考" prop="reliability">
          <el-input v-model="form.reliability" placeholder="请输入可信度参考" />
        </el-form-item>
        <el-form-item label="解析精度级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入解析精度级别" />
        </el-form-item>
        <el-form-item label="删除标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="异常原因" prop="exceptionCause">
          <el-input v-model="form.exceptionCause" placeholder="请输入异常原因" />
        </el-form-item>
        <el-form-item label="归属人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入归属人" />
        </el-form-item>
        <el-form-item label="0=待处理，1=已处理" prop="state">
          <el-input v-model="form.state" placeholder="请输入0=待处理，1=已处理" />
        </el-form-item>
        <el-form-item label="更新后地址" prop="updateAddressLine">
          <el-input v-model="form.updateAddressLine" placeholder="请输入更新后地址" />
        </el-form-item>
        <el-form-item label="归属人工号" prop="workCode">
          <el-input v-model="form.workCode" placeholder="请输入归属人工号" />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区" prop="district">
          <el-input v-model="form.district" placeholder="请输入区" />
        </el-form-item>
        <el-form-item label="街道" prop="townName">
          <el-input v-model="form.townName" placeholder="请输入街道" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStoreAddressCheck,
  getStoreAddressCheck,
  delStoreAddressCheck,
  addStoreAddressCheck,
  updateStoreAddressCheck,
  updateStoreAddressState,
} from '@/api/sfa/storeAddressCheck';

export default {
  name: 'StoreAddressCheck',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店地址校验管理表格数据
      storeAddressCheckList: [],
      storeCategoryOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      queryTime: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeCode: null,
        storeId: null,
        storeName: null,
        orgId: null,
        addressLine: null,
        reliability: null,
        level: null,
        exceptionCause: null,
        userId: null,
        state: null,
        skipCheck: null,
        validFlag: null,
        updateAddressLine: null,
        workCode: null,
        province: null,
        city: null,
        district: null,
        townName: null,
        queryTime: null,
        storeCategoryList: null,
        stateList: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // 门店类型
        storeCategoryOptions: [],
      },
    };
  },
  created() {
    this.getDicts('store_address_check_category').then((response) => {
      this.storeCategoryOptions = response.data;
    });
    this.getList();
  },
  methods: {
    // 当前列是否可以被选中
    selectable(row) {
      return row.state === '2';
    },
    onInput() {
      this.$forceUpdate();
    },
    /** 查询门店地址校验管理列表 */
    getList() {
      this.loading = true;
      listStoreAddressCheck(this.queryParams).then((response) => {
        this.storeAddressCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 门店类型
    storeCategoryFormatter(row, column) {
      return this.selectDictLabel(this.storeCategoryOptions, row.storeCategory);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeAddressId: null,
        storeCode: null,
        storeId: null,
        storeName: null,
        orgId: null,
        addressLine: null,
        reliability: null,
        level: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        exceptionCause: null,
        userId: null,
        state: null,
        stateName: null,
        skipCheck: null,
        skipCheckName: null,
        validFlag: null,
        updateAddressLine: null,
        workCode: null,
        province: null,
        city: null,
        district: null,
        townName: null,
        queryTime: null,
        storeCategoryList: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryTime != null && this.queryTime != '') {
        this.queryParams.startTime = this.queryTime[0] + ' 00:00:00';
        this.queryParams.endTime = this.queryTime[1] + ' 23:59:59';
      } else {
        this.queryParams.startTime = '';
        this.queryParams.endTime = '';
      }
      if (this.queryParams.storeCategoryList != null) {
        if (this.queryParams.storeCategoryList.length < 1) {
          this.queryParams.storeCategoryList = null;
        }
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.storeCategoryList = undefined;
      this.queryTime = [];
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.storeAddressId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加门店地址校验管理';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storeAddressId = row.storeAddressId || this.ids;
      getStoreAddressCheck(storeAddressId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = '修改门店地址校验管理';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.storeAddressId != null) {
            updateStoreAddressCheck(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addStoreAddressCheck(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    initUpdateStore() {
      const storeAddressIds = this.ids;
      console.log('选择规范门店ID数据' + storeAddressIds);
      this.$modal
        .confirm('是否修改门店地址规范状态为已处理？')
        .then(function () {
          return updateStoreAddressState(storeAddressIds).then((response) => {
            console.log(response);
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('修改成功');
        })
        .catch(() => {});
    },
    handleDelete(row) {
      if (row.skipCheck == '0') {
        this.$modal
          .confirm('是否确认跳过门店名称为"' + row.storeName + '"的地址校验？')
          .then(function () {
            updateStoreAddressCheck({ storeAddressId: row.storeAddressId, skipCheck: 1 }).then(
              (response) => {
                if (response.code == 200) {
                  return true;
                }
              }
            );
          })
          .then(() => {
            this.$modal.msgSuccess('操作成功');
            this.open = false;
            this.getList();
          });
      } else {
        this.$modal
          .confirm('是否取消跳过门店名称为"' + row.storeName + '"的地址校验？')
          .then(function () {
            updateStoreAddressCheck({ storeAddressId: row.storeAddressId, skipCheck: 0 }).then(
              (response) => {
                if (response.code == 200) {
                  return true;
                }
              }
            );
          })
          .then(() => {
            this.$modal.msgSuccess('操作成功');
            this.open = false;
            this.getList();
          });
      }
    },
    handleStoreAddressSate(row) {
      if (row.state != '0') {
        this.$modal
          .confirm('是否确认重置门店名称为"' + row.storeName + '"的地址校验状态为待规范？')
          .then(function () {
            updateStoreAddressCheck({ storeAddressId: row.storeAddressId, state: 0 }).then(
              (response) => {
                if (response.code == 200) {
                  return true;
                }
              }
            );
          })
          .then(() => {
            this.$modal.msgSuccess('操作成功');
            this.open = false;
            this.getList();
          });
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.queryParams.storeCategoryList != null) {
        if (this.queryParams.storeCategoryList.length < 1) {
          this.queryParams.storeCategoryList = null;
        }
      }
      this.download(
        'sfa/storeAddressCheck/export',
        {
          ...this.queryParams,
        },
        `storeAddressCheck_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
