<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">

      <el-form-item label="车辆所有人" prop="carBlong">
        <el-select v-model="queryParams.carBlong" placeholder="请选择车辆所有人" clearable>
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.Car_blong)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="车牌号码" prop="carNo">
        <el-input
          v-model="queryParams.carNo"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</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>

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入
        </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="车辆品牌" align="center" prop="brand" />
      <el-table-column label="车辆型号" align="center" prop="carModel" />
      <el-table-column label="车牌号码" align="center" prop="carNo" />
      <el-table-column label="车辆照片" align="center" prop="carPhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.carPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="初登日期" align="center" prop="carRegisterDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.carRegisterDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年检日期" align="center" prop="inspectionDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆使用人" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="userPhone" />
      <el-table-column label="车辆所有人" align="center" prop="carBlong">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.Car_blong" :value="scope.row.carBlong"/>
        </template>
      </el-table-column>
      <el-table-column label="行驶证相片" align="center" prop="drivingLicence" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.drivingLicence" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="运营证" align="center" prop="operationCertificate" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.operationCertificate" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="二维日期" align="center" prop="repairDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下线日期" align="center" prop="outlineDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outlineDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险起止日期" align="center" prop="baoxianSttime" width="150">
        <template slot-scope="scope">
          <div >{{scope.row.baoxianSttime}}</div> 至 <div>{{scope.row.baoxianEntime}}</div>
        </template>
      </el-table-column>
      <el-table-column label="承保公司" align="center" prop="company" width="120"/>
      <el-table-column label="保险内容" align="center" prop="content" width="100"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <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.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="车辆品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入车辆品牌" />
        </el-form-item>
        <el-form-item label="车辆型号" prop="carModel">
          <el-input v-model="form.carModel" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="车牌号码" prop="carNo">
          <el-input v-model="form.carNo" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="车辆照片" prop="carPhoto">
          <image-upload v-model="form.carPhoto"/>
        </el-form-item>
        <el-form-item label="初登日期" prop="carRegisterDate">
          <el-date-picker clearable
            v-model="form.carRegisterDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择初登日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年检日期" prop="inspectionDate">
          <el-date-picker clearable
            v-model="form.inspectionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择年检日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆使用人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入车辆使用人" />
        </el-form-item>
        <el-form-item label="手机号" prop="userPhone">
          <el-input type="number" v-model="form.userPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="车辆所有人" prop="carBlong">
          <el-select v-model="form.carBlong" placeholder="请选择车辆所有人">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.Car_blong)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行驶证相片" prop="drivingLicence">
          <image-upload v-model="form.drivingLicence"/>
        </el-form-item>
        <el-form-item label="运营证" prop="operationCertificate">
          <image-upload v-model="form.operationCertificate"/>
        </el-form-item>

        <el-form-item label="二维日期" prop="repairDate">
          <el-date-picker clearable
            v-model="form.repairDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择二维日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下线日期" prop="outlineDate">
          <el-date-picker clearable
                          v-model="form.outlineDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择下线日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保险起日期" prop="baoxianSttime">
          <el-date-picker clearable
                          v-model="form.baoxianSttime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择保险起日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="保险止日期" prop="baoxianEntime">
          <el-date-picker clearable
                          v-model="form.baoxianEntime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择保险止日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="承保公司" prop="company">
          <el-input v-model="form.company" placeholder="请输入承保公司" />
        </el-form-item>

        <el-form-item label="保险内容" prop="content">
          <el-input v-model="form.content" 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>

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--          <div class="el-upload__tip" slot="tip">
                      <el-checkbox v-model="upload.updateSupport"/>
                      是否更新已经存在的用户数据
                    </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!--          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                             @click="importTemplate">下载模板
                    </el-link>-->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar } from "./api/car";
import { getAccessToken } from "@/utils/auth";

export default {
  name: "Car",
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getAccessToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/drivingSchool/system/car/importData"
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        brand: null,
        carModel: null,
        carNo: null,
        carPhoto: null,
        carRegisterDate: null,
        inspectionDate: null,
        userName: null,
        userPhone: null,
        carBlong: null,
        drivingLicence: null,
        operationCertificate: null,
        outlineDate: null,
        repairDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        brand: [
          { required: true, message: "车辆品牌不能为空", trigger: "blur" }
        ],
        carModel: [
          { required: true, message: "车辆型号不能为空", trigger: "blur" }
        ],
        carNo: [
          { required: true, message: "车牌号码不能为空", trigger: "blur" }
        ],
        carPhoto: [
          { required: true, message: "车辆照片不能为空", trigger: "blur" }
        ],
        carRegisterDate: [
          { required: true, message: "初登日期不能为空", trigger: "blur" }
        ],
        inspectionDate: [
          { required: true, message: "年检日期不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "车辆使用人不能为空", trigger: "blur" }
        ],
        userPhone: [
          { required: true, message: "使用人手机号不能为空", trigger: "blur" }
        ],
        carBlong: [
          { required: true, message: "车辆所有人 1：驾校 2：教练 3：其他不能为空", trigger: "change" }
        ],
        drivingLicence: [
          { required: true, message: "行驶证相片不能为空", trigger: "blur" }
        ],
        operationCertificate: [
          { required: true, message: "运营证不能为空", trigger: "blur" }
        ],
        outlineDate: [
          { required: true, message: "下线日期不能为空", trigger: "blur" }
        ],
        repairDate: [
          { required: true, message: "二维日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "车辆导入";
      this.upload.open = true;
    },
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        brand: null,
        carModel: null,
        carNo: null,
        carPhoto: null,
        carRegisterDate: null,
        inspectionDate: null,
        userName: null,
        userPhone: null,
        carBlong: null,
        drivingLicence: null,
        operationCertificate: null,
        outlineDate: null,
        repairDate: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: 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.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
      getCar(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != 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 ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆信息编号为"' + ids + '"的数据项？').then(function() {
        return delCar(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('drivingSchool/system/car/export', {
        ...this.queryParams
      }, `car_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
