<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef"  v-show="showSearch" label-width="90px">
      <el-row >
				<el-col :span="6">
				  <el-form-item label="车牌号" prop="plateNo">
            <el-input
              v-model="queryParams.plateNo"
              placeholder="请输入车牌号"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
				</el-col>
        <el-col :span="6">
				  <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
				</el-col>
        <el-col :span="6">
				  <el-form-item label="司机手机号" prop="driverPhone">
        <el-input
          v-model="queryParams.driverPhone"
          placeholder="请输入司机手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
				</el-col>
				<el-col :span="6" >
					<el-form-item>
            <el-button type="primary" icon="search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
				</el-col>
      </el-row>
      



      
      



    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['document:vehicle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['document:vehicle:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['document:vehicle:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['document:vehicle:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车牌号" align="center" prop="plateNo" width="100" />
      <el-table-column label="VIN/车架号" align="center" prop="vin" width="170" />
      <el-table-column label="车型" align="center" prop="vehicleTypeId" />
      <el-table-column label="申报承重(吨)" align="center" prop="declaredWeightTon" width="100" />
      <el-table-column label="申报载方(立方米)" align="center" prop="declaredVolumeM3" width="150" />
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="司机手机号" align="center" prop="driverPhone" width="120" />
      <el-table-column label="司机身份证号" align="center" prop="driverIdNo" width="150" show-overflow-tooltip />
      <el-table-column label="驾驶证号" align="center" prop="driverLicenseNo" width="100" show-overflow-tooltip />
      <el-table-column label="准驾车型" align="center" prop="driverLicenseLevel" />
      <el-table-column label="年检到期日" align="center" prop="inspectionExpireDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inspectionExpireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险到期日" align="center" prop="insuranceExpireDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.insuranceExpireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="150" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['document:vehicle:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['document:vehicle:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆档案对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="vehicleRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="车牌号" prop="plateNo">
          <el-input v-model="form.plateNo" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="VIN/车架号" prop="vin">
          <el-input v-model="form.vin" placeholder="请输入VIN/车架号" />
        </el-form-item>
        <el-form-item label="车型ID" prop="vehicleTypeId">
          <el-input v-model="form.vehicleTypeId" placeholder="请输入车型ID" />
        </el-form-item>
        <el-form-item label="申报承重(吨)" prop="declaredWeightTon">
          <el-input v-model="form.declaredWeightTon" placeholder="请输入申报承重(吨)" />
        </el-form-item>
        <el-form-item label="申报载方(立方米)" prop="declaredVolumeM3">
          <el-input v-model="form.declaredVolumeM3" placeholder="请输入申报载方(立方米)" />
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="司机手机号" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入司机手机号" />
        </el-form-item>
        <el-form-item label="司机身份证号(可选)" prop="driverIdNo">
          <el-input v-model="form.driverIdNo" placeholder="请输入司机身份证号(可选)" />
        </el-form-item>
        <el-form-item label="驾驶证号(可选)" prop="driverLicenseNo">
          <el-input v-model="form.driverLicenseNo" placeholder="请输入驾驶证号(可选)" />
        </el-form-item>
        <el-form-item label="准驾车型(可选)" prop="driverLicenseLevel">
          <el-input v-model="form.driverLicenseLevel" placeholder="请输入准驾车型(可选)" />
        </el-form-item>
        <el-form-item label="年检到期日" prop="inspectionExpireDate">
          <el-date-picker clearable
            v-model="form.inspectionExpireDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择年检到期日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保险到期日" prop="insuranceExpireDate">
          <el-date-picker clearable
            v-model="form.insuranceExpireDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择保险到期日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Vehicle">
import { listVehicle, getVehicle, delVehicle, addVehicle, updateVehicle } from "@/api/document/vehicle"

const { proxy } = getCurrentInstance()

const vehicleList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    plateNo: null,
    vin: null,
    vehicleTypeId: null,
    declaredWeightTon: null,
    declaredVolumeM3: null,
    driverName: null,
    driverPhone: null,
    driverIdNo: null,
    driverLicenseNo: null,
    driverLicenseLevel: null,
    inspectionExpireDate: null,
    insuranceExpireDate: null,
    status: null,
    isDelete: null
  },
  rules: {
    plateNo: [
      { required: true, message: "车牌号不能为空", trigger: "blur" }
    ],
    vehicleTypeId: [
      { required: true, message: "车型ID不能为空", trigger: "blur" }
    ],
    declaredWeightTon: [
      { required: true, message: "申报承重(吨)不能为空", trigger: "blur" }
    ],
    declaredVolumeM3: [
      { required: true, message: "申报载方(立方米)不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态(0启用 1停用 2维修)不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询车辆档案列表 */
function getList() {
  loading.value = true
  listVehicle(queryParams.value).then(response => {
    vehicleList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    plateNo: null,
    vin: null,
    vehicleTypeId: null,
    declaredWeightTon: null,
    declaredVolumeM3: null,
    driverName: null,
    driverPhone: null,
    driverIdNo: null,
    driverLicenseNo: null,
    driverLicenseLevel: null,
    inspectionExpireDate: null,
    insuranceExpireDate: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    isDelete: null
  }
  proxy.resetForm("vehicleRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加车辆档案"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getVehicle(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改车辆档案"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["vehicleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateVehicle(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addVehicle(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除车辆档案编号为"' + _ids + '"的数据项？').then(function() {
    return delVehicle(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('document/vehicle/export', {
    ...queryParams.value
  }, `vehicle_${new Date().getTime()}.xlsx`)
}

getList()
</script>
