<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" @submit.native.prevent>
      <el-form-item label="地图名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入地图名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-no-more-click type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button v-no-more-click 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 v-no-more-click
                   type="primary"
                   plain
                   icon="el-icon-plus"
                   size="mini"
                   @click="handleAdd"
                   v-hasPermi="['iscs:map:add']"
        >新增</el-button>
      </el-col>
<!--      <el-button-->
<!--        v-no-more-click-->
<!--        type="info"-->
<!--        plain-->
<!--        icon="el-icon-upload2"-->
<!--        size="mini"-->
<!--        @click="handleOpenImport"-->

<!--      >导入</el-button-->
<!--      >-->
      <el-col :span="1.5">
        <el-button v-no-more-click
                   type="danger"
                   plain
                   icon="el-icon-delete"
                   size="mini"
                   :disabled="multiple"
                   @click="handleDelete"
                   v-hasPermi="['iscs:map:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="地图编号" align="center"  prop="id" ></el-table-column>
      <el-table-column label="地图名称" align="center"  prop="name" >
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地图图片" align="left" prop="imageUrl" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <div class="img-box" v-if="scope.row.imageUrl">
            <el-image
              style="width: 75px; height: 75px"
              :preview-teleported="true"
              class="images"
              :hide-on-click-modal="true"
              :src="scope.row.imageUrl"
              :zoom-rate="1.2"
              :preview-src-list="[scope.row.imageUrl]"
              :initial-index="1"

            >
            </el-image>
            <i class="el-icon-zoom-in" id="eyeicon"></i>
          </div>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="地图宽度" align="center" prop="width" >

      </el-table-column>
      <el-table-column label="地图高度" align="center" prop="height" >

      </el-table-column>
      <el-table-column label="横坐标" align="center" prop="x" >

      </el-table-column>
      <el-table-column label="纵坐标" align="center" prop="y" >

      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-no-more-click
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['iscs:map:edit']"
          >修改</el-button>
<!--          <el-button v-no-more-click-->
<!--                     size="mini"-->
<!--                     type="text"-->
<!--                     icon="el-icon-edit"-->
<!--                     @click="handleDetail(scope.row)"-->
<!--                     v-hasPermi="['iscs:map:edit']"-->
<!--          >详情</el-button>-->
          <el-button v-no-more-click
                     size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['iscs:map:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @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="name">
          <el-input v-model="form.name" placeholder="请输入地图名称" />
        </el-form-item>
        <el-form-item label="地图图片" prop="imageUrl">
          <ImageUploadSingle
            :limit="1"
            :value="form.imageUrl"
            :fileSize="5"
            @onUploaded="handleIconUplaoded"
            @onRemoved="handleIconRemoved"
          ></ImageUploadSingle>
        </el-form-item>
        <el-form-item label="图片宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入图片宽度" />
        </el-form-item>
        <el-form-item label="图片高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入图片高度" />
        </el-form-item>
        <el-form-item label="横坐标" prop="x">
          <el-input v-model="form.x" placeholder="请输入横坐标" />
        </el-form-item>
        <el-form-item label="纵坐标" prop="y">
          <el-input v-model="form.y" placeholder="请输入纵坐标" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitForm">确 定</el-button>
        <el-button v-no-more-click @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--    车总测试-->
    <el-dialog  :visible.sync="ImportVisible" width="500px" append-to-body>
      <el-form ref="form" :model="ImportForm" :rules="rules" label-width="80px">
        <el-form-item label="userName" prop="userName">
          <el-input v-model="ImportForm.userName" placeholder="请输入userName" />
        </el-form-item>
        <el-button
          v-no-more-click
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['iscs:sop:import']"
        >导入</el-button
        >
      </el-form>

    </el-dialog>
<!--    车总测试-->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls,.png,.jpg,.jpeg,.doc,.docx,.pdf"
        :headers="upload.headers"
        :action="upload.url + '?userName=' + upload.userName"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.userName" /> 是否更新已经存在的设备数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitFileForm">确 定</el-button>
        <el-button v-no-more-click @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { deleteIsMapByIds, getIsMapPage, insertIsMap, selectIsMapById, updateIsMap } from '@/api/system/mapconfig'
import { getToken } from '@/utils/auth'

export default {
  name: "autoCodeRule",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name:undefined,
      },
      // 表单参数
      form: {},
      ImportVisible:false,//车总测试导入
      ImportForm:{},//车总测试
      // 车总测试 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        userName: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/characteristic/insertUserFingerprintDat"
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "地图名称", trigger: "blur" }
        ],
        imageUrl: [
          { required: true, message: "地图图片", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 车总测试用的
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
      this.upload.userName=this.ImportForm.userName;
    },
    handleOpenImport(){
      this.ImportVisible = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {

      this.$refs.upload.submit();
    },
    // 上面都是车总测试

    handleDetail(row){
      selectIsMapById(row.id).then((res)=>{
        console.log(res);
      })
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getIsMapPage(this.queryParams).then(response => {
          this.List = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attribute:"",
        value:"",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加地图数据";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ruleId = row.id || this.ids
      selectIsMapById(ruleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改地图数据";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateIsMap(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertIsMap(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //图标上传成功
    handleIconUplaoded(imgUrl) {
      this.form.imageUrl = imgUrl[0].url
    },
    // 图标移除
    handleIconRemoved(imgUrl) {
      this.form.imageUrl = null
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      debugger;
      const ruleIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除所选数据项？').then(function() {
        return deleteIsMapByIds(ruleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

  }
};
</script>
<style scoped lang="scss">
//图片放大
.img-box {
  width: 75px;
  height: 75px;
  position: relative;

  #eyeicon {
    display: none;
  }
}

.img-box:hover {
  background: #000;

  .images {
    opacity: 0.6;
  }

  #eyeicon {
    display: block;
    position: absolute;
    top: 40%;
    left: 40%;
    z-index: 100;
    color: white;
    pointer-events: none;
  }
}
</style>
