<template>
  <div>
    <div v-show="!isShow">
      <div id="container" ></div>
      <el-button type="primary" @click="isShowAll">返回</el-button>
    </div>

    <div v-show="!isList">
      <el-form :model="form" ref="myForm">
        <table style="width: 100%">
          <tr>
            <td>车辆VIN:</td>
            <td>
              <el-input v-model="form.carVin" size="medium" style="width: 100%;"></el-input>
            </td>

            <td>车辆名称:</td>
            <td>
              <el-input v-model="form.carName" size="medium" style="width: 100%;"></el-input>
            </td>

            <td>车辆类型:</td>
            <td>
              <el-select v-model="form.carTypeId" placeholder="请选择车辆类型" style="width: 100%">
                <el-option v-for="carType in carTypes" :label="carType.carTypeName" :value="carType.carTypeId" :key="carType.carTypeId"></el-option>
              </el-select>
            </td>

            <td>电子围栏:</td>
            <td>
              <el-input v-model="form.fenceName" size="medium" style="width: 100%;"></el-input>
            </td>

            <td>车辆状态:</td>
            <td>
              <el-select v-model="form.carStatus" placeholder="请选择车辆状态" style="width: 100%">
                <el-option value="1">在线</el-option>
                <el-option value="0">离线</el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td>车辆品牌:</td>
            <td><el-input v-model="form.carBrand" size="medium" style="width: 100%;"></el-input></td>
            <td>
              <el-button type="primary" @click="findForm" size="mini">搜索</el-button>
            </td>
            <td>
              <el-button @click="resetForm" size="mini">重置</el-button>
            </td>
          </tr>
        </table>
      </el-form>
      <br><br>
      <div style="text-align: right;">
        <button type="submit" @click="refreshTable">刷新</button>
      </div>

      <el-table :data="carList"
                element-loading-text="Loading"
                border
                fit
                highlight-current-row>
        <el-table-column label="车辆编号">
          <template slot-scope="scope">
            {{scope.row.carId}}
          </template>
        </el-table-column>
        <el-table-column label="车辆VIN">
          <template slot-scope="scope">
            {{scope.row.carVin}}
          </template>
        </el-table-column>
        <el-table-column label="车辆名称">
          <template slot-scope="scope">
            {{scope.row.carName}}
          </template>
        </el-table-column>
        <el-table-column label="车辆品牌">
          <template slot-scope="scope">
            {{scope.row.carBrand}}
          </template>
        </el-table-column>
        <el-table-column label="车辆类型">
          <template slot-scope="scope">
            {{scope.row.carTypeName}}
          </template>
        </el-table-column>
        <el-table-column label="电子围栏">
          <template slot-scope="scope">
            {{scope.row.fenceName}}
          </template>
        </el-table-column>
        <el-table-column label="车辆状态">
          <template slot-scope="scope">
            <div v-if="scope.row.carStatus===0">离线</div>
            <div v-if="scope.row.carStatus===1">在线</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="updateCarList(scope.row)">修改</el-button>
            <el-button type="primary" @click="delCar(scope.row.carVin)">删除</el-button>
            <el-button type="primary" @click="realTimeTrajectory">实时轨迹</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="text-align: right;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.form.pageNum"
          :page-sizes="[3, 5, 7, 10]"
          :page-size="this.form.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

      <el-dialog :visible.sync="dialogVisible" width="30%">
        <el-form :model="carForm">
          <el-form-item label="车辆编号">
            <el-input v-model="carForm.carId" readonly></el-input>
          </el-form-item>

          <el-form-item label="车辆VIN">
            <el-input v-model="carForm.carVin" readonly></el-input>
          </el-form-item>

          <el-form-item label="车辆名称">
            <el-input v-model="carForm.carName" readonly placeholder="请输入车辆名称"></el-input>
          </el-form-item>

          <el-form-item label="车辆品牌">
            <el-input v-model="carForm.carBrand" readonly placeholder="请输入车辆品牌"></el-input>
          </el-form-item>

          <el-form-item label="车辆类型">
            <el-select v-model="carForm.carTypeId" placeholder="请选择车辆类型">
              <el-option v-for="carType in carTypes" :label="carType.carTypeName" :value="carType.carTypeId" :key="carType.carTypeId"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="电子围栏">
            <el-select v-model="carForm.fenceId" placeholder="请选择围栏">
              <el-option v-for="fence in fences" :label="fence.fenceName" :value="fence.fenceId" :key="fence.fenceId"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateCar">确定修改</el-button>
        </span>
      </el-dialog>
      <el-button type="primary" @click="tapInsert">添加车辆</el-button>
      <el-dialog :visible.sync="dialogVisibleTwo" width="30%">
        <el-form :model="carInsert">
          <el-form-item label="车辆VIN">
            <el-input v-model="carInsert.carVin" placeholder="请输入车辆VIN"></el-input>
          </el-form-item>

          <el-form-item label="车辆名称">
            <el-input v-model="carInsert.carName" placeholder="请输入车辆名称"></el-input>
          </el-form-item>

          <el-form-item label="车辆品牌">
            <el-input v-model="carInsert.carBrand" placeholder="请输入车辆品牌"></el-input>
          </el-form-item>

          <el-form-item label="车辆类型">
            <el-select v-model="carInsert.carTypeId" placeholder="请选择车辆类型">
              <el-option v-for="carType in carTypes" :label="carType.carTypeName" :value="carType.carTypeId" :key="carType.carTypeId"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="电子围栏">
            <el-select v-model="carInsert.fenceId" placeholder="请选择围栏">
              <el-option v-for="fence in fences" :label="fence.fenceName" :value="fence.fenceId" :key="fence.fenceId"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleTwo = false">取 消</el-button>
          <el-button type="primary" @click="insertCar">确定添加</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader';
import {delCar, getCarList, insertCar, listCarType, listFence, updateCar} from "@/api/table";
export default {
  name: 'AMapAnimation',
  props: {
    equipmentId: String,
  },
  beforeDestroy() {
    // this.map && this.map.destroy();//离开页面，销毁掉
  },
  data() {
    return {
      carList:[],
      total:0,
      form:{
        pageNum:1,
        pageSize:10,
        carVin:null,
        carName:null,
        carBrand:null,
        carTypeId:null,
        fenceName:null,
        carStatus:null,
      },
      carForm:{
        carId:null,
        carVin:null,
        carName:null,
        carBrand:null,
        carTypeId:null,
        fenceId:null,
      },
      carInsert:{
        carVin:null,
        carName:null,
        carBrand:null,
        carTypeId:null,
        fenceId:null,
      },
      carTypes:[],
      carVin:null,
      car:null,
      isShow:true,
      isList:false,
      dialogVisible:false,
      dialogVisibleTwo:false,
      fences:[],
      duration:2000,
      AMap: null,
      map: null,
      marker: null,
      lineArr: [
        [116.478935,39.997761],
        [116.478939,39.997825],
        [116.478912,39.998549],
        [116.478912,39.998549],
        [116.478998,39.998555],
        [116.478998,39.998555],
        [116.479282,39.99856],
        [116.479658,39.998528],
        [116.480151,39.998453],
        [116.480784,39.998302],
        [116.480784,39.998302],
        [116.481149,39.998184],
        [116.481573,39.997997],
        [116.481863,39.997846],
        [116.482072,39.997718],
        [116.482362,39.997718],
        [116.483633,39.998935],
        [116.48367,39.998968],
        [116.484648,39.999861]],
    };
  },
  mounted() {

  },
  created() {
    this.getList()
    this.listCarType()
    this.listFence()
  },
  methods: {
    /*重置表单*/
    resetForm() {
      this.form = { // 这里将form对象重置为空对象或者你定义的初始值
        carVin: '', // 将车辆VIN字段重置为空
        carName: '', // 将车辆名称字段重置为空
        carTypeId: null, // 将车辆类型字段重置为null或者你定义的初始值
        fenceName: '', // 将电子围栏字段重置为空
        carStatus: '', // 将车辆状态字段重置为空
        carBrand: '', // 将车辆品牌字段重置为空
      };
      this.$refs.myForm.resetFields(); // 重置表单验证状态
    },
    isShowAll(){
      this.isShow = true
      this.isList = false
      // this.map.destroy();//离开页面，销毁掉
      location.reload()
    },
    insertCar(){
      insertCar(this.carInsert).then(res=>{
        if(res.code === 200){
          this.$message.success("添加成功！")
          location.reload()
        }
      })
    },
    tapInsert(){
      this.dialogVisibleTwo = true
    },
    updateCarList(car){
      this.dialogVisible = true
      this.carForm = car
      console.log(this.carForm)
    },
    /*电子围栏列表*/
    listFence(){
      listFence().then(res=>{
        this.fences = res.data
      })
    },
    /*地图展示*/
    realTimeTrajectory(){
      this.isShow = false
      this.isList = true
      this.initMap()
      this.startAnimation()
    },
    /*刷新*/
    refreshTable(){
      this.getList()
    },
    handleSizeChange(val) {
      this.form.pageSize=val
      this.getList()
    },
    handleCurrentChange(val) {
      this.form.pageNum=val
      this.getList()
    },
    /*车辆列表*/
    getList(){
      getCarList(this.form).then(response => {
        console.log(response)
        this.carList = response.data.list
        this.total = response.data.total
      })
    },
    delCar(carVin){
      this.carVin = carVin
      delCar(this.carVin).then(res=>{
        if(res.code === 200){
          this.$message.success("删除成功！")
          location.reload()
        }
      })
    },
    updateCar(){
      updateCar(this.carForm).then(res=>{
        if(res.code === 200){
          this.$message.success("修改成功！")
          location.reload()
        }
      })
    },
    /*表单查询*/
    findForm(){
      this.getList()
    },
    /*查询车辆类型表*/
    listCarType(){
      listCarType().then(res=>{
        this.carTypes = res.data
      })
    },
    initMap() {
      AMapLoader.load({
        key: "0b5f26b194361cbc9571101bc2856962", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: [
          "AMap.Scale",
          "AMap.HawkEye",
          "AMap.ToolBar",
          "AMap.AutoComplete",
          "AMap.PlaceSearch",
          "AMap.ControlBar",
          "AMap.MouseTool",
          "AMap.DragRoute",
          "AMap.MoveAnimation"], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      }).then((AMap) => {
        // 获取到作为地图容器的DOM元素，创建地图实例
        this.map = new AMap.Map("container", { //设置地图容器id
          resizeEnable: true,
          viewMode: "3D", // 使用3D视图
          zoomEnable: true, // 地图是否可缩放，默认值为true
          dragEnable: true, // 地图是否可通过鼠标拖拽平移，默认为true
          doubleClickZoom: true, // 地图是否可通过双击鼠标放大地图，默认为true
          zoom: 17, //初始化地图级别
          center: this.lineArr[0], // 初始化中心点坐标 北京
          // mapStyle: "amap://styles/darkblue", // 设置颜色底层
        })

        this.marker = new AMap.Marker({
          position: this.lineArr[0],
          icon: "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png",
          offset: new AMap.Pixel(-13, -26),
        });
        this.map.add(this.marker)

        // 绘制轨迹
        this.polyline = new AMap.Polyline({
          path: this.lineArr,
          showDir: true,
          strokeColor: "#28F",  //线颜色
          // strokeOpacity: 1,     //线透明度
          strokeWeight: 6,      //线宽
          // strokeStyle: "solid"  //线样式
        });
        this.map.add(this.polyline)

        // 走过的路径
        this.passedPolyline = new AMap.Polyline({
          strokeColor: "#AF5",  //线颜色
          strokeWeight: 6,      //线宽
        });
        this.map.add(this.passedPolyline)

        // 监听marker移动
        this.marker.on('moving', (e) => {
          this.passedPolyline.setPath(e.passedPath); // 设置路径样式
          this.map.setCenter(e.target.getPosition(), true) // 设置地图中心点
        });

        this.map.setFitView(); // 根据覆盖物自适应展示地图

      }).catch(e => {
        console.log(e)
      })
    },
    startAnimation () {
      setTimeout(()=>{
        AMap.plugin('AMap.MoveAnimation', () => {
          console.log('开始回放')
          this.marker.moveAlong(this.lineArr, {
            // 每一段的时长
            duration: this.duration,//可根据实际采集时间间隔设置
            // JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
            autoRotation: true,
          });
        })
      },2000)
    },
  }
}
</script>
<style scoped lang="scss">
#container{
  padding:0px;
  margin: 0px;
  width: 100%;
  height: 800px;
}
html {
  font-size: 12px;
}
.amap-copyright{
  box-sizing: content-box
}
* {
  box-sizing: border-box
}
.input-textarea{
  color:grey;
  height:8em;
  overflow:auto;
  border-radius:0.4rem;
  border:1px solid #ced4da;
  margin-bottom:1rem;
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
  line-height: 1.5;
  font-weight: 300;
  color: #111213;
}

hr {
  margin: 0.5rem 0;
  box-sizing: content-box;
  height: 0;
  overflow: visible;
  border: 0;
  border-top: 1px solid rgba(0, 0, 0, .1)
}

p {
  margin-top: 0;
  margin-bottom: 0;
}

label {
  display: inline-block;
  margin-bottom: 0.4rem;
}

label, .btn {
  margin-left: 0;
  font-size: 1rem;
}

button, input, select {
  margin: 0;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  overflow: visible;
  text-transform: none
}

[type=button]::-moz-focus-inner, [type=reset]::-moz-focus-inner, [type=submit]::-moz-focus-inner, button::-moz-focus-inner {
  padding: 0;
  border-style: none;
}

input[type=checkbox], input[type=radio] {
  box-sizing: border-box;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  padding: 0;
  margin: 0 0.5rem 0 0;
}

h4 {
  font-family: inherit;
  line-height: 1.8;
  font-weight: 300;
  color: inherit;
  font-size: 1.1rem;
  margin-top: 0;
  margin-bottom: .5rem
}

.btn {
  display: inline-block;
  font-weight: 400;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  border: 1px solid transparent;
  transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
  background-color: transparent;
  background-image: none;
  color: #25A5F7;
  border-color: #25A5F7;
  padding: .25rem .5rem;
  line-height: 1.5;
  border-radius: 1rem;
  -webkit-appearance: button;
  cursor:pointer;
}

.btn:hover {
  color: #fff;
  background-color: #25A5F7;
  border-color: #25A5F7
}

.btn:hover {
  text-decoration: none
}

.input-item {
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -ms-flex-align: center;
  align-items: center;
  width: 100%;
  height: 3rem;
}

.input-item:last-child {
  margin-bottom: 0;
}

.input-item>select, .input-item>input[type=text], .input-item>input[type=date] {
  position: relative;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  width: 1%;
  margin-bottom: 0;
}

.input-item>select:not(:last-child), .input-item>input[type=text]:not(:last-child), .input-item>input[type=date]:not(:last-child) {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0
}

.input-item>select:not(:first-child), .input-item>input[type=text]:not(:first-child), .input-item>input[type=date]:not(:first-child) {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0
}

.input-item-prepend {
  margin-right: -1px;
}

.input-item-text, input[type=text],input[type=date], select {
  height: calc(2.2rem + 2px);
}

.input-item-text {
  width: 6rem;
  text-align: justify;
  padding: 0.4rem 0.7rem;
  display: inline-block;
  text-justify: distribute-all-lines;
  /*ie6-8*/
  text-align-last: justify;
  /* ie9*/
  -moz-text-align-last: justify;
  /*ff*/
  -webkit-text-align-last: justify;
  /*chrome 20+*/
  -ms-flex-align: center;
  align-items: center;
  margin-bottom: 0;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #495057;
  text-align: center;
  white-space: nowrap;
  background-color: #e9ecef;
  border: 1px solid #ced4da;
  border-radius: .25rem;
  border-bottom-right-radius: 0;
  border-top-right-radius: 0;
}

.input-item-text input[type=checkbox], .input-item-text input[type=radio] {
  margin-top: 0
}

.input-card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-color: #fff;
  background-clip: border-box;
  border-radius: .25rem;
  width: 22rem;
  border-width: 0;
  border-radius: 0.4rem;
  box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  -ms-flex: 1 1 auto;
  flex: 1 1 auto;
  padding: 0.75rem 1.25rem;
}

.input-text {
  line-height: 2rem;
  margin-right: 2rem;
}

.info hr {
  margin-right: 0;
  margin-left: 0;
  border-top-color: grey;
}

.info {
  padding: .75rem 1.25rem;
  margin-bottom: 1rem;
  border-radius: .25rem;
  position: fixed;
  top: 1rem;
  background-color: white;
  width: auto;
  min-width: 22rem;
  border-width: 0;
  right: 1rem;
  box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
}

.code {
  left: 1.5rem;
  right: 1.5rem;
  top: 1.5rem;
  bottom: 1.5rem;
  overflow: auto;
  margin-bottom: 0rem;
}

.code .btn {
  top: 1rem;
  position: absolute;
  right: 1rem;
}

.code .result {
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 0.5rem;
  padding: 1rem;
  bottom: 1rem;
  position: absolute;
  top: 5.5rem;
  right: 1rem;
  left: 1rem;
  overflow: auto;
}

.code .status {
  color: #80adff;
  display: inline-block;
  font-size: 14px;
}

.code h4 {
  display: inline-block;
  max-width: 20rem;
  margin-right: 1rem;
  margin-bottom: 1rem;
}

select, input[type=text], input[type=date] {
  display: inline-block;
  width: 100%;
  padding: .375rem 1.75rem .375rem .75rem;
  line-height: 1.5;
  color: #495057;
  vertical-align: middle;
  background: #fff url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4 5'%3E%3Cpath fill='%23343a40' d='M2 0L0 2h4zm0 5L0 3h4z'/%3E%3C/svg%3E") no-repeat right .75rem center;
  background-size: 8px 10px;
  border: 1px solid #ced4da;
  border-radius: .25rem;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none
}

input[type=text],input[type=date] {
  background: #fff;
  padding: .375rem .75rem;
}

select:focus, input[type=text]:focus, input[type=date]:focus {
  border-color: #80bdff;
  outline: 0;
  box-shadow: 0 0 0 .1rem rgba(128, 189, 255, .1)
}

.btn:focus {
  outline: 0;
  box-shadow: none;
}

select:focus::-ms-value, input[type=text]:focus::-ms-value,input[type=date]:focus::-ms-value {
  color: #495057;
  background-color: #fff
}


/* native toastr */
.native-toast {
  position: fixed;
  background-color: rgba(50, 50, 50, .8);
  border-radius: 33px;
  color: white;
  left: 50%;
  text-align: center;
  padding: 6px 12px;
  opacity: 0;
  z-index: 99999;
  transition: transform .25s, opacity .25s, top .25s;
  box-sizing: border-box;
}

.native-toast-bottom {
  bottom: 50px;
  -ms-transform: translateX(-50%) translateY(50px);
  transform: translateX(-50%) translateY(50px)
}

.native-toast-bottom.native-toast-shown {
  opacity: 1;
  -ms-transform: translateX(-50%) translateY(0);
  transform: translateX(-50%) translateY(0);
}

.native-toast-bottom.native-toast-edge {
  bottom: 0;
}

.native-toast-top {
  top: 50px;
  -ms-transform: translateX(-50%) translateY(-50px);
  transform: translateX(-50%) translateY(-50px)
}

.native-toast-top.native-toast-shown {
  opacity: 1;
  -ms-transform: translateX(-50%) translateY(0);
  transform: translateX(-50%) translateY(0);
}

.native-toast-top.native-toast-edge {
  top: 0;
}

.native-toast-center {
  top: 0;
  -ms-transform: translateX(-50%) translateY(-50px);
  transform: translateX(-50%) translateY(-50px)
}

.native-toast-center.native-toast-shown {
  opacity: 1;
  top: 50%;
  -ms-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
}

.native-toast-edge {
  border-radius: 0;
  width: 100%;
  text-align: left;
}

@media screen and (min-width: 40rem) {
  .native-toast:not(.native-toast-edge) {
    max-width: 18rem;
  }
}

/*
  max-width does not seem to work in small screen?
*/

/*@media screen and (max-width: 768px) {
  .native-toast:not(.native-toast-edge) {
    max-width: 400px;
  }
}

@media screen and (max-width: 468px) {
  .native-toast:not(.native-toast-edge) {
    max-width: 300px;
  }
}*/

/* types */

.native-toast-error {
  background-color: #d92727;
  color: white;
}

.native-toast-success {
  background-color: #62a465;
  color: white;
}

.native-toast-warning {
  background-color: #fdaf17;
  color: white;
}

.native-toast-info {
  background-color: #5060ba;
  color: white;
}

[class^="native-toast-icon-"] {
  vertical-align: middle;
  margin-right: 8px
}

[class^="native-toast-icon-"] svg {
  width: 16px;
  height: 16px;
}
</style>

