<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          v-model="queryForm.licensePlate"
          placeholder="请输入车牌号"
          @keyup.enter.native="handleQuery"
        ></el-input>
      </el-form-item>
      <el-form-item label="用车类型" prop="vehicleType">
        <el-select
          v-model="queryForm.vehicleType"
          placeholder="请选择用车类型"
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="dict in dict.type.vehicle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="司机" prop="driver">
        <el-input
          v-model="queryForm.driver"
          placeholder="请输入司机姓名"
          @keyup.enter.native="handleQuery"
        ></el-input>
      </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-table
      v-loading="loading"
      :data="vehicleList">
      <el-table-column label="序号" align="center" type="index">
      </el-table-column>
      <el-table-column label="车牌号" align="center" v-if="columns[0].visible" prop="licensePlate"></el-table-column>
      <el-table-column label="人数" align="center" v-if="columns[1].visible" prop="peopleNumber"></el-table-column>
      <el-table-column label="出发地点" align="center" v-if="columns[2].visible"
                       prop="departurePlace"></el-table-column>
      <el-table-column label="目的地" align="center" v-if="columns[3].visible" prop="destination"></el-table-column>
      <el-table-column label="用车开始时间" align="center" v-if="columns[4].visible" prop="startTime"></el-table-column>
      <el-table-column label="用车结束时间" align="center" v-if="columns[5].visible" prop="endTime"></el-table-column>
      <el-table-column label="用车人" align="center" v-if="columns[6].visible" prop="applicantName"></el-table-column>
      <el-table-column label="车辆类型" align="center" v-if="columns[7].visible" prop="vehicleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vehicle_type" :value="scope.row.vehicleType"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="公里数" align="center" v-if="columns[8].visible" prop="kilometers"></el-table-column>
      <el-table-column label="操作" align="center" v-if="columns[9].visible">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleCheck(scope.row)"
            v-hasPermi="['system:vehicle:check']"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      @close="close"
    >
      <el-form
        ref="postForm"
        :model="postForm"
        label-width="100px"
        size="small"
      >
        <el-row>
          <el-col :span="11">
            <el-form-item label="车牌号" prop="licensePlate">
              <el-input :value="postForm.licensePlate"></el-input>
            </el-form-item>
            <el-form-item label="用车开始时间" prop="startTime">
              <el-input :value="postForm.startTime"></el-input>
            </el-form-item>
            <el-form-item label="用车人" prop="applicantName">
              <el-input :value="postForm.applicantName"></el-input>
            </el-form-item>

            <el-form-item label="出发地点" prop="departurePlace">
              <el-input :value="postForm.departurePlace"></el-input>
            </el-form-item>

            <el-form-item label="公里数" prop="kilometers">
              <el-input :value="postForm.kilometers"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="2">
            <el-form-item label="车辆类型" prop="vehicleType">
              <el-select :value="postForm.vehicleType" style="width: 100%" placeholder="请选择场地类型">
                <el-option
                  v-for="dict in dict.type.vehicle_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="用车结束时间" prop="endTime">
              <el-input :value="postForm.endTime"></el-input>
            </el-form-item>
            <el-form-item label="人数" prop="peopleNumber">
              <el-input :value="postForm.peopleNumber"></el-input>
            </el-form-item>
            <el-form-item label="目的地" prop="destination">
              <el-input :value="postForm.destination"></el-input>
            </el-form-item>

            <!-- <el-form-item label="用车类型" prop="vehicleType">
                <el-input :value="getVehicleType(postForm.vehicleType)">
                </el-input>
            </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

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

<script>
import {getRecordList, getRecordInfo} from '@/api/smartSchool/schoolManage/vehicleApply'

export default {
  name: 'VehicleRecord',
  dicts: ['vehicle_type'],
  data() {
    return {
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        licensePlate: '',// 车牌号
        vehicleType: '',// 用车类型
        driver: '',// 司机
        // peopleNumber: '',// 人数
        // departurePlace: '',// 出发地点
        // destination: '',// 目的地
        // startTime: '',// 用车开始时间
        // endTime: '',// 用车结束时间
        // kilometers: '',// 公里数
        // applicantName: '',// 用车人
      },
      // 数据总数
      total: 0,
      // 遮罩层
      loading: false,
      // 信息列表
      vehicleList: [],
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: '',
      // 查看列表
      postForm: {},
      // 列表信息
      columns: [
        {key: 0, label: `车牌号`, visible: true},
        {key: 1, label: `人数`, visible: true},
        {key: 2, label: `出发地点`, visible: true},
        {key: 3, label: `目的地`, visible: true},
        {key: 4, label: `用车开始时间`, visible: true},
        {key: 5, label: `用车结束时间`, visible: true},
        {key: 6, label: `用车人`, visible: true},
        {key: 7, label: `用车类型`, visible: true},
        {key: 8, label: `公里数`, visible: true},
        {key: 9, label: `操作`, visible: true},
      ],
    }
  },
  /**
   * 计算属性
   * 1. 拿到字典数据：实则是数组
   * 2. 自己遍历数组，拿到对应值
   * 3. :value='computed'
   * 4. ???
   * */
  computed: {
    getVehicleType(vehicleType) {
      type = dict.type.vehicle_type.value.map((value) => {
        return value
      })
    }
  },

  created() {
    this.getList();
  },

  methods: {
    // 获取列表数据
    getList() {
      this.loading = true; // 加载效果
      getRecordList(this.queryForm).then(response => {
        this.vehicleList = response.rows;
        this.total = response.total;
        this.loading = false
      }).catch(err => {
        this.loading = fasle;
      })
    },

    // 搜索按钮操作
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getList();
    },

    // 查看操作
    handleCheck(row) {
      this.title = "用车记录详细信息";
      this.$modal.loading('正在加载数据，请稍等...');
      getRecordInfo(row.id).then(response => {
        console.log(response);
        this.postForm = response.data;
        this.open = true;
        this.$modal.closeLoading();
      }).catch(err => {
        this.$modal.closeLoading();
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 关闭操作
    close() {
      this.open = false
    }
  }
}
</script>

<style>

</style>
