<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆类型" prop="rescueCarType">
        <el-select v-model="queryParams.rescueCarType" placeholder="请选择车辆类型" clearable>
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.JYC_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号" prop="rescueCarNum">
        <el-input
          v-model="queryParams.rescueCarNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="车辆所属" prop="carOwn">-->
<!--        <el-select v-model="queryParams.carOwn" placeholder="请选择车辆所属" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.car_own"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="车牌颜色" prop="carLicenseColor">
        <el-select v-model="queryParams.carLicenseColor" placeholder="请选择车牌颜色" clearable>
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.CAR_LICENSE_COLOR)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆性质" prop="carUseNature">
        <el-select v-model="queryParams.carUseNature" placeholder="请选择车辆性质" clearable>
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.CAR_USE_NATURE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rescueCarList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center">
        <template scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆类型" align="center" prop="rescueCarType">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.JYC_TYPE" :value="scope.row.rescueCarType"/>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="rescueCarNum" />
      <el-table-column label="品牌型号" align="center" prop="rescueCarBrand" />
      <el-table-column label="图片" align="center" prop="carImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.carImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="购买时间" align="center" prop="carBuyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.carBuyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保养到期时间" align="center" prop="carKeepTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.carKeepTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="保险到期时间" align="center" prop="carInsuranceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.carInsuranceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年检到期时间" align="center" prop="carCheckTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.carCheckTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌颜色" align="center" prop="carLicenseColor">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.CAR_LICENSE_COLOR" :value="scope.row.carLicenseColor"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="车辆所属" align="center" prop="carLicenseColor">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.car_own" :value="scope.row.carOwn"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="车辆性质" align="center" prop="carUseNature">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.CAR_USE_NATURE" :value="scope.row.carUseNature"/>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改救援车辆信息对话框 -->
    <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="rescueCarType">
          <el-select v-model="form.rescueCarType" placeholder="请选择车辆类型">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.JYC_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号" prop="rescueCarNum">
          <el-input v-model="form.rescueCarNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="品牌型号" prop="rescueCarBrand">
          <el-input v-model="form.rescueCarBrand" placeholder="请输入品牌型号" />
        </el-form-item>
        <el-form-item label="图片" prop="carImage">
          <image-upload v-model="form.carImage"/>
        </el-form-item>
        <el-form-item label="行驶证" prop="driveLicenseImage">
          <image-upload v-model="form.driveLicenseImage"/>
        </el-form-item>
        <el-form-item label="购买时间" prop="carBuyTime">
          <el-date-picker clearable
                          v-model="form.carBuyTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择购买时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保养到期时间" prop="carBuyTime">
          <el-date-picker clearable
                          v-model="form.carKeepTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保险到期时间" prop="carBuyTime">
          <el-date-picker clearable
                          v-model="form.carInsuranceTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年检到期时间" prop="carBuyTime">
          <el-date-picker clearable
                          v-model="form.carCheckTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车牌颜色" prop="carLicenseColor">
          <el-radio-group v-model="form.carLicenseColor">
            <el-radio
              v-for="dict in this.getDictDatas(DICT_TYPE.CAR_LICENSE_COLOR)"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆性质" prop="carUseNature">
          <el-radio-group v-model="form.carUseNature">
            <el-radio
              v-for="dict in this.getDictDatas(DICT_TYPE.CAR_USE_NATURE)"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车架号" prop="frameNumber">
          <el-input v-model="form.frameNumber" placeholder="请输入车架号" />
        </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 { listRescueCar, getRescueCar, delRescueCar, addRescueCar, updateRescueCar } from "./api/carInfo";

  export default {
    name: "RescueCar",
    dicts: ['car_own', 'car_use_nature', 'jyc_type', 'car_license_color'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 救援车辆信息表格数据
        rescueCarList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          rescueCarType: null,
          rescueCarNum: null,
          rescueCarBrand: null,
          carOwn: null,
          carImage: null,
          driveLicenseImage: null,
          carBuyTime: null,
          carLicenseColor: null,
          carUseNature: null,
          frameNumber: null,
          possessorId: null,
          deptId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询救援车辆信息列表 */
      getList() {
        this.loading = true;
        listRescueCar(this.queryParams).then(response => {
          this.rescueCarList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          rescueCarType: null,
          rescueCarNum: null,
          rescueCarBrand: null,
          carOwn: null,
          carImage: null,
          driveLicenseImage: null,
          carBuyTime: null,
          carLicenseColor: null,
          carUseNature: null,
          frameNumber: null,
          possessorId: null,
          deptId: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNo = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加救援车辆信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getRescueCar(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改救援车辆信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.carOwn = "0"
            if (this.form.id != null) {
              updateRescueCar(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRescueCar(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除救援车辆信息编号为"' + ids + '"的数据项？').then(function() {
          return delRescueCar(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/rescueCar/export', {
          ...this.queryParams
        }, `rescueCar_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
