<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select
          v-model="queryForm.vehicleType"
          placeholder="请选择车辆类型"
          style="width: 100%"
          clearable>
          <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="licensePlate">
        <el-input
          v-model="queryForm.licensePlate"
          placeholder="请输入车牌号"
          @keyup.enter.native="handleQuery"></el-input>
      </el-form-item>
      <el-form-item label="能源类型" prop="vehicleFuel">
        <el-select
          v-model="queryForm.vehicleFuel"
          placeholder="请选择车辆能源类型"
          style="width: 100%"
          clearable>
          <el-option
            v-for="dict in dict.type.vehicle_fuel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车辆状态" prop="vehicleState">
        <el-select
          v-model="queryForm.vehicleState"
          placeholder="请选择车辆当前状态"
          clearable>
          <el-option
            v-for="dict in dict.type.vehicle_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </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"
          v-hasPermi="['system:vehicle: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="['system:vehicle: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="['system:vehicle:remove']"
        >删除
        </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" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="vehicleList"
      :cell-style="cellStyle"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="车辆品牌" align="center" v-if="columns[0].visible" prop="vehicleBrand"
                       show-overflow-tooltip/>
      <el-table-column label="车牌号" align="center" v-if="columns[1].visible" prop="licensePlate">
      </el-table-column>
      <el-table-column label="车身颜色" align="center" v-if="columns[2].visible" prop="vehicleColour">
      </el-table-column>
      <el-table-column label="车辆类型" align="center" v-if="columns[3].visible" prop="vehicleType"
                       show-overflow-tooltip>
        <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[4].visible" prop="vehicleFuel"
                       show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vehicle_fuel" :value="scope.row.vehicleFuel"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="出厂日期" align="center" v-if="columns[5].visible" prop="dateOfProduction"
                       show-overflow-tooltip/>
      <el-table-column label="车辆状态" align="center" v-if="columns[6].visible" prop="vehicleState"
                       show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tag :type="judgeStateType(scope.row.vehicleState)">
            <dict-tag :options="dict.type.vehicle_state" :value="scope.row.vehicleState"></dict-tag>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" v-if="columns[7].visible"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:vehicle:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:vehicle:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="cancel">
      <el-form ref="postForm" :model="postForm" :rules="rules" label-width="80px">
        <el-form-item label="车辆品牌" prop="vehicleBrand">
          <el-input v-model="postForm.vehicleBrand"></el-input>
        </el-form-item>
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="postForm.licensePlate" :readonly="readonly"></el-input>
        </el-form-item>
        <el-form-item label="车身颜色" prop="vehicleColour">
          <el-input v-model="postForm.vehicleColour"></el-input>
        </el-form-item>
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-select
            v-model="postForm.vehicleType"
            placeholder="选择类型"
            style="width: 100%"
            clearable>
            <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="vehicleFuel">
          <el-select
            v-model="postForm.vehicleFuel"
            placeholder="选择类型"
            style="width: 100%"
            clearable>
            <el-option
              v-for="dict in dict.type.vehicle_fuel"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出厂日期">
          <el-date-picker
            v-model="postForm.dateOfProduction"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 100%"
            :picker-options="limitProduction"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆状态" prop="vehicleState">
          <el-select
            v-model="postForm.vehicleState"
            placeholder="选择类型"
            style="width: 100%"
            clearable>
            <el-option
              v-for="dict in dict.type.vehicle_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></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>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryForm.pageNum"
      :limit.sync="queryForm.pageSize"
      @pagination="getList"
    ></pagination>
    <!-- 导出对话框 -->
    <ExportTable
      :open-export.sync="exportOptions.open"
      :title="exportOptions.title"
      @export="exportFile"
    ></ExportTable>
  </div>
</template>

<script>
import {getInfoList, addInfo, editInfo, deleteInfo, getInfo} from "@/api/smartSchool/vehicleManage/vehicleInfo";
import {CustomCellStyle} from '@/enums/customStyle';
import {ExportType} from "@/enums/common";

export default {
  name: 'VehicleInfo',
  dicts: ['vehicle_type', 'vehicle_fuel', 'vehicle_state'],
  data() {
    return {
      // 导出选项
      exportOptions: {
        title: '选择导出类别',
        open: false
      },
      // 查询表单
      queryForm: {
        pageNum: 1,
        pageSize: 10,
        vehicleType: '',// 车辆类型
        licensePlate: '',// 车牌号
        vehicleFuel: '',// 燃料类型
        vehicleState: '',// 车辆状态

        // vehicleBrand: '',// 车辆品牌
        // vehicleColour: '',// 车身颜色
        // dateOfProduction: '',// 出厂日期
        // submitState: '',// 是否提交
      },
      // 遮罩层
      loading: false,
      // 课程信息列表
      vehicleList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 是否只读
      readonly: false,
      // 显示搜索条件
      showSearch: true,
      // 数据总数
      total: 0,
      // 新增/修改表单
      postForm: {},
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        vehicleBrand: [
          {required: true, message: "车辆品牌不能为空", trigger: "blur"},
        ],
        licensePlate: [
          {required: true, message: "车牌号不能为空", trigger: "blur"},
        ],
        vehicleType: [
          {required: true, message: "车辆类型不能为空", trigger: "blur"},
        ],
        vehicleFuel: [
          {required: true, message: "能源类型不能为空", trigger: "blur"},
        ],
        vehicleState: [
          {required: true, message: "车辆状态不能为空", trigger: "blur"},
        ],
      },
      // 弹出层标题
      title: '',
      // 列表信息
      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},
      ],
      limitProduction: {
        disabledDate: time => Date.parse(time) > Date.parse(new Date())
      },
      // 自定义列表单元格样式
      cellStyle: CustomCellStyle
    }
  },
  created() {
    this.getList();
  },
  computed: {
    // 能源类型
    judgeFuelType() {
      return vehicleFuel => {
        let result = '';
        switch (vehicleFuel) {
          case '0':
            result = 'warning';
            break;
          case '1':
            result = 'success'
            break;
        }
        return result
      }
    },
    // 车辆状态
    judgeStateType() {
      return vehicleState => {
        let type = '';
        switch (vehicleState) {
          case '0':
            type = 'success';
            break;
          case '1':
            type = 'warning'
            break;
          case '2':
            type = 'danger'
            break;
        }
        return type
      }
    }
  },
  methods: {
    /** 获取列表数据 */
    getList() {
      this.loading = true; // 加载效果
      getInfoList(this.queryForm).then(response => {
        this.loading = false;
        this.vehicleList = response.rows;
        this.total = response.total;
      }).catch(err => {
        this.loading = false;
      })
    },
    // 搜索按钮
    handleQuery() {
      this.throttle(() => {
        this.queryForm.pageNum = 1;
        this.getList();
      })
    },
    // 重置按钮
    resetQuery() {
      this.queryForm = {
        pageNum: 1,
        pageSize: 10,
        vehicleBrand: '',// 车辆品牌
        licensePlate: '',// 车牌号
        vehicleType: '',// 车辆类型
        vehicleFuel: '',// 燃料类型
        vehicleColour: '',// 车身颜色
        dateOfProduction: '',// 出厂日期
        vehicleState: '',// 车辆状态
      }
      this.handleQuery(); // 重新发一次请求
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.open = true;
      this.readonly = false;
      this.title = "添加车辆信息";
    },
    // 修改按钮操作
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.title = "修改车辆信息";
      this.$modal.loading('正在加载数据，请稍等...');
      getInfo(id).then(response => {
        this.postForm = response.data;
        this.open = true;
        this.readonly = true;
        this.$modal.closeLoading();
      }).catch(err => {
        this.$modal.closeLoading();
      })
    },
    // 删除按钮操作
    handleDelete(row) {
      this.throttle(() => {
        const ids = row.id || this.ids;
        if (ids) {
          this.$modal.confirm(`是否确认删除选中的${Object.hasOwnProperty.call(ids, 'length') ? ids.length : 1}条数据？`).then(() => {
            this.$modal.loading('正在处理数据，请稍等...');
            return deleteInfo(ids);
          }).then(() => {
            this.$modal.closeLoading();
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {
            this.$modal.closeLoading();
          });
        }
      })
    },
    // 导出按钮操作
    handleExport() {
      this.exportOptions.open = true;
      // this.throttle(() => {
      //   const ids = this.ids;
      //   this.$modal.confirm(`是否导出所选数据或全部导出？`).then(() => {
      //     this.download('vehicle/export/' + ids, {
      //     ...this.queryForm
      //     }, `车辆信息${ new Date().getTime() }.xlsx`)
      //   })
      // })
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 提交表单
    submitForm() {
      this.throttle(() => {
        this.$refs["postForm"].validate(valid => {
          if (valid) {
            this.$modal.loading('正在上传数据，请稍等...');
            if (this.postForm.id != null) {
              // 编辑
              editInfo(this.postForm).then(response => {
                this.$modal.closeLoading();
                this.$modal.msgSuccess("修改成功");
                console.log(this);
                this.open = false;
                this.getList();
              }).catch(err => {
                this.$modal.closeLoading();
              });
            } else {
              // 新增
              addInfo(this.postForm).then(response => {
                this.$modal.closeLoading();
                console.log(this);
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).catch(err => {
                this.$modal.closeLoading();
              });
            }
          }
        });
      })
    },
    /** 导出 */
    exportFile(callback) {
      const ids = this.ids;
      const options = [
        {type: ExportType.TOTAL, path: '/vehicle/export', params: {}},
        {type: ExportType.SELECT, path: '/vehicle/export/' + ids, params: {}},
        {type: ExportType.QUERY, path: '/vehicle/export', params: this.queryForm},
      ]
      callback(options, `车辆信息${Date.now()}.xlsx`);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // 这里需要重置对话框表单
      this.reset();
    },
    /** 重置对话框中表单 */
    reset() {
      this.postForm = {
        vehicleBrand: '',// 车辆品牌
        licensePlate: '',// 车牌号
        vehicleType: '',// 车辆类型
        vehicleFuel: '',// 燃料类型
        vehicleColour: '',// 车身颜色
        dateOfProduction: '',// 出厂日期
        vehicleState: '',// 车辆状态
        title: '' // 弹出层标题
      };
      this.resetForm('postForm');
    },
    submitTime() {

    },
    cancelTime() {

    },
  }
}
</script>

<style>

</style>
