<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="dictType">
        <el-select v-model="queryParams.areaId">
          <el-option
            v-for="item in areaOptions"
            :key="item.areaId"
            :label="item.areaName"
            :value="item.areaId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="禁停区" label-width="100" prop="parkingName">
        <el-input
          v-model="queryParams.parkingName"
          placeholder="请输入禁停区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" clearable>
          <el-option
            v-for="dict in dict.type.et_parking_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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:parking: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:parking: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:parking: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:parking:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="parkingId" />
      <el-table-column label="名称" align="center" prop="parkingName" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.et_parking_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" key="status" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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:parking:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:parking: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="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="禁停区" label-width="90" prop="parkingName">
            <el-input style="width: 90%" v-model="form.parkingName" placeholder="请输入禁停区" />
          </el-form-item>
        </el-col>
        <el-col>
          <el-col :span="12">
            <el-form-item label="类型">
              <el-radio-group v-model="form.type">
                <el-radio
                  v-for="dict in dict.type.et_parking_type"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-form-item label="边界" prop="boundaryStr">
            <area-map :key="key" :zoom="19" v-model="form.boundaryStr" :dataId="form.areaId" :lon="areaLon" :lat="areaLat" :pathList="form.boundaryStr"  @mapList="mapList" @center="center" />
          </el-form-item>
<!--          <span style="color: red">双击区域可重新编辑</span>-->
        </el-col>
        <el-col >
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请新建边界" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请新建边界" disabled/>
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </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 {
    listParking,
    getParking,
    delParking,
    addParking,
    updateParking,
    changeParkingStatus
  } from '@/api/system/parking'
import {getArea, optionselect as getAreaOptionselect} from "@/api/system/area";


export default {
  name: "Parking",
  dicts: ['et_parking_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 禁停区表格数据
      parkingList: [],
      // 默认区域id
      defaultAreaId: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 通过key重新渲染area-map组件
      key: 0,
      areaLon: null,
      areaLat: null,
      // 分区列表
      areaOptions:[],
      // 类型列表
      typeOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkingName: null,
        areaId: null,
        type: "2"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parkingName: [
          { required: true, message: "禁停区不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        boundaryStr: [
          { required: true, message: "边界不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const areaId = this.$route.params && this.$route.params.areaId;
    this.getArea(areaId);
    this.getAreaList();
  },
  methods: {
    // 停车区状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.parkingName + '"吗？').then(function() {
        return changeParkingStatus(row.parkingId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    mapList(data){
      let mapListJson = JSON.stringify(data);
      console.log("mapListJson:"+mapListJson);
      this.form.boundaryStr = mapListJson;
    },
    center(center){
      // console.log("center1111111111:"+center);
      //center根据“,”分割
      let centerArr = center.toString().split(",");
      this.form.longitude = centerArr[0];
      this.form.latitude = centerArr[1];
    },
    /** 查询运营区域详细 */
    getArea(areaId) {
      getArea(areaId).then(response => {
        this.queryParams.areaId = response.data.areaId;
        this.areaLon = response.data.longitude;
        this.areaLat = response.data.latitude;
        this.defaultAreaId = response.data.areaId;
        this.getList();
      });
    },
    /** 查询字典类型列表 */
    getAreaList() {
      getAreaOptionselect().then(response => {
        this.areaOptions = response.data;
      });
    },
    /** 查询禁停区列表 */
    getList() {
      this.loading = true;
      listParking(this.queryParams).then(response => {
        this.parkingList = response.rows;
        this.total = response.total;
        this.defaultAreaId = this.queryParams.areaId;
        getArea(this.defaultAreaId).then(response => {
          this.queryParams.areaId = response.data.areaId;
          this.areaLon = response.data.longitude;
          this.areaLat = response.data.latitude;
        });
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        parkingId: null,
        parkingName: null,
        areaId: null,
        boundaryStr: null,
        longitude: null,
        latitude: null,
        createBy: null,
        createTime: null,
        type: "2"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.parkingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加禁停区";
      this.key++;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const parkingId = row.parkingId || this.ids
      getParking(parkingId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改禁停区";
        this.key++;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.areaId = this.defaultAreaId;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.parkingId != null) {
            updateParking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const parkingIds = row.parkingId || this.ids;
      this.$modal.confirm('是否确认删除禁停区编号为"' + parkingIds + '"的数据项？').then(function() {
        return delParking(parkingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/parking/export', {
        ...this.queryParams
      }, `parking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
