<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修保养时间" prop="maintenanceTime" >
        <el-date-picker
          v-model="time1"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="保险购买时间" prop="insuranceBuyTime">
        <el-date-picker
          v-model="time2"
          value-format="yyyy-MM-dd"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年审时间" prop="annualAuditTime">
        <el-date-picker
          v-model="time3"
          value-format="yyyy-MM-dd"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </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"
          :loading="exportLoading"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rescueCarSpendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <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="carNum" />
      <el-table-column label="维修保养时间" align="center" prop="maintenanceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="maintenanceContent" />
      <el-table-column label="金额" align="center" prop="maintenanceMoney" />
      <el-table-column label="保险" align="center" prop="insuranceContent" />
      <el-table-column label="保险购买时间" align="center" prop="insuranceBuyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.insuranceBuyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险金额" align="center" prop="insuranceMoney" />
      <el-table-column label="年审时间" align="center" prop="annualAuditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.annualAuditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年审金额" align="center" prop="annualAuditMoney" />
      <el-table-column label="其他" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right"  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="rescueCarNum">
        <el-select v-model="form.rescueCarId" placeholder="请选择车辆">
          <el-option
            v-for="item in carList"
            :key="item.id"
            :label="item.rescueCarNum"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="维修保养时间" prop="maintenanceTime">
          <el-date-picker clearable
                          v-model="form.maintenanceTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择维修保养时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input type="textarea"  v-model="form.maintenanceContent" placeholder="请输入维修保养项目"/>
        </el-form-item>
        <el-form-item label="金额" prop="maintenanceMoney">
          <el-input-number v-model="form.maintenanceMoney" :precision="2" :step="0.01" ></el-input-number>
        </el-form-item>
        <el-form-item label="保险">
          <el-input type="textarea" v-model="form.insuranceContent" placeholder="请输入保险相关"/>
        </el-form-item>
        <el-form-item label="保险购买时间" prop="insuranceBuyTime">
          <el-date-picker clearable
                          v-model="form.insuranceBuyTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择保险购买时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保险金额" prop="insuranceMoney">
          <el-input-number v-model="form.insuranceMoney" :precision="2" :step="0.01" ></el-input-number>
        </el-form-item>
        <el-form-item label="年审时间" prop="annualAuditTime">
          <el-date-picker clearable
                          v-model="form.annualAuditTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择年审时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="年审金额" prop="annualAuditMoney">
          <el-input-number v-model="form.annualAuditMoney" :precision="2" :step="0.01" ></el-input-number>
        </el-form-item>
        <el-form-item label="其他" prop="remark">
          <el-input type="textarea" v-model="form.remark" 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 { listRescueCarSpend, getRescueCarSpend, delRescueCarSpend, addRescueCarSpend, updateRescueCarSpend,listCar,exportCarSpend } from "./api/rescueCarSpend";

export default {
  name: "RescueCarSpend",
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 导出遮罩层
      exportLoading: false,
      // 总条数
      total: 0,
      time1:[],
      time2:[],
      time3:[],
      // 车辆消费管理表格数据
      rescueCarSpendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        rescueCarId: null,
        carNum: null,
        maintenanceTime: null,
        maintenanceContent: null,
        maintenanceMoney: null,
        insuranceContent: null,
        insuranceBuyTime: null,
        insuranceMoney: null,
        annualAuditTime: null,
        annualAuditMoney: null,
        deptId: null,
      },
      carList:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCarList();
  },
  methods: {
    getCarList(){
      listCar().then(res=>{
        this.carList = res.data.records
      })
    },
    /** 查询车辆消费管理列表 */
    getList() {
      this.loading = true;
      listRescueCarSpend(this.queryParams).then(response => {
        this.rescueCarSpendList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        rescueCarId: null,
        carNum: null,
        maintenanceTime: null,
        maintenanceContent: null,
        maintenanceMoney: null,
        insuranceContent: null,
        insuranceBuyTime: null,
        insuranceMoney: null,
        annualAuditTime: null,
        annualAuditMoney: null,
        remark: null,
        deptId: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      if (this.time1&&this.time1.length>0){
        this.queryParams.maintenanceStart = this.time1[0];
        this.queryParams.maintenanceEnd = this.time1[1];
      }
      if (this.time2&&this.time2.length>0){
        this.queryParams.maintenanceStart = this.time2[0];
        this.queryParams.maintenanceEnd = this.time2[1];
      }
      if (this.time3&&this.time3.length>0){
        this.queryParams.maintenanceStart = this.time3[0];
        this.queryParams.maintenanceEnd = this.time3[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.time1=[]
      this.time2=[]
      this.time3=[]
        this.queryParams={}
      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
      getRescueCarSpend(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) {
            updateRescueCarSpend(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRescueCarSpend(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 delRescueCarSpend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    async handleExport() {
      try {
        this.exportLoading = true
        const data = await exportCarSpend(this.queryParams)
        this.$download.excel(data, `rescueCarSpend_${new Date().getTime()}.xlsx`)
      }catch{}
      finally {
        this.exportLoading = false
      }
    }
  }
};
</script>
