<template>
  <div class="gassVehicle app-container">

    <drawer title="重大危险源" :inner="true" :width="drawerWidth" :mask="false">

      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="资源名称" prop="resourceName">
          <el-input
            v-model="queryParams.resourceName"
            placeholder="请输入资源名称"
            clearable
            size="small"
            style="width: 140px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="资源类型" prop="resourceType">
          <el-select v-model="queryParams.resourceType" style="width: 140px" placeholder="请选择资源类型" clearable size="small">
            <el-option
              v-for="dict in resourceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input
            v-model="queryParams.contacts"
            placeholder="请输入联系人"
            clearable
            size="small"
            style="width: 125px"
            @keyup.enter.native="handleQuery"
          />
        </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"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="resourcesList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="45" align="center" />
        <el-table-column label="资源名称" align="center" prop="resourceName" />
        <el-table-column label="资源类型" align="center" prop="resourceType" :formatter="resourceTypeFormat" />
        <el-table-column label="经度" align="center" prop="longitude" />
        <el-table-column label="纬度" align="center" prop="latitude" />
        <el-table-column label="联系人" align="center" prop="contacts" />
        <el-table-column label="联系电话" align="center" prop="contactNumber" />
        <el-table-column label="资源描述" align="center" prop="resourceDescription" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-map-location"
              @click="getViewLocation(scope.row)"
            >查看位置</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</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"
      />

    </drawer>

    <div id="map"></div>

    <!-- 添加或修改应急资源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="资源名称" prop="resourceName">
              <el-input v-model="form.resourceName" placeholder="请输入资源名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资源类型" prop="resourceType">
              <el-select v-model="form.resourceType" placeholder="请选择资源类型">
                <el-option
                  v-for="dict in resourceTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>-->
        <el-row>
          <el-col :span="23">
            <el-form-item label="经纬度" prop="longitude">
              <el-col :span="9">
                <el-input v-model="form.longitude" placeholder="请输入经度" />
              </el-col>
              <el-col :span="9" style="margin-left: 10px">
                <el-input v-model="form.latitude" placeholder="请输入纬度" />
              </el-col>
              <el-col :span="3" style="margin-left: 30px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="资源描述" prop="resourceDescription">
          <el-input v-model="form.resourceDescription" type="textarea" 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>

    <GetPos
      :dialogVisible.sync="dialogTableVisibles"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />
  </div>
</template>

<script>
import { listResources, getResources, delResources, addResources, updateResources, exportResources } from "@/api/emergencydrills/resources";
import drawer from  "./component/drawer";
import GetPos from '@/components/GetPos';
import { mapBound } from "@/utils/mapClass/boundaryMap";

export default {
  name: "Resources",
  components: {
    GetPos,
    drawer
  },
  data() {
    return {
      map: null,
      recordMarkers: [],
      drawerWidth: '800px',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急资源表格数据
      resourcesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 资源类型字典
      resourceTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        resourceName: null,
        resourceType: null,
        contacts: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      /**--------------地图使用数据---------------*/
      dialogTableVisibles: false,
      devicePos: []
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_resource_type").then(response => {
      this.resourceTypeOptions = response.data;
    });
  },
  mounted(){
    this.map = new AMap.Map("map", {
      center: [117.08, 39.98],
      zoom: 14,
    });
    mapBound('三河市', this.map, false);
  },
  methods: {
    /** 查询应急资源列表 */
    getList() {
      this.loading = true;
      listResources(this.queryParams).then(response => {
        this.resourcesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 资源类型字典翻译
    resourceTypeFormat(row, column) {
      return this.selectDictLabel(this.resourceTypeOptions, row.resourceType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        resourceName: null,
        resourceType: null,
        longitude: null,
        latitude: null,
        contacts: null,
        contactNumber: null,
        resourceDescription: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应急资源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getResources(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急资源";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResources(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResources(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除应急资源编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delResources(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有应急资源数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportResources(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度 选择
     * @param res
     */
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    /**获取设备位置*/
    getViewLocation(row){
      this.map.remove(this.recordMarkers);
      if (row.longitude!=="" && row.latitude!="" && row.longitude!==undefined && row.latitude!==undefined){
        let marker = new AMap.Marker({
          icon: require("@/assets/firstimage/yjzy.png"),
          position: [row.longitude,row.latitude],
          offset: new AMap.Pixel(-13, -30)
        });
        /*marker.on('click', () => {
          this.showDetail(row);
        });*/
        console.log(this.map)
        this.map.add(marker);
        this.map.setCenter([row.longitude,row.latitude]);
        this.recordMarkers.push(marker);
      }else{
        this.$message({
          message: '当前设备未配置位置信息！',
          type: 'warning'
        });
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.gassVehicle{
  positoin:relative;
  /*background-color: red;*/
  height:calc(100vh - 50px);
  padding:20px;
  padding-bottom:0px;
}

#map{
  /*position: fixed;*/
  /*width:calc(100vw - 250px);*/
  /*top:70px;*/
  /*bottom:0;*/
  height: 100%;
  float: right;
  width: calc(100vw - 1000px);
}

/*!* 表格最外边框 *!
.el-table --border, .el-table --group{
  border: none;
}*/
.el-table{
  background-color: #00afff !important;
}

</style>
