<template>
  <my-page>
    <template slot="left">
      <CustList style="width: 320px;" custType="1" @change="handleCustsChange"></CustList>
    </template>
    <div class="app-container">
      <Search :show-search="showSearch" @search="handleSearch" />
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:vehicle:add']"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:vehicle:edit']"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:vehicle:remove']"
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:vehicle:export']"
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
      </el-row>

      <el-table
        v-loading="loading"
        :data="vehicleList"
        :highlight-current-row="true"
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="" type="index" width="55" align="center" />
        <el-table-column label="车牌号码" align="center" prop="vehicleNumber" sortable="custom" />
        <el-table-column label="品牌型号" align="center" prop="vehicleBrand" sortable="custom" />
        <el-table-column label="企业名称" align="center" prop="custName" sortable="custom" />
        <el-table-column label="载重" align="center" prop="vehicleLoad" sortable="custom" />
        <el-table-column label="装载体积" align="center" prop="vehicleVol" sortable="custom" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['tm:vehicle:query']"
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleRead(scope.row)"
            >查看</el-button>
            <el-button
              v-hasPermi="['tm:vehicle:edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              v-hasPermi="['tm:vehicle:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        hide-on-single-page
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改车辆信息对话框 -->
      <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
        <!-- <el-form ref="form" :model="form" :rules="rules" label-width="100px">
         
          <el-col :span="12">
            <el-form-item label="行驶证id" prop="vehicleLicenseId">
              <el-input v-model="form.vehicleLicenseId" placeholder="请输入行驶证id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营运证id" prop="operationCertificateId">
              <el-input v-model="form.operationCertificateId" placeholder="请输入营运证id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商业险id" prop="commercialInsuranceId">
              <el-input v-model="form.commercialInsuranceId" placeholder="请输入商业险id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交强险id" prop="compulsoryTrafficInsuranceId">
              <el-input v-model="form.compulsoryTrafficInsuranceId" placeholder="请输入交强险id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产权证id" prop="vehicleOwnershipCertificateId">
              <el-input v-model="form.vehicleOwnershipCertificateId" placeholder="请输入产权证id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他文件id" prop="otherFileId">
              <el-input v-model="form.otherFileId" placeholder="请输入其他文件id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-form> -->
        <VehicleEdit ref="edit" v-if="open" :bid="showId" :readonly="readonly" @saved="refresh"></VehicleEdit>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="!readonly" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </my-page>
</template>

<script>
import { pageListVehicle, getVehicle, delVehicle, addVehicle, updateVehicle } from '@/api/tm/vehicle'
import CustList from '@/views/tm/components/CustList'
import VehicleEdit from '@/views/tm/components/VehicleEdit'
import Search from './modules/search.vue'

export default {
  name: 'Vehicle',
  components: {
    CustList,
    VehicleEdit,
    Search
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      vehicleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custId: null,
        vehicleType: null,
        vehicleStucture: null,
        vehicleModel: null,
        vehicleLength: null,
        vehicleLoad: null,
        vehicleVol: null,
        vehicleBrand: null,
        vehicleNo: null,
        vehicleNumber: null,
        vehicleFakeNumber: null,
        frameNumber: null,
        engineNumber: null,
        vehicleLicense: null,
        operationNumber: null,
        driverId: null,
        certDate: null,
        insuranceBeginDate: null,
        insuranceEndDate: null,
        insuranceNo: null,
        gpsNo: null,
        driveBeginDate: null,
        driveEndDate: null,
        consultBeginDate: null,
        consultEndDate: null,
        asDate: null,
        asNo: null,
        consultNo: null,
        OPTIONS: null,
        certValid: null,
        lendOut: null,
        WEIGHT: null,
        frameSize: null,
        vehicleLicenseId: null,
        operationCertificateId: null,
        commercialInsuranceId: null,
        compulsoryTrafficInsuranceId: null,
        vehicleOwnershipCertificateId: null,
        otherFileId: null,
        status: null,
        remark: null,
        orderByColumn: 'createTime',
        isAsc: 'DESC',
      },
      showId: null,
      readonly: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCustsChange(custs) {
      const cust = custs[0] || {};
      this.queryParams.custId = cust.id;
      this.handleSearch();
    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true
      pageListVehicle(this.queryParams).then(response => {
        this.vehicleList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    refresh() {
      this.open = false;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false
    },
    // 排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams)
      this.getList()
    },
    /** 搜索按钮操作 */
    handleSearch(payload) {
      this.queryParams.pageNum = 1
      this.queryParams = {
        ...this.queryParams,
        ...payload
      }
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.vehicleNumber)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleRead(row) {
      const id = row.id || this.ids
      this.open = true
      this.showId = id
      this.readonly = true
      this.title = '查看车辆信息'
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.showId = ''
      this.readonly = false
      this.title = '新增车辆'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.open = true
      this.showId = id
      this.readonly = false
      this.title = '编辑车辆信息'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.edit.submitForm()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      const names = row.vehicleNumber || this.names
      this.$modal.confirm('是否确认删除车牌号为"' + names + '"的车辆？').then(function() {
        return delVehicle(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tm/vehicle/export', {
        ...this.queryParams
      }, `vehicle_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
