<!--车辆排班   保养弹框-->
<template>
  <el-dialog title="安排用车" :visible.sync="anpai" class="user-car">
    <el-form :model="form"  ref="form" :rules="rules" label-width="140px">
      <el-form-item label="车牌号">
        <span>{{currentItem.item.numberPlate}}</span>
      </el-form-item>
      <el-form-item label="车型名称">
        <span>{{currentItem.item.vehicleModel.name}}</span>
      </el-form-item>
      <el-row>
        <el-col :span="8">
          <el-form-item label="当前停靠公司:">
            <span>{{currentItem.item.parkCompanyName}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属公司">
            <span>{{currentItem.item.subordinateBranchName}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="公里数">
            <span>{{currentItem.item.maintenanceMileage}}km</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="房车配置">
        <span>{{currentItem.item.vehicleModel.keyword}}</span>
      </el-form-item>
      <el-form-item label="选择时间" prop="times">
        <el-date-picker
          v-model="form.times"
          type="datetimerange"
          :picker-options="pickerOptions0"
          placeholder="选择时间范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="还车分公司" prop="arrivalBranchCompanyName">
        <el-autocomplete
          class="inline-input"
          v-model="form.arrivalBranchCompanyName"
          :fetch-suggestions="querySearch"
          placeholder="请输入内容"
          @select="handleSelectArrivalCompanyName"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="用途" prop="bookType">
        <!--bookType 2-租车、1-分公司使用、3-维修、4、展览、5、旅游、6、保养、7、预约中、8、禁用-->
        <el-select class="filter-item" v-model="form.bookType" placeholder="请选择用途">
          <el-option label="展览" value="4"></el-option>
          <el-option label="房车游" value="5"></el-option>
          <el-option label="保养" value="6"></el-option>
          <el-option label="禁用" value="8"></el-option>

          <!--<el-option v-for="item  in getAllUseType() " :key="item.code" :label="item.val"-->
                     <!--:value="item.code"></el-option>-->
        </el-select>
      </el-form-item>
      <el-form-item label="使用人" prop="vehicleUsername">
        <el-input v-model="form.vehicleUsername" placeholder="请输入使用人"></el-input>
      </el-form-item>
      <el-form-item label="使用人联系方式" prop="vehicleUserPhone">
        <el-input v-model="form.vehicleUserPhone" placeholder="请输入使用人联系方式"></el-input>
      </el-form-item>
      <el-form-item label="说明">
        <el-input v-model="form.remark" :rows="3" placeholder="请输入说明"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel()">取消</el-button>
      <el-button type="primary" @click="handelOk('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>
<style>
  .user-car .el-row{
    margin-bottom: 0;
  }
</style>
<script>
  import {mapGetters} from 'vuex';
  import {
    VEHICLE_CONSTANT_VEHICLE_BRAND,
    VEHICLE_CONSTANT_VEHICLE_USE,
    VEHICLE_CONSTANT_BRAND_CODE_UNKOWN,
    BELONG_TO_TYPE_OWN,
    VEHICLE_CONSTANT_USE_TYPE_UNKOWN,
    VEHICLE_CONSTANT_STATUS_NORMAL
  } from '../../../store/modules/baseInfo';
  import {
    getConstantByTypeAndCode,
    getConstantListByType,
    getAllInsuranceCompany,
    getInsuranceCompany
  } from 'api/base_info/constant/';
  import {
    getAllCompany,
    getAll
  } from 'api/base_info/branch_company/';
  import {
    bookPlan
  } from 'api/vehicle/vehicleSchedulManage';
  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";
  import ElFormItem from "../../../../node_modules/element-ui/packages/form/src/form-item.vue";
  import {
    formatDate
  } from 'utils/dateFormattor';
  export default {
    components: {
      ElFormItem,
      ElCol,
      ElRow},
    props: ["currentItem"],
    name: 'vehiclePlanModal',
    data() {
      return {
        pickerOptions0: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
          }
        },
        anpai:false,
        allCompaniesArr:[],
        form:{
          remark: "",
          vehicleUsername: "",
          arrivalBranchCompanyId: undefined,//还车分公司id
          arrivalBranchCompanyName: "",//还车分公司名称
          vehicleUserPhone:"",//使用人联系电话
          bookType: undefined,//用途
          times: [],
        },
        rules: {
          vehicleUsername: [
            {
              type: 'string',
              required: true,
              message: '请输入使用人姓名',
              trigger: 'blur'
            },
            {
              min: 0,
              max: 20,
              message: '长度小于 20 个字符',
              trigger: 'blur'
            }
          ],
          arrivalBranchCompanyName:{
            type: 'string',
            required: true,
            message: '请选择还车公司',
            trigger: 'change'
          },
          vehicleUserPhone:{
            type: 'string',
            required: true,
            message: '请输入使用人联系方式',
            trigger: 'blur'
          },
          bookType:{
            type: 'string',
            required: true,
            message: '请选择用途',
            trigger: 'change'
          },
          times: {
            required: true,
            message: '请选择时间',
          }
        }
      }
    },
    created() {
      console.log(this.currentItem);
      getAll()
        .then(response => {
          this.allCompaniesArr = response.data;
        })
      getAllCompany(codeAndBranchCompany => {
        this.allCompanies = codeAndBranchCompany;
      });
    },
    watch: {
      anpai(newValue, oldValue){
        if(!newValue){
          this.$emit("anpaiDialogEvent", false);
        }
      },
    },
    mounted() {
      this.anpai = true;
    },
    methods: {
      /**
       * 还车分公司
       */
      handleSelectArrivalCompanyName(item){
        this.form.arrivalBranchCompanyId = item.id;
        this.form.arrivalBranchCompanyName = item.name;
        console.log(item);
      },
      querySearch(queryString, cb) {
        let selectArry = [];
        this.allCompaniesArr.map(function(item){
          item.value = item.name;
          selectArry.push(item);
        });
        this.selectArry = selectArry;
        var results = queryString ? selectArry.filter(this.createFilter(queryString)) : selectArry;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.name.indexOf(queryString.toLowerCase()) != -1);
        };
      },
      /**
       * 弹框-取消
       * */
      cancel() {
        this.$emit("anpaiDialogEvent", false);
      },
      /**
       * 确定用车
       * */
      handelOk(formName){
        const set = this.$refs;
        console.log(this.form.times);
        set[formName].validate(valid => {
          if (valid) {
            let params = {
              vehicleId:this.currentItem.item.id,
              bookStartDate: formatDate(this.form.times[0], "yyyy-MM-dd hh")+":00:00",//预定开始时间
              bookEndDate: formatDate(this.form.times[1], "yyyy-MM-dd hh")+":00:00",//预定结束时间
              retCompany: this.form.arrivalBranchCompanyId,//还车分公司
              bookType: this.form.bookType,//预定车辆用途类型
              vehicleUsername: this.form.vehicleUsername,//使用人姓名
              vehicleUserPhone: this.form.vehicleUserPhone,//使用人电话
              remark: this.form.remark//说明
            };
            bookPlan(params).then(response => {
              if (response.status === 200) {
                this.$emit("anpaiDialogEvent", true);
              } else {
                this.$notify({
                  title: '失败',
                  message: '操作失败！',
                  type: 'error',
                  duration: 2000
                });
              }
            })
          }
        })
      },

    }
  }
</script>

