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

    <el-dialog :visible.sync="showLongitude" append-to-body>
      经度
      <el-input v-model="form.longitude"></el-input>
      纬度
      <el-input v-model="form.latitude"></el-input>
      <supermap ref="enterprisemap" :visible.sync="showLongitude"
                style="width: 100%;height: 500px;" :setLatLng="setLatLng"></supermap>
      <el-button type="primary" @click="showLongitude=false">确定</el-button>
    </el-dialog>

    <el-form :model="queryParams" class="formclass" ref="queryForm" size="large" :inline="true" v-show="showSearch"
             label-width="100px">
      <el-form-item label="巡更点名称" prop="busPatrolstationName">
        <el-input
          v-model="queryParams.busPatrolstationName"
          placeholder="请输入巡更点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="busPatrolstationCode">
        <el-input
          v-model="queryParams.busPatrolstationCode"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="所属企业id" prop="busEnterpriseId">-->
      <!--<el-input-->
      <!--v-model="queryParams.busEnterpriseId"-->
      <!--placeholder="请输入所属企业id"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属企业名称" prop="busEnterpriseName">-->
      <!--<el-input-->
      <!--v-model="queryParams.busEnterpriseName"-->
      <!--placeholder="请输入所属企业名称"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="经度" prop="longitude">-->
      <!--<el-input-->
      <!--v-model="queryParams.longitude"-->
      <!--placeholder="请输入经度"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="纬度" prop="latitude">-->
      <!--<el-input-->
      <!--v-model="queryParams.latitude"-->
      <!--placeholder="请输入纬度"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="备注" prop="remarks">-->
      <!--<el-input-->
      <!--v-model="queryParams.remarks"-->
      <!--placeholder="请输入备注"-->
      <!--clearable-->
      <!--@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"
          v-hasPermi="['system:patrolstation: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="['system:patrolstation: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="['system:patrolstation: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="['system:patrolstation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patrolstationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="id" align="center" prop="busPatrolstationId" />-->
      <el-table-column label="巡更点名称" align="center" prop="busPatrolstationName"/>
      <el-table-column label="编码" align="center" prop="busPatrolstationCode"/>
      <!--<el-table-column label="所属企业id" align="center" prop="busEnterpriseId" />-->
      <el-table-column label="所属企业名称" align="center" prop="busEnterpriseName"/>
      <el-table-column label="经度" align="center" prop="longitude"/>
      <el-table-column label="纬度" align="center" prop="latitude"/>
      <el-table-column label="备注" align="center" prop="remarks"/>
      <el-table-column label="状态" align="center" prop="delFlag"/>
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:patrolstation:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:patrolstation:remove']"
          >废弃
          </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="busPatrolstationName">
          <el-input v-model="form.busPatrolstationName" placeholder="请输入巡更点名称"/>
        </el-form-item>
        <el-form-item label="编码" prop="busPatrolstationCode">
          <el-input v-model="form.busPatrolstationCode" placeholder="请输入编码"/>
        </el-form-item>
        <!--<el-form-item label="所属企业id" prop="busEnterpriseId">-->
        <!--<el-input v-model="form.busEnterpriseId" placeholder="请输入所属企业id" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="所属企业名称" prop="busEnterpriseName">-->
        <!--<el-input v-model="form.busEnterpriseName" placeholder="请输入所属企业名称" />-->
        <!--</el-form-item>-->
        <el-form-item label="经度" prop="longitude" @dblclick.native="showMap">
          <el-input v-model="form.longitude" placeholder="请输入经度"/>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude" @dblclick.native="showMap">
          <el-input v-model="form.latitude" placeholder="请输入纬度"/>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" 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 {
    listPatrolstation,
    getPatrolstation,
    delPatrolstation,
    addPatrolstation,
    updatePatrolstation
  } from "@/api/centerdata_emergency1/patrolstation";
  import supermap from './supermap'

  export default {
    name: "Patrolstation",
    components: {
      supermap
    },
    data() {
      return {
        showLongitude: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 巡更点表格数据
        patrolstationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          busPatrolstationName: null,
          busPatrolstationCode: null,
          busEnterpriseId: null,
          busEnterpriseName: null,
          longitude: null,
          latitude: null,
          remarks: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          busPatrolstationCode: [
            {required: true, message: "巡更点编码不能为空", trigger: "blur"}
          ],
          busPatrolstationName: [
            {required: true, message: "巡更点名称不能为空", trigger: "blur"}
          ],
        }
      };
    },

    watch: {
      '$route.query.busEnterpriseId': {
        handler: function (newValue, oldValue) {
          if (newValue != undefined) {
            this.queryParams.busEnterpriseId = newValue
          }
          if (oldValue != undefined) {
            this.queryParams.busEnterpriseId = oldValue
          }
          this.getList();
        },
      }
    },
    created() {
      this.queryParams.busEnterpriseId = this.$route.query.busEnterpriseId
      this.form.busEnterpriseId = this.$route.query.busEnterpriseId
      this.form.busEnterpriseName = this.$route.query.busEnterpriseName
      this.getList();
    },
    methods: {
      showMap() {
        this.showLongitude = true
        this.$nextTick(() => {
          if (this.form.latitude != null && this.form.latitude != undefined && this.form.longitude != null && this.form.longitude != undefined) {
            this.$refs.enterprisemap.setMarker(this.form.latitude, this.form.longitude)
          }
        })
      },
      setLatLng: function (lat, lng) {
        this.form.latitude = lat
        this.form.longitude = lng
      },
      /** 查询巡更点列表 */
      getList() {
        this.loading = true;
        listPatrolstation(this.queryParams).then(response => {
          this.patrolstationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          busPatrolstationId: null,
          busPatrolstationName: null,
          busPatrolstationCode: null,
          busEnterpriseId: this.form.busEnterpriseId,
          busEnterpriseName: this.form.busEnterpriseName,
          longitude: null,
          latitude: null,
          remarks: 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.busPatrolstationId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加巡更点";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const busPatrolstationId = row.busPatrolstationId || this.ids
        getPatrolstation(busPatrolstationId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改巡更点";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.busPatrolstationId != null) {
              updatePatrolstation(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPatrolstation(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const busPatrolstationIds = row.busPatrolstationId || this.ids;
        this.$modal.confirm('是否确认废弃巡更点编号为"' + busPatrolstationIds + '"的数据项？').then(function () {
          return delPatrolstation(busPatrolstationIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("废弃成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/patrolstation/export', {
          ...this.queryParams
        }, `patrolstation_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
