<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地图名称" prop="mapId">
        <el-select v-model="queryParams.mapId" placeholder="地图名称">
          <el-option v-for="item in this.MapOptions" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </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-point:add']"
        >新增
        </el-button>
      </el-col>
      <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-point:remove']"
        >删除
        </el-button>
      </el-col>
<!--      <el-button v-no-more-click-->
<!--                 type="info"-->
<!--                 plain-->
<!--                 icon="el-icon-upload2"-->
<!--                 size="mini"-->
<!--                 @click="handleImport"-->

<!--      >导入</el-button>-->
      <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" width="210px" prop="id">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地图名称" align="center" width="210px" prop="mapName">
        <template slot-scope="scope">
          <span>{{ scope.row.mapName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地图类型" align="center" prop="mapType" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.map_type"
            :value="scope.row.mapType"
          />
        </template>
      </el-table-column>

      <el-table-column label="实体名称" align="center" width="210px" prop="entityName">
        <template slot-scope="scope">
          <span>{{ scope.row.entityName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="横坐标" align="center" prop="x" width="180">
      </el-table-column>
      <el-table-column label="纵坐标" align="center" prop="y" width="180">
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <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-point: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-point: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="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="地图名称" prop="mapId">
          <el-select style="width: 280px" v-model="form.mapId" placeholder="地图名称">
            <el-option v-for="item in this.MapOptions" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地图类型" prop="mapType">
          <el-select style="width: 280px" v-model="form.mapType" placeholder="地图类型">
            <el-option
              v-for="dict in dict.type.map_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.mapType=='1'" label="实体" prop="entityId">
          <treeselect
            style="width: 280px"
            v-model="form.entityId"
            :options="deptOptions"
            :normalizer="Marsnormalizer"
            placeholder="选择实体"
            @change="handleWorkstationChange"
          />
        </el-form-item>
        <el-form-item v-if="form.mapType=='2'" label="实体" prop="entityId">
          <el-select style="width: 280px" v-model="form.entityId" placeholder="实体">
            <el-option v-for="item in this.spmOptions" :key="item.pointId" :label="item.pointName" :value="item.pointId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.mapType=='3'" label="实体" prop="entityId">
          <el-select style="width: 280px" v-model="form.entityId" placeholder="实体">
            <el-option v-for="item in this.spmOptions" :key="item.pointId" :label="item.pointName" :value="item.pointId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.mapType=='4'" label="实体" prop="entityId">
          <el-select style="width: 280px" v-model="form.entityId" placeholder="实体">
            <el-option v-for="item in this.motorOptions" :key="item.motorId" :label="item.motorName" :value="item.motorId">
            </el-option>
          </el-select>
        </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
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".mp4, .pdf, .doc, .docx, .xls, .xlsx"
        :headers="upload.headers"
        :action="upload.url"
        :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">
          <span>mp4、pdf, .doc, .docx, .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 {
  deleteIsMapPointByIds,
  getIsMapPointPage,
  insertIsMapPoint,
  selectIsMapPointById,
  updateIsMapPoint
} from '@/api/system/mappoint'
import { getIsMapPage } from '@/api/system/mapconfig'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { listMarsDept } from '@/api/system/marsdept'
import { getIsIsolationPointPage } from '@/api/mes/spm/segregationPoint'
import { getToken } from '@/utils/auth'
import { getIsMotorPage } from '@/api/mes/motor'

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

      // 查询参数
      queryParams: {
        current: 1,
        size: 10
      },
      // 表单参数
      form: {},
      // 地图名称 下拉
      MapOptions: null,
      // 岗位下拉
      deptOptions: null,
      // 隔离点下拉
      spmOptions: null,
      motorOptions: null,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/loginByFingerprintDat"
      },
      // 表单校验
      rules: {
        mapId: [
          { required: true, message: '地图名称', trigger: 'blur' }
        ],
        mapType: [
          { required: true, message: '地图类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getOtherList()
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = 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();
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true
      getIsMapPointPage(this.queryParams).then(response => {
          this.List = response.data.records
          this.total = response.data.total
          this.loading = false
        }
      )
    },
    getOtherList() {
      const data = {
        current: 1,
        size: -1
      }
      getIsMapPage(data).then(response => {
        this.MapOptions = response.data.records
      })
      listMarsDept(data).then(response => {
        //
        console.log(response,'岗位')
        this.deptOptions = this.handleTree(response.data.records,"workstationId","parentId")
      })
      getIsIsolationPointPage(data).then(response => {
        console.log(response,'隔离点数据')
        this.spmOptions = response.data.records
      })
      getIsMotorPage(data).then(response => {
        console.log(response,'电机点位')
        this.motorOptions=response.data.records
      })
    },
    /** 转换mars岗位数据结构 */
    Marsnormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.workstationId,
        label: node.workstationName,
        children: node.children,
      };
    },
    handleWorkstationChange(value) {
      this.form.entityId = value;
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
       mapId: undefined,
        mapType: undefined,
        x: undefined,
        y: undefined,
      }
      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
      selectIsMapPointById(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) {
            updateIsMapPoint(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            insertIsMapPoint(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      debugger;
      const ruleIds = row.id || this.ids
      this.$modal.confirm('是否确认删除所选数据项？').then(function() {
        return deleteIsMapPointByIds(ruleIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>
