<template>
  <div class="table-container">
    <!-- 查询表单 -->
    <div class="query-form">
      <el-input
        v-model="queryParams.carVin"
        placeholder="输入 VIN 码进行查询"
        style="width: 200px; margin-right: 10px;"
      />
      <el-button type="primary" @click="filterData">查询</el-button>
      <el-button type="primary" @click="resetQuery">重置</el-button>
    </div>
    <el-button type="primary" @click="addItem">添加</el-button>
    <div class="table-container">
    <el-table :data="listDate" border stripe style="width: 100%;" :show-header="true">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="VIN码" prop="carVin" align="center"/>
      <el-table-column label="车辆编号" prop="carPlate" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="车辆品牌" prop="carBrand" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="车辆型号" prop="carModel" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="车辆车型(如客车,卡车,公交车等)" prop="carTypeId" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="车辆电机厂商" prop="carMotorManufacturer" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="电机型号" prop="carMotorModel" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="车辆电池厂商" prop="carBatteryManufacturer" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="电池型号" prop="carBatteryModel" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="围栏组编码" prop="groupCode" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="在线状态" prop="state" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_car_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column width="150" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-takeaway-box"
            @click="cs(scope.row)"
          >自己測試用*
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-takeaway-box"
            @click="listItem(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-takeaway-box"
            @click="toVehicleData(scope.row)"
          >实时数据
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="editItem(scope.row)"
          >
            修改
          </el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delItem(scope.row)"
          >
            删除
          </el-button>

          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="findStrategy(scope.row)"
          >
            配置策略
          </el-button>
        </template>
      </el-table-column>
    </el-table>


      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />


      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="vin码" prop="carVin">
            <el-input v-model="form.carVin" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="车辆编号" prop="carPlate">
            <el-input v-model="form.carPlate" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="车辆型号" prop="carModel">
            <el-input v-model="form.carModel" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="车辆品牌" prop="carBrand">
            <el-input v-model="form.carBrand" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="车辆电机厂商" prop="carMotorManufacturer">
            <el-input v-model="form.carMotorManufacturer" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="电池型号" prop="carBatteryModel">
            <el-input v-model="form.carBatteryModel" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="电机型号" prop="carMotorModel">
            <el-input v-model="form.carMotorModel" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="电子围栏" prop="groupCode">
            <el-input v-model="form.groupCode" placeholder="请输入"/>
          </el-form-item>
          <el-form-item label="车辆状态" prop="state">
            <el-radio-group v-model="form.state">
              <el-radio
                v-for="dict in dict.type.sys_car_status"
                :key="dict.value"
                :label="dict.value"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
          </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>
    <el-dialog :visible.sync="strategy">

      <el-select v-model="carStrategy.strategyId" placeholder="请选择">
        <el-option
          v-for="item in strategyList"
          :key="item.strategyName"
          :label="item.strategyName"
          :value="item.strategyName">
        </el-option>
      </el-select>

      <el-button @click="addCarStrategy()">
        确定
      </el-button>


    </el-dialog>
  </div>
</template>

<script>

import {listManager , getManagerId , addManager,updateManager,delManager} from "@/api/car/ListManager";
// import { findStrategyList } from '@/api/platform/strategy'


  export default {
    dicts: ['sys_car_status'],
    components: {},
    props: {},
    data() {
      return {
        carStrategy:{
          sysCarId:'',
          strategyId:''
        },
        strategy:false,
        strategyList:[],
        total: 0,
        open: false,
        form: {},
        title: "",
        listDate: [],
        rules: {
          carVin: [
            {required: true, message: "vin不能为空", trigger: "blur"}
          ],
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          plateNumber: undefined,
          carVin: undefined,
          state: undefined,
        },
      };
    },
    methods: {
      cs(row){
        const carVin = row.carVin;
        alert(carVin);
        this.$router.push({
          path: `/vehicleManagement/record`,
          query: { carVin }
        });
      },
      //配置策略的前台
      findStrategy(row){
        findStrategyList(3).then(res=>{
          this.strategyList=res.data.rows;
        })
            this.carStrategy.sysCarId=row.id;
            this.strategy=true;
      },

      toVehicleData(row){
        const carVin = row.carVin;
        alert(carVin);
        this.$router.push({
          path: `/vehicleManagement/trajectory`,
          query: { carVin }
        });
      },
      listItem(row) {
        const carVin = row.carVin;
        alert(carVin);
        this.$router.push({
          path: `/vehicleManagement/test`,
          query: { carVin }
        });
      },
      delItem(row){
        const id = row.id || this.ids;
        this.$modal.confirm('是否确认删除').then(function() {
          return delManager(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateManager(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addManager(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      cancel(){
        this.open = false;
        this.reset();
      },
      editItem(row){
        this.reset();
        const id = row.id || this.ids
        getManagerId(id).then(response => {
          this.form = response.data;
          this.open = true;
        });
      },
      addItem(){
        this.reset();
        this.open = true;
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          remark: undefined
        };
        this.resetForm("form");
      },
      resetQuery() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          plateNumber: '',
          vin: '',
          onlineStatus: undefined,
        };
        this.getList();
      },
      filterData() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      getList() {
        listManager(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.listDate = response.data.rows;
            this.total = response.data.total;
          }
        );
      },
    },
    //生命周期 - 创建完成（可以访问当前this实例）",
    created() {
      this.getList();
    },
    beforeCreate() {
    }, //生命周期 - 创建之前",
    beforeUpdate() {
    }, //生命周期 - 更新之前",
    updated() {
    }, //生命周期 - 更新之后",
    beforeDestroy() {
    }, //生命周期 - 销毁之前",
    destroyed() {
    }, //生命周期 - 销毁完成",
    activated() {
    } //如果页面有keep-alive缓存功能，这个函数会触发",
  };
</script>
<style scoped>
.el-button--primary {
  border-radius: 10px; /* 修改边框半径 */
  background-color: #409eff; /* 修改背景色 */
  border-color: #409eff; /* 修改边框颜色 */
  &:hover {
    background-color: #66b1ff; /* 修改悬停时的背景色 */
    border-color: #66b1ff; /* 修改悬停时的边框颜色 */
  }
}

.el-button--danger {
  border-radius: 10px; /* 修改边框半径 */
  background-color: #f56c6c; /* 修改背景色 */
  border-color: #f56c6c; /* 修改边框颜色 */
  &:hover {
    background-color: #f78989; /* 修改悬停时的背景色 */
    border-color: #f78989; /* 修改悬停时的边框颜色 */
  }
}
.table-container {
  padding: 20px;
}

.query-form {
  margin-bottom: 20px;
}
.table-container .el-table__body-wrapper tbody tr:hover {
  background-color: #d8b7ff; /* 紫色背景 */
}
.table-container {
  padding: 20px;
  background-color: #f5f5f5;
}


</style>
