<template>
  <div>
    <h1>车辆管理页面</h1>
    <!--    {{ vehicleList }}-->

    <!--    列表表单-->
    <el-form ref="form" :model="vehicleListParams" label-width="80px">
      <el-form-item label="类型">
        <el-select v-model="vehicleListParams.vehicleType" clearable placeholder="车辆类型" @change="getVehicleList">
          <el-option v-for="item in vehicleTypes" :key="item.vehicleTypeId" :label="item.vehicleTypeName"
                     :value="item.vehicleTypeId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="vehicleListParams.vehicleState" clearable placeholder="车辆状态" @change="getVehicleList">
          <el-option key="1" label="在线" value="1"></el-option>
          <el-option key="0" label="离线" value="0"></el-option>
        </el-select>
      </el-form-item>
    </el-form>


    <el-button type="primary" icon="el-icon-edit" @click="toInsert">新增车辆</el-button>


    <!--    列表-->
    <el-table :data="vehicleList" style="width: 100%" border v-loading="loading">
      <el-table-column label="车辆id" width="90" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.vehicleId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆类型" width="90" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.vehicleTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电机厂商" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.motorManufacturer }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电池厂商" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.batteryManufacturer }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电机编号" width="180" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.motorNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电池编号" width="180" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.batteryNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="vin码" width="180" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.vin }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆状态" width="120" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px" v-if="1==scope.row.vehicleState">
            <el-tag type="success">在线</el-tag>
          </span>
          <span style="margin-left: 10px" v-if="0==scope.row.vehicleState">
            <el-tag type="danger">离线</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="toEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteById(scope.row.vehicleId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="vehicleListParams.pageNum"
      :page-sizes="[5,7,10,20]"
      :page-size="vehicleListParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!--    编辑对话框-->
    <el-dialog :title=title :visible.sync="dialogEdit">
      <el-form :model="vehicle">
        <el-form-item label="车辆类型" :label-width="formLabelWidth">
          <el-select v-model="vehicle.vehicleType" placeholder="车辆类型">
            <el-option v-for="item in vehicleTypes" :key="item.vehicleTypeId" :label="item.vehicleTypeName"
                       :value="item.vehicleTypeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电机厂商" :label-width="formLabelWidth">
          <el-input v-model="vehicle.motorManufacturer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电池厂商" :label-width="formLabelWidth">
          <el-input v-model="vehicle.batteryManufacturer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电机编号" :label-width="formLabelWidth">
          <el-input v-model="vehicle.motorNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电池编号" :label-width="formLabelWidth">
          <el-input v-model="vehicle.batteryNumber" autocomplete="off"></el-input>
        </el-form-item>
        <!--        标签复选框 需要在点击编辑框的时候查询当前车说绑定的标识-->
        <el-checkbox-group v-model="vehicle.logoIds">
          <el-checkbox :label=1>标识1</el-checkbox>
          <el-checkbox :label=2>标识2</el-checkbox>
          <el-checkbox :label=3>标识3</el-checkbox>
        </el-checkbox-group>

      </el-form>
      <!--      {{ vehicle }}-->
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset()">取 消</el-button>
        <el-button type="primary" @click="editById">确 定</el-button>
      </div>
    </el-dialog>

    <!--    新增对话框-->
    <el-dialog :title=title :visible.sync="dialogInsert">
      <el-form :model="vehicle">
        <el-form-item label="车辆类型" :label-width="formLabelWidth">
          <el-select v-model="vehicle.vehicleType" placeholder="车辆类型">
            <el-option v-for="item in vehicleTypes" :key="item.vehicleTypeId" :label="item.vehicleTypeName"
                       :value="item.vehicleTypeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电机厂商" :label-width="formLabelWidth">
          <el-input v-model="vehicle.motorManufacturer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电池厂商" :label-width="formLabelWidth">
          <el-input v-model="vehicle.batteryManufacturer" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电机编号" :label-width="formLabelWidth">
          <el-input v-model="vehicle.motorNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电池编号" :label-width="formLabelWidth">
          <el-input v-model="vehicle.batteryNumber" autocomplete="off"></el-input>
        </el-form-item>

        <!--        标签复选框-->
        <el-checkbox-group v-model="vehicle.logoIds">
          <el-checkbox :label=1>标识1</el-checkbox>
          <el-checkbox :label=2>标识2</el-checkbox>
          <el-checkbox :label=3>标识3</el-checkbox>
        </el-checkbox-group>
      </el-form>
      <!--      {{ vehicle }}-->
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset()">取 消</el-button>
        <el-button type="primary" @click="insert">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {deleteById, editById, getBindLogoByVehicleId, getVehicleTypes, insert, list} from "@/api/couplet/vehicle";

export default {
  //import引入的组件需要注入到对象中才能使用"
  components: {},
  props: {},
  data() {
    //这里存放数据"
    return {
      //加载
      loading: true,
      //车辆列表
      vehicleList: [],

      //总页数
      total: 0,

      //车辆类型
      vehicleTypes: [],

      //查询列表的入参
      vehicleListParams: {
        //车辆类型
        vehicleType: '',
        //车辆状态 0离线 1在线
        vehicleState: '',

        pageNum: 1,
        pageSize: 5,


      },

      //编辑、新增车辆的入参
      vehicle: {
        //车辆id
        vehicleId: '',

        //车辆类型
        vehicleType: '',

        //电机厂商
        motorManufacturer: '',

        //电池厂商
        batteryManufacturer: '',

        //电机编号
        motorNumber: '',

        //电池编号
        batteryNumber: '',

        //车辆绑定标识
        logoIds: [],


      },

      //对话框的参数
      formLabelWidth: '120px',
      dialogEdit: false,
      dialogInsert: false,
      // 弹出层标题
      title: ""

    };
  },
  //计算属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    //获取车辆列表
    getVehicleList() {
      list(this.vehicleListParams).then(
        res => {
          if (res.code == 200) {
            this.vehicleList = res.data.list;
            this.total = res.data.total;
            this.loading = false;
          } else {
            this.$message.error("列表查询异常");
          }

        }
      )
    },

    //分页
    handleSizeChange(val) {
      this.vehicleListParams.pageSize = val;
      this.getVehicleList();
    },
    handleCurrentChange(val) {
      this.vehicleListParams.pageNum = val;
      this.getVehicleList();
    },

    //获取车辆类型
    getVehicleTypes() {
      getVehicleTypes().then(
        res => {
          this.vehicleTypes = res.data;
        }
      )
    },

    //根据车辆id进行删除
    deleteById(vehicleId) {
      this.$confirm('此操作将永久删除该数据，是否继续？', "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(vehicleId).then(
          res => {
            if (res.code == 200) {
              //刷新列表
              this.getVehicleList();
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
            } else {
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            }
          }
        )
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    //编辑按钮
    toEdit(row) {
      this.reset()

      //将各个属性进行赋值
      this.vehicle.vehicleId = row.vehicleId;
      this.vehicle.vehicleType = row.vehicleType;
      this.vehicle.motorManufacturer = row.motorManufacturer;
      this.vehicle.batteryManufacturer = row.batteryManufacturer;
      this.vehicle.motorNumber = row.motorNumber;
      this.vehicle.batteryNumber = row.batteryNumber;
      //查询与当前车辆绑定 的标识
      getBindLogoByVehicleId(row.vehicleId).then(
        res => {
          //再赋值
          this.vehicle.logoIds = res.data;
        }
      )

      this.title = "编辑车辆"
      this.dialogEdit = true;
    },
    //重置对话框表单
    reset() {
      this.dialogEdit = false
      this.dialogInsert = false
      this.vehicle = {
        //车辆id
        vehicleId: '',
        //车辆类型
        vehicleType: '',
        //电机厂商
        motorManufacturer: '',
        //电池厂商
        batteryManufacturer: '',
        //电机编号
        motorNumber: '',
        //电池编号
        batteryNumber: '',
        //车辆绑定标识
        logoIds: [],
      }
      // this.$message.info("已取消")
    },
    //确认编辑
    editById() {
      editById(this.vehicle).then(
        res => {
          if (res.code == 200) {
            this.dialogEdit = false;
            this.getVehicleList();
            this.$message.success("编辑成功");
          } else {
            this.$message.error("编辑失败");
          }
        }
      );
    },


    //新增按钮
    toInsert() {
      this.reset()
      this.title = "新增车辆"
      this.dialogInsert = true;
    },
    //确认插入
    insert() {
      insert(this.vehicle).then(
        res => {
          if (res.code == 200) {
            this.dialogInsert = false;
            this.getVehicleList();
            this.$message.success("新增成功");
          } else {
            this.$message.error("新增失败");
          }
        }
      )
    },


  },
  //生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    //获取车辆列表
    this.getVehicleList()

    //获取车辆类型
    this.getVehicleTypes()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前",
  beforeMount() {
  }, //生命周期 - 挂载之前",
  beforeUpdate() {
  }, //生命周期 - 更新之前",
  updated() {
  }, //生命周期 - 更新之后",
  beforeDestroy() {
  }, //生命周期 - 销毁之前",
  destroyed() {
  }, //生命周期 - 销毁完成",
  activated() {
  } //如果页面有keep-alive缓存功能，这个函数会触发",
};
</script>
<style scoped>

</style>
