<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input v-model="queryParams.licensePlate" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <!-- <el-form-item label="驾驶员" prop="driver">
        <el-input v-model="queryParams.driver" placeholder="请输入驾驶员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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"
          v-hasPermi="['oa:car:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['oa:car:edit']">修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['oa:car:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['oa:car:export']">导出</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="车辆id" align="center" prop="carId" /> -->
      <el-table-column label="车辆状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="statusTypeStyle(scope.row.status)">{{ statusTypeText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="驾驶员" align="center" prop="driverUser.nickName" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="车型" align="center" prop="series" />
      <el-table-column label="购置时间" align="center" prop="acquisitionTime" />
      <el-table-column label="是否为租车" align="center" prop="isRent">
        <template slot-scope="scope">
          {{ scope.row.isRent == "0" ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column label="购买价格" align="center" prop="cost">
        <template slot-scope="scope">
          {{ scope.row.cost + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="预计使用年限" align="center" prop="expectLife">
        <template slot-scope="scope">
          {{ scope.row.expectLife + '年' }}
        </template>
      </el-table-column>
      <el-table-column label="预计行驶里程" align="center" prop="expectKm">
        <template slot-scope="scope">
          {{ scope.row.expectKm + '万千米' }}
        </template>
      </el-table-column>
      <el-table-column label="油耗（km）" align="center" prop="fuel" >
        <template slot-scope="scope">
          {{ scope.row.fuel + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="保险（天）" align="center" prop="insurance" >
        <template slot-scope="scope">
          {{ scope.row.insurance + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="维修（天）" align="center" prop="maintenance" >
        <template slot-scope="scope">
          {{ scope.row.maintenance + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="单日成本" align="center" prop="dayCost" >
        <template slot-scope="scope">
          {{ scope.row.dayCost + '元' }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="200px">

        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleAddDetail(scope.row)"
            v-hasPermi="['oa:car:query']">查看明细</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['oa:car:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" style="color: #fc0000;"
            @click="handleDelete(scope.row)" v-hasPermi="['oa:car:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改cmc车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="车型" prop="series">
          <el-input v-model="form.series" placeholder="请输入车型" />
        </el-form-item>
        <el-form-item label="购置时间" prop="acquisitionTime">
          <el-date-picker v-model="form.acquisitionTime" type="date" placeholder="选择日期" format="yyyy-MM-dd"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购买价格" prop="cost">
          <el-input style="width:130px;margin-right:10px;" v-model="form.cost" placeholder="请输入金额" />
          <span>元</span>
        </el-form-item>
        <el-form-item label="预计使用年限" prop="expectLife">
          <el-input v-model="form.expectLife" placeholder="请输入年限" style="width:130px;margin-right:10px;" />
          <span>年</span>
        </el-form-item>
        <el-form-item label="预计行驶里程" prop="expectKm">
          <el-input v-model="form.expectKm" placeholder="请输入里程" style="width:130px;margin-right:10px;" />
          <span>万千米</span>
        </el-form-item>
        <el-form-item label="油耗（元/km）" prop="fuel">
          <el-input style="width:130px;margin-right:10px;" v-model="form.fuel" placeholder="请输入金额" />
          <span>元</span>
        </el-form-item>
        <el-form-item label="保险（元/天）" prop="insurance">
          <el-input style="width:130px;margin-right:10px;" v-model="form.insurance" placeholder="请输入金额" />
          <span>元</span>
        </el-form-item>
        <el-form-item label="维修（元/天）" prop="maintenance">
          <el-input style="width:130px;margin-right:10px;" v-model="form.maintenance" placeholder="请输入金额" />
          <span>元</span>
        </el-form-item>
        <el-form-item label="单日成本" prop="dayCost">
          <el-input style="width:130px;margin-right:10px;" v-model="form.dayCost" placeholder="请输入金额" />
          <span>元</span>
        </el-form-item>
        <el-form-item label="驾驶员" prop="driver">
          <el-select v-model="form.driver" filterable placeholder="请选择" clearable>
            <el-option v-for="item in driverList" :key="item.userId" :label="item.nickName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否为租车" prop="isRent">
          <el-radio-group v-model="form.isRent">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.isRent == '1'" label="单日成本" prop="dayCost">
          <el-input style="width:400px;margin-right:10px;" v-model="form.dayCost" placeholder="请输入单日成本"/>
          <span>元</span>
        </el-form-item>
        <el-form-item label="车辆状态" prop="status">
          <el-select v-model="form.status" filterable placeholder="请选择">
            <el-option v-for="item in statusList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </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 { listCar, getCar, delCar, addCar, updateCar } from "@/api/oa/car/car";
import { getUserByPost } from "@/api/system/post";

export default {
  name: "Car",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc车辆信息表格数据
      carList: [],
      statusList: [{ id: '0', name: '已派出' }, { id: '1', name: '可使用' }, { id: '2', name: '维修中' }, { id: '3', name: '已还车' }, { id: '4', name: '已报废' }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        licensePlate: null,
        driver: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      driverList: [],
      detailOpen: false
    };
  },
  created() {
    this.getList();
    this.getDriverList();
  },
  methods: {
    /** 查询cmc车辆信息列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询驾驶员列表
    getDriverList() {
      getUserByPost({ postName: '驾驶员' }).then(response => {
        this.driverList = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: null,
        licensePlate: null,
        driver: 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 = "修改车辆信息";
      });
    },
    // 新增明细按钮
    handleAddDetail(row) {
      this.$router.push({
        path: '/car/detail',
        query: {
          carId: row.carId,
        }
      })
    },
    /** 提交按钮 */
    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;
      const licensePlate = row.licensePlate
      this.$modal.confirm('是否确认删除车牌号为【"' + licensePlate + '"】的车辆数据？').then(function () {
        return delCar(carIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/car/export', {
        ...this.queryParams
      }, `car_${new Date().getTime()}.xlsx`)
    },
    statusTypeText(row) {
      if (row == '0') {
        return '已派出'
      }
      if (row == '1') {
        return '可使用'
      }
      if (row == '2') {
        return '维修中'
      }
      if (row == '3') {
        return '已还车'
      }
      if (row == '4') {
        return '已报废'
      }
    },
    statusTypeStyle(row) {
      if (row == '0') {
        return 'warning'
      }
      if (row == '1') {
        return 'success'
      }
      if (row == '2') {
        return 'primary'
      }
      if (row == '3') {
        return 'danger'
      }
      if (row == '4') {
        return 'info'
      }
    }
  }
};
</script>
