<template>
  <div class="app-container">
    <span v-if="enterprise == null">

    </span>
    <span v-else>
      <span v-if="enterprise.authentication != 1 ">
      <h2>企业信息尚未完善，请先补充完善后通知管理员审核</h2>
        <!-- 添加或修改企业对话框 -->
    <el-dialog title="补充企业信息" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="enterprise" :rules="enterprise" label-width="80px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="enterprise.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="法定代表人" prop="legalPerson">
          <el-input v-model="enterprise.legalPerson" placeholder="请输入法定代表人" />
        </el-form-item>
        <el-form-item label="经营执照凭证号码" prop="businessLincenseNumber">
          <el-input v-model="enterprise.businessLincenseNumber" placeholder="请输入经营执照凭证号码" />
        </el-form-item>
        <el-form-item label="企业成立日期" prop="estabinessDate">
          <el-date-picker clearable
                          v-model="enterprise.estabinessDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择企业成立日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经营范围" prop="businessScope">
          <el-input v-model="enterprise.businessScope" placeholder="请输入经营范围" />
        </el-form-item>
        <el-form-item label="注册地址" prop="address">
          <el-input v-model="enterprise.address" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="企业联系方式" prop="contactPhone">
          <el-input v-model="enterprise.contactPhone" placeholder="请输入企业联系方式" />
        </el-form-item>
        <el-form-item label="公司邮箱" prop="email">
          <el-input v-model="enterprise.email" placeholder="请输入公司邮箱" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="enterprise.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    </span>
    <span v-else>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆识别码" prop="vin">
        <el-input
          v-model="queryParams.vin"
          placeholder="请输入车辆识别码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="registrationNumber">
        <el-input
          v-model="queryParams.registrationNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆唯一标识" align="center" prop="carId" />
      <el-table-column label="车辆识别码" align="center" prop="vin" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="生产年份" align="center" prop="manufactureYear" />
      <el-table-column label="车身类型" align="center" prop="bodyType" />
      <el-table-column label="车辆颜色" align="center" prop="color" />
      <el-table-column label="发动机排量" align="center" prop="engineCapacity" />
      <el-table-column label="燃油类型" align="center" prop="fuelType" />
      <el-table-column label="变速器类型" align="center" prop="transmission" />
      <el-table-column label="驱动类型" align="center" prop="driveType" />
      <el-table-column label="行驶里程" align="center" prop="mileage" />
      <el-table-column label="注册日期" align="center" prop="registrationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="registrationNumber" />
      <el-table-column label="所有者" align="center" prop="ownerId" />
      <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)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="查看围栏">
        <span slot-scope="scope">
          <el-button type="text" @click="fence(scope.row.carId)">查看围栏</el-button>
        </span>
      </el-table-column>
      <el-table-column label="查看实时坐标">
        <span slot-scope="scope">
          <el-button type="text" @click="realTime(scope.row.vin)">查看实时坐标</el-button>
        </span>
      </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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="生产年份" prop="manufactureYear">
          <el-input v-model="form.manufactureYear" placeholder="请输入生产年份" />
        </el-form-item>
        <el-form-item label="车身类型" prop="bodyType">
          <el-input v-model="form.bodyType" placeholder="请输入车身类型" />
        </el-form-item>
        <el-form-item label="车辆颜色" prop="color">
          <el-input v-model="form.color" placeholder="请输入车辆颜色" />
        </el-form-item>
        <el-form-item label="发动机排量" prop="engineCapacity">
          <el-input v-model="form.engineCapacity" placeholder="请输入发动机排量" />
        </el-form-item>
        <el-form-item label="燃油类型" prop="fuelType">
          <el-select v-model="form.fuelType">
            <el-option label="Petrol" value="Petrol"></el-option>
            <el-option label="Diesel" value="Diesel"></el-option>
            <el-option label="Electric" value="Electric"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="变速器类型" prop="transmission">
          <el-select v-model="form.transmission">
            <el-option label="Automatic" value="Automatic"></el-option>
            <el-option label="Manual" value="Manual"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="驱动类型" prop="driveType">
          <el-select v-model="form.driveType">
            <el-option label="FWD" value="FWD"></el-option>
            <el-option label="RWD" value="RWD"></el-option>
            <el-option label="AWD" value="AWD"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行驶里程" prop="mileage">
          <el-input v-model="form.mileage" placeholder="请输入行驶里程" />
        </el-form-item>
        <el-form-item label="注册日期" prop="registrationDate">
          <el-date-picker clearable
            v-model="form.registrationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择注册日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车牌号" prop="registrationNumber">
          <el-input v-model="form.registrationNumber" 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>
    </span>
    </span>
  </div>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar, query } from "@/api/goods/car";
import {listEnterprise} from "@/api/goods/business";

export default {
  name: "Car",
  data() {
    return {
      enterprise: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vin: null,
        registrationNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    bulk() {
      alert("信息已发送，等待处理")
    },
    fence(carId) {
      this.$router.push({path: '/client/sever/fence',query:{carId:carId}})
    },
    realTime(vin) {
      this.$router.push({path: '/client/sever/random',query:{vin:vin}})

    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        console.log(response)
        this.carList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
      listEnterprise().then(res => {
        this.enterprise = res.data
        console.log(res.data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: null,
        vin: null,
        brand: null,
        model: null,
        manufactureYear: null,
        bodyType: null,
        color: null,
        engineCapacity: null,
        fuelType: null,
        transmission: null,
        driveType: null,
        mileage: null,
        registrationDate: null,
        registrationNumber: null,
        ownerId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carId = row.carId || this.ids
      getCar(carId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carId != null) {
            updateCar(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCar(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.carId || this.ids;
      this.$modal.confirm('是否确认删除车辆信息编号为"' + carIds + '"的数据项？').then(function() {
        return delCar(carIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rule/car/export', {
        ...this.queryParams
      }, `car_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
