<template>
  <!-- 车辆派送 -->
  <el-dialog :title="title" v-if="showFlag" :visible.sync="showFlag" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">

      <el-row>
        <el-col :span="12">
          <el-form-item label="派车编号" prop="dispatchCode" disabled="" v-if="form.dispatchId != null">
            <el-input v-model="form.dispatchCode" placeholder="请输入派车编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用车说明" prop="dispatchTitle">
            <el-input v-model="form.dispatchTitle" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="车辆" prop="vehicleId">
            <el-input
              v-model="form.vehicleName"
              readonly
              placeholder="请选择车辆"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="$refs['VehicleSelectRef'].showFlag = true"
              ></el-button>
            </el-input>
            <VehicleSelect
              ref="VehicleSelectRef"
              @onSelected="onVehicleSelect"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="驾驶人" prop="driverBy">
            <el-input v-model="form.driverBy" placeholder="请输入驾驶人" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="派车时间" prop="dispatchDate">
            <el-date-picker clearable
                            v-model="form.dispatchDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择派车时间">
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="派时里程" prop="startMileage">
            <el-input v-model="form.startMileage" placeholder="请输入派时里程" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-if="optType !='view'">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getVehicleDispatch, addVehicleDispatch, updateVehicleDispatch } from "@/api/mes/md/vehicleDispatch";
  import VehicleSelect from "@/views/mes/md/vehicle/components/singleSelect.vue";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "DispatchBack",
    dicts: ["sys_yes_no", "mes_item_product"],
    components: { VehicleSelect },
    data() {
      return {
        showFlag: false,
        dispatchId: null,
        optType:null,
        // 表单参数
        form: {
          dispatchId: null,
          dispatchCode: null,
          dispatchTitle: null,
          vehicleId: null,
          vehicleName: null,
          dispatchBy: null,
          driverBy: null,        dispatchDate: null,
          backDate: null,        travelMileage: null,
          startMileage: null,        endMileage: null,
          oilConsumption: null,        remark: null},
        // 表单校验
        rules: {
          vehicleId: [
            { required: true, message: "车辆不能为空", trigger: "blur" }
          ],
          vehicleId: [
            { required: true, message: "车辆不能为空", trigger: "blur" }
          ],
        },
        title:'确认派送'
      };
    },
    created() {

    },
    methods: {
      // 取消按钮
      cancel() {
        this.showFlag = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dispatchId: null,
          dispatchCode: null,
          dispatchTitle: null,
          vehicleId: null,
          vehicleName: null,
          dispatchBy: null,
          driverBy: null,        dispatchDate: null,
          backDate: null,        travelMileage: null,
          startMileage: null,        endMileage: null,
          oilConsumption: null,        remark: null
        };
        this.resetForm("form");
      },
      onVehicleSelect(row) {
        if (row != undefined && row != null) {
          this.form.vehicleId = row.vehicleId;
          this.form.vehicleName = row.vehicleName;
        }
      },
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.dispatchId != null) {
              updateVehicleDispatch(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.showFlag=false;
                this.$emit("reload");
              });
            } else {
              addVehicleDispatch(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.showFlag=false;
                this.$emit("reload");
              });
            }
          }
        });
      },
    },
  };
</script>
