<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" size="small">
      <el-form-item label="车牌号" prop="licenceNumber">
        <el-input
          v-model="queryParams.licenceNumber"
          clearable
          placeholder="请输入车牌号"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="车辆VIN" prop="vehicleVin">
        <el-input
          v-model="queryParams.vehicleVin"
          clearable
          placeholder="请输入车辆VIN"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="vehicleStatus">
        <el-select
          v-model="queryParams.vehicleStatus"
          clearable
          placeholder="车辆状态"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.car_vehicle_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @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
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleBatchDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="vehicleList"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="车牌号" prop="licenceNumber" align="center"/>
      <el-table-column label="车辆颜色" prop="vehicleColor" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_vehicle_color" :value="scope.row.vehicleColor"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆VIN" prop="vehicleVin" align="center"/>
      <el-table-column label="车辆类型" prop="vehicleTypeName" align="center"/>
      <el-table-column label="车辆品牌" prop="vehicleBrand" align="center"/>
      <el-table-column label="车辆型号" prop="vehicleModel" align="center"/>
      <el-table-column label="车辆状态" prop="vehicleStatus" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_vehicle_status" :value="scope.row.vehicleStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template v-if="scope.row.roleId !== 1" slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
          <el-button
            icon="el-icon-odometer"
            size="mini"
            type="text"
            @click="handleInfo(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" label-width="100px">
        <el-form-item label="车牌号" prop="licenceNumber">
          <el-input v-model="form.licenceNumber" placeholder="请输入车牌号"/>
        </el-form-item>
        <el-form-item label="车辆颜色" prop="vehicleColor">
          <el-select
            v-model="form.vehicleColor"
            clearable
            placeholder="车辆颜色"
          >
            <el-option
              v-for="dict in dict.type.car_vehicle_color"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车辆VIN" prop="vehicleVin">
          <el-input v-model="form.vehicleVin" placeholder="请输入车辆VIN"/>
        </el-form-item>
        <el-form-item label="车辆类型" prop="vehicleTypeId">
          <el-select v-model="form.vehicleTypeId" placeholder="请选择车辆类型">
            <el-option v-for="type in vehicleTypeList" :label="type.vehicleTypeName" :value="type.vehicleTypeId"
                       :key="type.vehicleTypeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆品牌" prop="vehicleBrand">
          <el-input v-model="form.vehicleBrand" placeholder="请输入车辆品牌"/>
        </el-form-item>
        <el-form-item label="车辆型号" prop="vehicleModel">
          <el-input v-model="form.vehicleModel" placeholder="请输入车辆型号"/>
        </el-form-item>
        <el-form-item label="车辆行驶证" prop="vehicleLicense">
          <el-input v-model="form.vehicleLicense" placeholder="请输入车辆行驶证"/>
        </el-form-item>
        <el-form-item label="行驶证到期日期" prop="vehicleLicenseDueDate">
          <el-date-picker
            v-model="form.vehicleLicenseDueDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请输入行驶证到期日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆状态" prop="vehicleStatus">
          <el-select
            v-model="form.vehicleStatus"
            clearable
            placeholder="车辆状态"
          >
            <el-option
              v-for="dict in dict.type.car_vehicle_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="电子围栏组" prop="fenceGroupId">
          <el-input v-model="form.fenceGroupId" 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 {
  showList,
  save,
  findById,
  deleted,
  update,
  getVehicleType,
  batchDelete,
} from "../../../api/car/vehicle";

export default {
  name: "Vehicle",
  dicts: ['car_vehicle_status', 'car_vehicle_color'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆表格数据
      vehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        licenceNumber: undefined,
        vehicleVin: undefined,
        vehicleStatus: undefined
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 车辆类型列表
      vehicleTypeList: [],
      // 多选框选中数据
      vehicleIds: [],
    };
  },
  created() {
    this.getList();
    this.getVehicleTypeList();
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      showList(this.queryParams).then(response => {
          this.vehicleList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 查询角色列表 */
    getVehicleTypeList() {
      getVehicleType().then(response => {
          this.vehicleTypeList = response.data;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleId: undefined,
        licenceNumber: undefined,
        vehicleColor: undefined,
        vehicleVin: undefined,
        vehicleTypeId: undefined,
        vehicleBrand: undefined,
        vehicleModel: undefined,
        vehicleLicense: undefined,
        vehicleLicenseDueDate: undefined,
        vehicleStatus: undefined,
        fenceGroupId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "录入车辆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vehicleId = row.vehicleId
      findById(vehicleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleId != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            save(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleId = row.vehicleId;
      this.$modal.confirm('是否确认删除编号为"' + vehicleId + '"的数据项？').then(function () {
        return deleted(vehicleId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 多选框选中数据 */
    handleSelectionChange(val) {
      this.vehicleIds = [];
      for (let key in val) {
        this.vehicleIds.push(val[key].vehicleId);
      }
    },
    /** 批量删除汽车 */
    handleBatchDelete() {
      console.log(this.vehicleIds);
      batchDelete(this.vehicleIds).then(response => {
        this.$modal.msgSuccess("批量删除成功");
        this.getList();
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('car/vehicleManage/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    },
    /** 跳转到详情页面 */
    handleInfo(row){
      this.$router.push({
        path:'vehicleInfo', query:{vehicleId: row.vehicleId}
      })
    },
  }
};
</script>
