<template>
  <div class="app-container">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="派车编号" prop="dispatchCode">
        <el-input
          v-model="queryParams.dispatchCode"
          placeholder="请输入派车编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="dispatchTitle">
        <el-input
          v-model="queryParams.dispatchTitle"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派车人" prop="dispatchBy">
        <el-input
          v-model="queryParams.dispatchBy"
          placeholder="请输入派车人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驾驶人" prop="driverBy">
        <el-input
          v-model="queryParams.driverBy"
          placeholder="请输入驾驶人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派车时间" prop="dispatchDate">
        <el-date-picker clearable
          v-model="queryParams.dispatchDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择派车时间">
        </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"
          v-hasPermi="['md:vehicleDispatch:add']"
        >新增</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="['md:vehicleDispatch:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:vehicleDispatch:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleDispatchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="派车编号" width="120" align="center" prop="dispatchCode">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['md:vehicleDispatch:query']"
          >{{ scope.row.dispatchCode }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="dispatchTitle" />
      <el-table-column label="车辆名称" align="center" prop="vehicleName" />
      <el-table-column label="派车人" align="center" prop="dispatchBy" />
      <el-table-column label="驾驶人" align="center" prop="driverBy" />
      <el-table-column label="派车时间" align="center" prop="dispatchDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dispatchDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="返回时间" align="center" prop="backDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.backDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="派时里程" align="center" prop="startMileage" />
      <el-table-column label="还车里程" align="center" prop="endMileage" />
      <el-table-column label="本次里程数" align="center" prop="travelMileage" />
      <el-table-column label="耗油量" align="center" prop="oilConsumption" />
      <el-table-column label="单据状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.vehicle_dispatch_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDispatch(scope.row)"
            v-hasPermi="['md:vehicleDispatch:edit']"
            v-if="scope.row.status==1"
          >确认派送</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBack(scope.row)"
            v-if="scope.row.status==2"
            v-hasPermi="['md:vehicleDispatch:edit']"
          >车辆回收</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status==1"
            v-hasPermi="['md:vehicleDispatch:remove']"
          >删除</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"
    />

    <DispatchBack ref="DispatchBakRef" @reload="getList()"/>
    <DispatchAdd ref="DispatchAddRef" @reload="getList()"/>

  </div>
</template>

<script>
import { listVehicleDispatch, getVehicleDispatch, delVehicleDispatch, addVehicleDispatch, updateVehicleDispatch } from "@/api/mes/md/vehicleDispatch";
import VehicleSelect from "@/views/mes/md/vehicle/components/singleSelect.vue";
import DispatchBack from "./components/dispatchBack.vue";
import DispatchAdd from "./components/dispatchAdd.vue";

export default {
  name: "VehicleDispatch",
  dicts: [
    "vehicle_dispatch_status",
  ],
  components: { DispatchBack,DispatchAdd },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 派车记录表格数据
      vehicleDispatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      backOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dispatchCode: null,        dispatchTitle: null,        vehicleId: null,        dispatchBy: null,        driverBy: null,        dispatchDate: null,        backDate: null,        travelMileage: null,        startMileage: null,        endMileage: null,        oilConsumption: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询派车记录列表 */
    getList() {
      this.loading = true;
      listVehicleDispatch(this.queryParams).then(response => {
        this.vehicleDispatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.backOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dispatchId: null,        dispatchCode: null,        dispatchTitle: null,        vehicleId: null,        dispatchBy: null,        driverBy: null,        dispatchDate: null,        backDate: null,        travelMileage: null,        startMileage: null,        endMileage: null,        oilConsumption: null,        remark: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    onVehicleSelect(row) {
      if (row != undefined && row != null) {
        this.form.itemId = row.itemId;
        this.form.itemName = row.itemName;
        this.form.unit = row.unitOfMeasure;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dispatchId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs['DispatchAddRef'].showFlag=true;
      this.$refs['DispatchAddRef'].optType=null;
      this.$refs['DispatchAddRef'].title = "添加派车记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const dispatchId = row.dispatchId || this.ids
      getVehicleDispatch(dispatchId).then(response => {
        this.$refs['DispatchAddRef'].showFlag=true;
        this.$refs['DispatchAddRef'].optType=null;
        this.$refs['DispatchAddRef'].title = "修改派车记录";
        this.$refs['DispatchAddRef'].form=response.data;
      });
    },
    // 确认派送
    handleDispatch(row) {
      const dispatchId = row.dispatchId || this.ids
      getVehicleDispatch(dispatchId).then(response => {
        this.$refs['DispatchAddRef'].showFlag=true;
        this.$refs['DispatchAddRef'].optType=null;
        this.$refs['DispatchAddRef'].title = "确认派车";
        response.data.status = 2;
        this.$refs['DispatchAddRef'].form=response.data;
      });
    },
    handleBack(row) {
      const dispatchId = row.dispatchId || this.ids
      getVehicleDispatch(dispatchId).then(response => {
        this.$refs['DispatchBakRef'].reset();
        response.data.status = 3;
        this.$refs['DispatchBakRef'].form=response.data;
        this.$refs['DispatchBakRef'].showFlag=true;
        this.$refs['DispatchBakRef'].optType=null;
        this.$refs['DispatchBakRef'].dispatchId=dispatchId;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dispatchId != null) {
            updateVehicleDispatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.backOpen = false;
              this.getList();
            });
          } else {
            addVehicleDispatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.backOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    // 查询明细按钮操作
    handleView(row) {
      const dispatchId = row.dispatchId || this.ids
      getVehicleDispatch(dispatchId).then(response => {
        if(row.status == 1 || row.status == 2){
          this.$refs['DispatchAddRef'].showFlag=true;
          this.$refs['DispatchAddRef'].title = "查看";
          this.$refs['DispatchAddRef'].form=response.data;
          this.$refs['DispatchAddRef'].optType='view';
        }else{
          this.$refs['DispatchBakRef'].showFlag=true;
          this.$refs['DispatchBakRef'].title = "查看";
          this.$refs['DispatchBakRef'].form=response.data;
          this.$refs['DispatchBakRef'].optType='view';
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dispatchIds = row.dispatchId || this.ids;
      this.$modal.confirm('是否确认删除派车记录编号为"' + dispatchIds + '"的数据项？').then(function() {
        return delVehicleDispatch(dispatchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/vehicleDispatch/export', {
        ...this.queryParams
      }, `vehicleDispatch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
