<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" ref="filter-container">
      <el-form :rules="rules4Query" ref="queryForm" :inline="inline" :model="listQuery">
        <el-form-item label="申请状态">
          <el-select class="filter-item" v-model="listQuery.status" placeholder="请选择申请状态">
            <el-option :key="undefined" label="无" :value="undefined"></el-option>
            <el-option v-for="(val, key, index)   in statusList " :key="val.code" :label="val.val"
                       :value="val.code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号">
          <el-input v-model="listQuery.numberPlate" placeholder="请输入车牌号"></el-input>
        </el-form-item>

        <el-form-item label="所属大区">
					<el-select class="filter-item" v-model="listQuery.zoneId" placeholder="请选择" @change="getProvinceRegions">
					  <el-option :key="undefined" label="无" :value="undefined"></el-option>
					  <el-option v-for="val in getAllZoneList" :key="val.id" :label="val.name" :value="val.id"> </el-option>
					</el-select>
				</el-form-item>

        <el-form-item label="提车公司">
					<el-select class="filter-item" v-model="listQuery.liftCompany" placeholder="请选择" @change="getAllBranchCompanyChange">
            <el-option :key="undefined" label="无" :value="undefined"></el-option>
            <el-option v-for="val in allBranchCompany" :key="val.id" :label="val.name" :value="val.id"> </el-option>
					</el-select>
				</el-form-item>
        <el-form-item label="预订月份" prop="selectedMonth4Query">
          <el-date-picker
            v-model="selectedMonth4Query"
            type="month"
            :editable="true"
            format="yyyy-MM"
            placeholder="请选择预订月份">
          </el-date-picker>
        </el-form-item>
        <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      </el-form>
    </div>


    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row
              style="width: 100%">
      <el-table-column align="center" label="车牌号" width="120">
        <template scope="scope">
          <span>{{scope.row.numberPlate}}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column align="center" label="停靠公司" width="120">
        <template scope="scope">
          <span>{{scope.row.liftCompanyName}}</span>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="申请状态">
        <template scope="scope">
          <span>{{getBookRecordStatus(scope.row.status)}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="使用人/联系方式" width="250">
        <template scope="scope">
          <span>{{scope.row.vehicleUsername}}/{{scope.row.vehicleUserPhone}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="出车用途" width="120">
        <template scope="scope">
          <span v-if="scope.row.bookType==1">租车</span>
          <span v-if="scope.row.bookType==2">用户租赁</span>
          <span v-if="scope.row.bookType==3">维修</span>
          <span v-if="scope.row.bookType==4">展览</span>
          <span v-if="scope.row.bookType==5">旅游</span>
          <span v-if="scope.row.bookType==6">保养</span>
          <span v-if="scope.row.bookType==7">预约中</span>
          <span v-if="scope.row.bookType==8">禁用</span>
          <span v-if="scope.row.bookType==9">客户用车</span>
          <span v-if="scope.row.bookType==10">其他</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="预定时间" width="220">
        <template scope="scope">
          <span>{{scope.row.bookStartDate}} 至 {{scope.row.bookEndDate}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="预定时间" width="220">
        <template scope="scope">
        <span
          v-if="checkIfApply(scope.row.status) && checkIfBooked(scope.row.bookedDate, [scope.row.bookStartDate, scope.row.bookEndDate])"
          style="color: red">
          {{getDatePeriodStr([scope.row.bookStartDate, scope.row.bookEndDate])}}(已被预订)
        </span>
          <span v-else>{{getDatePeriodStr([scope.row.bookStartDate, scope.row.bookEndDate])}}</span>

        </template>
      </el-table-column> -->

      <!--      <el-table-column align="center" label="提车地址" width="200">-->
      <!--        <template scope="scope">-->
      <!--          <span>{{scope.row.liftAddr}}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->

      <!-- <el-table-column align="center" label="目的地" width="200">
        <template scope="scope">
          <span>{{scope.row.destination}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="备注" width="200">
        <template scope="scope">
          <span>{{scope.row.remark}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="申请审核人" width="120">
        <template scope="scope">
          <span>{{scope.row.reviewerNameApply}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="取消人" width="100">
        <template scope="scope">
          <span>{{scope.row.reviewerNameCancel}}</span>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="提车公司" width="200">
        <template scope="scope">
          <span>{{scope.row.liftCompanyName}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="提车审核人" width="120">
        <template scope="scope">
          <span>{{scope.row.reviewerNameLift}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="提车时间" width="120">
        <template scope="scope">
          <span>{{scope.row.actualStartDate}}</span>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="还车公司" width="200">
        <template scope="scope">
          <span>{{scope.row.retCompanyName}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="还车审核人" width="120">
        <template scope="scope">
          <span>{{scope.row.reviewerNameReturn}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="还车时间" width="120">
        <template scope="scope">
          <span>{{scope.row.actualEndDate}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="随车物品" width="120">
        <template scope="scope">
          <el-button size="small" type="success" @click="handleAccItemInfo(scope.row)">查看随车物品</el-button>
        </template>
      </el-table-column>


      <el-table-column align="center" label="提车里程数" width="100">
        <template scope="scope">
          <span>{{scope.row.mileageLift}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="还车里程数" width="100">
        <template scope="scope">
          <span>{{scope.row.mileageRet}}</span>
        </template>
      </el-table-column>

      <el-table-column width="65" align="center" label="是否违章">
        <template scope="scope">
          <span>{{scope.row.haveViolation?'是':'否'}}</span>
        </template>
      </el-table-column> -->
        <el-table-column align="center" label="详情" width="100" fixed="right">
            <template scope="scope">
              <span style="color: #13CE66;cursor: pointer;" @click="handleDetail(scope.row)">订单详情</span>
            </template>
        </el-table-column>
      <el-table-column align="center" label="操作" width="200" fixed="right">
        <template scope="scope">
          <!-- <el-button size="small" type="success" @click="handleBookInfo(scope.row)">预订信息</el-button> -->
          <el-button  size="small" class="el-button el-button--text el-button--small"  v-if="scope.row.status==3" @click="illegalInquiry(scope.row)">违章记录</el-button>
          <el-button v-if="bookRecord_btn_prove&&scope.row.status==1" size="small" class="el-button el-button--text el-button--small"  @click="handleProve(scope.row)">通过</el-button>
          <el-button v-if="bookRecord_btn_reject&&scope.row.status==1" size="small" class="el-button el-button--text el-button--small"  style="color: red;" @click="handleReject(scope.row)">驳回</el-button>
          <template v-if="scope.row.vehicleDepartureLogVo==undefined">
            <el-button v-if="bookRecord_btn_lift&&scope.row.liftStatus==1" size="small" class="el-button el-button--text el-button--small"  @click="handleLift(scope.row)">出车</el-button>
          </template>
          <template v-if="scope.row.vehicleDepartureLogVo!=undefined">
            <el-button v-if="bookRecord_btn_ret&&scope.row.vehicleDepartureLogVo.state==0&&scope.row.retStatus==1" size="small" class="el-button el-button--text el-button--small"  @click="handleRet(scope.row)">收车</el-button>
          </template>
          <el-button v-if="bookRecord_btn_unbook&&scope.row.status==2" size="small" class="el-button el-button--text el-button--small"  style="color: red;" @click="handleUnbook(scope.row)">取消预定
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page" :page-sizes="[10,20,30,40,50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>

    <book-info-viewer ref="bookInfoViewer"></book-info-viewer>
    <acc-item-selector :selectedAccItem="selectedAccItem" :isReadOnly="true" ref="accItemSelector"></acc-item-selector>

    <!-- 查看详情 -->
    <el-dialog title="预约详情" :visible.sync="dialogDetailVisible" class="order-details">

      <el-form label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请状态">
              <span v-if="detailItem.status==1">申请中</span>
              <span v-if="detailItem.status==2">已通过</span>
              <span v-if="detailItem.status==3">已归还</span>
              <span v-if="detailItem.status==4">拒绝</span>
              <span v-if="detailItem.status==5">逾期归还</span>
              <span v-if="detailItem.status==6">取消预订</span>
              <span v-if="detailItem.status==7">已提车</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="车牌号">
              <span>{{detailItem.numberPlate}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆编号">
              <span>{{detailItem.vehicleId}}</span>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="7">
            <el-form-item label="当前停靠公司">
              <span>{{detailItem.liftCompanyName}}</span>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请账号">
              <span>{{detailItem.bookUserName}}</span>
              <span style="margin-left:10px"></span>
            </el-form-item>
          </el-col>
        </el-row>
        <hr>
        <el-row>
          <el-col :span="24">
            <el-form-item label="使用人">
              <span v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.user}}</span>
              <span style="margin-left:10px" v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.userTel}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="预定时间">
              <span>{{detailItem.bookStartDate}}</span> 至  <span>{{detailItem.bookEndDate}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="出车用途">
              <span v-if="detailItem.bookType==1">租车</span>
              <span v-if="detailItem.bookType==3">维修</span>
              <span v-if="detailItem.bookType==4">展览</span>
              <span v-if="detailItem.bookType==5">旅游</span>
              <span v-if="detailItem.bookType==6">保养</span>
              <span v-if="detailItem.bookType==8">禁用</span>
              <span v-if="detailItem.bookType==9">客户用车</span>
              <span v-if="detailItem.bookType==10">其他</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <span>{{detailItem.remark}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="随车物品">
              <span></span>
            </el-form-item>
          </el-col>
        </el-row>
        <hr>
        <el-row>
          <el-col :span="8">
            <el-form-item label="提车分公司">
              <span>{{detailItem.liftCompanyName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作人">
              <span v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.checkMan}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作时间">
              <span v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.departureTime}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="提车公里数">
              <span v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.mileageStart}}KM</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-left: 20px;">
          <el-col :span="3" v-for="(item, index) in departurePicList" :key="index" :offset="index%6 > 0 ? 1 : 0" style="margin-bottom:10px">
            <el-card>
              <img :src="item" class="image">
            </el-card>
          </el-col>
        </el-row>
        <hr>
        <el-row>
          <el-col :span="8">
            <el-form-item label="还车分公司">
              <span>{{detailItem.retCompanyName}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作人">
              <span v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.recycleMan}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作时间">
              <span v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.arrivalTime}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="还车公里数">
              <span v-if="detailItem.vehicleDepartureLogVo">{{detailItem.vehicleDepartureLogVo.mileageEnd}}KM</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-left: 20px;">
          <el-col :span="3" v-for="(item, index) in arrivalPicList" :key="index" :offset="index%6 > 0 ? 1 : 0" style="margin-bottom:10px">
            <el-card>
              <img :src="item" class="image">
            </el-card>
          </el-col>
        </el-row>
        <hr>
        <el-row>
          <el-col :span="8">
            <el-form-item label="审核人操作">
              <span>{{detailItem.reviewerNameApply}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="通过操作">
              <span></span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="detailItem.status==1" @click="handleReject(detailItem)">驳回</el-button>
        <el-button v-if="detailItem.status==1" type="primary" @click="handleProve(detailItem)">通过</el-button>
      </div>
    </el-dialog>

    <!--     出车对话框相关html元素  -->
    <el-dialog title="出车" :visible.sync="dialogForm4LiftVisible">
      <el-form :model="departureForm" :rules="departureFormrules" ref="departureForm" label-width="120px">
        <el-form-item label="车牌号">
          <el-input v-model="departureForm.numberPlate" disabled></el-input>
        </el-form-item>
        <el-form-item label="出车时间">
          <el-date-picker v-model="departureForm.departureDate" type="date" disabled
                          format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="出车分公司">
          <el-autocomplete
            class="inline-input"
            v-model="departureForm.departureBranchCompanyName"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            disabled
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="目的地分公司" prop="expectArrivalBranchCompanyName">
          <el-autocomplete
            class="inline-input"
            v-model="departureForm.expectArrivalBranchCompanyName"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            disabled
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="用途">
          <el-input v-model="departureForm.use" placeholder="请输入出车用途" disabled></el-input>
        </el-form-item>
        <el-form-item label="使用人">
          <el-input v-model="departureForm.user" placeholder="请输入出车使用人" disabled></el-input>
        </el-form-item>
        <el-form-item label="使用人联系方式">
          <el-input v-model="departureForm.userTel" placeholder="请输入出车使用人联系方式" disabled></el-input>
        </el-form-item>
        <el-form-item label="验车人" prop="checkMan">
          <el-input v-model="departureForm.checkMan" placeholder="请输入出车验车人"></el-input>
        </el-form-item>
        <el-form-item label="验车人联系方式" prop="checkManTel">
          <el-input v-model="departureForm.checkManTel" placeholder="请输入出车验车人联系方式"></el-input>
        </el-form-item>
        <el-form-item label="公里数" prop="mileage">
          <el-input v-model.number="departureForm.mileage" placeholder="请输入出车时公里数"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="departureForm.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
        <el-row>
          <el-col>
            <el-form-item label="出车截图" :style="{display:'block'}" required>
              <el-upload
                class="upload-demo"
                :headers="getHeaderWithToken"
                :action="BASE_API+'/api/universal/file/app/unauth/admin/upload'"
                :on-remove="handleDepartureRemove"
                :file-list="departurePicList"
                :on-success="handleDepartureSuccess"
                list-type="picture-card">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog4Lift('departureForm')">取 消</el-button>
        <el-button type="primary" @click="liftOk('departureForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!--     提车对话框相关html元素  -->
    <el-dialog title="收车" :visible.sync="dialogForm4RetVisible">
      <el-form :model="arrivalForm" :rules="arrivalFormrules" ref="form" label-width="90px">
        <el-form-item label="车牌号">
          <el-input v-model="arrivalForm.numberPlate" disabled></el-input>
        </el-form-item>
        <el-form-item label="收车时间">
          <el-date-picker v-model="arrivalForm.arrivalDate" type="date" disabled
                          format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="还车分公司">
          <el-autocomplete
            class="inline-input"
            v-model="arrivalForm.arrivalBranchCompanyName"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            disabled
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="收车人" prop="recycleMan">
          <el-input v-model="arrivalForm.recycleMan" placeholder="请输入收车人"></el-input>
        </el-form-item>
        <el-form-item label="收车人联系方式" prop="recycleManTel">
          <el-input v-model="arrivalForm.recycleManTel" placeholder="请输入收车人联系方式"></el-input>
        </el-form-item>
        <el-form-item label="公里数" prop="mileage">
          <el-input v-model.number="arrivalForm.mileage" placeholder="请输入收车时公里数"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="departureForm.remark" placeholder="请输入备注"></el-input>
        </el-form-item>
        <el-row>
          <el-col>
            <el-form-item label="收车截图" :style="{display:'block'}" required>
              <el-upload
                class="upload-demo"
                :headers="getHeaderWithToken"
                :action="BASE_API+'/api/universal/file/app/unauth/admin/upload'"
                :on-remove="handleArrivalRemove"
                :file-list="arrivalPicList"
                :on-success="handleArrivalSuccess"
                list-type="picture-card">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog4Ret('form')">取 消</el-button>
        <el-button type="primary" @click="ret('form')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="违章查询" :visible.sync="illegalVisible">
      <el-form>
        <el-row>
          <el-col>
            <el-form-item label="违章截图" :style="{display:'block'}">
              <el-upload
                class="upload-demo"
                :headers="getHeaderWithToken"
                :action="BASE_API+'/api/universal/file/app/unauth/admin/upload'"
                :on-remove="handleRemove"
                :file-list="fileList2"
                :on-success="handleSuccess"
                list-type="picture-card">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
              <el-form-item label="违章罚款">
                <el-input v-model="price" type="number" placeholder="请输入违章罚款金额" style="width: 50%;margin-right:10px"></el-input>元
              </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="okHandler('form')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    page,
    prove,
    reject,
    getObj,
    lift,
    ret,
    unbook,
    getOneIllegalRow,
    saveOrderViolation,
    departureVehicle,
    arrivalVehicle
  } from 'api/vehicle/bookRecord';


  import {
    getAllCompany,
    getAll
  } from 'api/base_info/branch_company/';
  import {
	  getAllZone
  } from 'api/base_info/constant/';
   import {
		getAllBranchCompanyByZoneId
  } from 'api/order/rentVehicle';
  import {
    formatDate
  } from 'utils/dateFormattor';

  import rsCode from '../../../utils/rsCode';

  import {mapGetters} from 'vuex';

  import {
    toEast8Date,
    deepCopyDate,
    newEast8Date
  } from 'utils/dateUtils';
  import {
    getToken
  } from '../../../utils/auth';
  import {
    BOOK_RECORD_STATUS_APPLY,
    BOOK_RECORD_STATUS_PROVED,
    BOOK_RECORD_STATUS_LIFT
  } from '../../../store/modules/baseInfo';

  import bookInfoViewer from '../bookInfoViewer';
  import accItemSelector from '../accompanyingItem/accItemSelector.vue';

  export default {
    name: 'bookRecord',
    components: {
      bookInfoViewer,
      accItemSelector
    },
    data() {
      return {
        BASE_API: process.env.BASE_API,
        departureFormrules:{
          checkMan:{
            required: true,
            message: '请输入验车人',
            trigger: 'blur'
          },
          checkManTel:{
            required: true,
            message: '请输入验车人联系方式',
            trigger: 'blur'
          },
          mileage:{
            type:"number",
            required: true,
            message: '请输入公里数',
            trigger: 'blur'
          }
        },
        departureForm:{
          vehicleId: null,
          departureBranchCompanyId: null,//出发地分公司id
          departureBranchCompanyName: '',//出发地分公司名称
          use: null,
          user: null,
          userTel: null,
          checkMan: null,
          checkManTel: null,
          mileage: null,
          numberPlate: null,
          expectArrivalBranchCompanyId: null,//目的地分公司id
          expectArrivalBranchCompanyName: '',//目的地分公司名称
          bookRecordId:null,
          departurePic:'',
          remark:null,
          bookStartDate: null,
        },
        departurePicList:[],
        arrivalPicList:[],
        arrivalForm:{
          vehicleId: null,
          numberPlate: null,
          arrivalDate: null,
          recycleMan: null,
          recycleManTel: null,
          mileage: null,
          arrivalBranchCompanyId: null,
          arrivalBranchCompanyName: '',
          arrivalPic:'',
          remark:'',
          bookRecordId:null,
        },
        arrivalFormrules:{
          recycleMan:{
            required: true,
            message: '请输入收车人',
            trigger: 'blur'
          },
          recycleManTel:{
            required: true,
            message: '请输入收车人联系方式',
            trigger: 'blur'
          },
          mileage:{
            type:"number",
            required: true,
            message: '请输入收车公里数',
            trigger: 'blur'
          }
        },
        departurePicList:[],
        statusList:[
          {
            code:'1',
            val:'申请中'
          },
          {
            code:'2',
            val:'已通过'
          },
          {
            code:'3',
            val:'已归还'
          },
          {
            code:'4',
            val:'拒绝'
          },
          {
            code:'5',
            val:'逾期归还'
          },
          {
            code:'6',
            val:'取消预订'
          },
          {
            code:'7',
            val:'已提车'
          },
        ],
        detailId:'',
        illegalVisible:false,
        price: undefined,//违章罚款金额
        fileList2:[],//违章截图
        detailItem:{},
        dialogDetailVisible:false,
        baranchQuery: {
					zoneId:null
        },
        allZoneArr:[],//全部片区
				allBranchCompany:[],
        state2: '',//搜索-分公司名称
        rules4Query: {

        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          zoneId:undefined,
					liftCompany: undefined,
          numberPlate: undefined,
          selectedMonth: undefined,
          status: undefined
        },
        inline: true,
        dialogFormVisible: false,
        dialogStatus: '',
        bookRecord_btn_prove: false,
        bookRecord_btn_unbook: false,
        bookRecord_btn_reject: false,
        bookRecord_btn_lift: false,
        bookRecord_btn_ret: false,
        dialogForm4LiftVisible: false,
        dialogForm4RetVisible: false,
        allCompanies: {},
        allCompaniesArr: [],
        tableKey: 0,
        form4Lift: {
          state1: '',
          id: undefined,
          mileageLift: undefined,
          liftCompany: undefined,
          liftRemark: undefined
        },
        rules4Lift: {
          liftCompany: [
            {
              required: true,
              message: '请选择提车分公司',
              trigger: 'blur',
              type: 'integer'
            }
          ],
          mileageLift: [
            {
              required: true,
              message: '请输入当前里程数',
              trigger: 'blur',
              type: 'integer'
            },
            {
              validator: (rule, value, callback) => {
                if (this.$utils.isUndefined(value) || this.$utils.isNull(value)) {
                  return callback();
                }
                if (!this.$utils.isInteger(value)) {
                  return callback(new Error('里程数必须为整数'));
                }
                if (value < 0 || value > 9999999999) {
                  return callback(new Error('里程数合法范围：[0 - 9999999999]'));
                }
                callback();
              },
              trigger: 'blur'
            }
          ],
          retRemark: [
            {
              min: 0,
              max: 2000,
              message: '长度小于 2000 个字符',
              trigger: 'blur'
            }
          ]
        },
        form4Ret: {
          id: undefined,
          mileageRet: undefined,
          haveViolation: undefined,
          retCompany: undefined,
          retRemark: undefined
        },
        rules4Ret: {
          retCompany: [
            {
              required: true,
              message: '请选择提车分公司',
              trigger: 'blur',
              type: 'integer'
            }
          ],
          mileageRet: [
            {
              required: true,
              message: '请输入当前里程数',
              trigger: 'blur',
              type: 'integer'
            },
            {
              validator: (rule, value, callback) => {
                if (this.$utils.isUndefined(value) || this.$utils.isNull(value)) {
                  return callback();
                }
                if (!this.$utils.isInteger(value)) {
                  return callback(new Error('里程数必须为整数'));
                }
                if (value < 0 || value > 9999999999) {
                  return callback(new Error('里程数合法范围：[0 - 9999999999]'));
                }
                callback();
              },
              trigger: 'blur'
            }
          ],
          retRemark: [
            {
              min: 0,
              max: 2000,
              message: '长度小于 2000 个字符',
              trigger: 'blur'
            }
          ]
        },
        selectedAccItem: undefined
      }
    },
    created() {
      this.getList();
      getAll()
        .then(response => {
          this.allCompaniesArr = response.data;
        })
      getAllCompany(codeAndBranchCompany => {//初始化公司列表
        this.allCompanies = codeAndBranchCompany;
      });
      this.bookRecord_btn_prove = this.elements['bookRecord:btn_prove'];
      this.bookRecord_btn_reject = this.elements['bookRecord:btn_reject'];
      this.bookRecord_btn_unbook = this.elements['bookRecord:btn_unbook'];
      this.bookRecord_btn_lift = this.elements['bookRecord:btn_lift'];
      this.bookRecord_btn_ret = this.elements['bookRecord:btn_ret'];
    },
    computed: {
      ...mapGetters([
        'elements',
        'bookRecordStatus'
      ]),
      getHeaderWithToken() {
        return {Authorization: getToken()};
      },
      //获取大区列表
			getAllZoneList(){
			  return getAllZone();
			},
      selectedMonth4Query: {
        get: function () {
          if (this.$utils.isString(this.listQuery.selectedMonth) && this.listQuery.selectedMonth !== '') {
            return toEast8Date(this.listQuery.selectedMonth);
          }
          return undefined;
        },
        set: function (date) {
          if (this.$utils.isDate(date)) {
            this.listQuery.selectedMonth = formatDate(date, 'yyyy-MM');
          } else {
            this.listQuery.selectedMonth = undefined;
          }
        }
      }
    },
    methods: {
      /**
       * 确定
       * */
      okHandler() {
        let arr = [];
        this.fileList2.map(function(item){
          arr.push(item.url);
        });
        let params = {
          illegalPic:arr.join(","),
          illegalAmount:this.price,
          id: this.detailId,
        };
        saveOrderViolation(params).then(response => {
          if (response.status === 200) {
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            });
            this.illegalVisible = false;
            this.getList();
          } else {
            this.$notify({
              title: '创建失败',
              message: '操作失败！',
              type: 'error',
              duration: 2000
            });
          }
        });
      },
       /**
       * 上传成功
       * */
      handleSuccess(res, file) {
        let c = {url: res.data};
        this.fileList2.push(c);
        this.showLoadingBody = false;
      },
      /**
       * 删除违章图片
       * */
      handleRemove(file, fileList1){
        this.fileList2 = fileList1;
      },
       /**
       * 出车上传成功
       * */
      handleDepartureSuccess(res, file) {
        let c = {url: res.data};
        this.departurePicList.push(c);
        this.showLoadingBody = false;
      },
      /**
       * 删除出车图片
       * */
      handleDepartureRemove(file, fileList2){
        this.departurePicList = fileList2;
      },
       /**
       * 上传成功
       * */
      handleArrivalSuccess(res, file) {
        let c = {url: res.data};
        this.arrivalPicList.push(c);
        this.showLoadingBody = false;
      },
      /**
       * 删除违章图片
       * */
      handleArrivalRemove(file, fileList3){
        this.arrivalPicList = fileList3;
      },
      /**
       * 操作-违章查询按钮，显示违章弹框
       */
      illegalInquiry(row){
        var t = this;
        this.fileList2 = [];
        this.price = '';
        if(row.vehicleDepartureLogVo.illegalPic){
          let arr = row.vehicleDepartureLogVo.illegalPic.split(",");
          let fileList2 = [];
          let p = {};
          arr.map(function(item){
            p = {
              url: item
            };
            fileList2.push(p);
          });
          t.fileList2 = fileList2;
        }
        if(row.vehicleDepartureLogVo.illegalAmount){
          t.price = parseFloat(row.vehicleDepartureLogVo.illegalAmount);
        }
        this.detailId = row.vehicleDepartureLogVo.id;
        this.illegalVisible = true;
      },
      /**
       * 弹框-取消
       * */
      cancel() {
        this.illegalVisible = false;
      },
      handleDetail(row){
        var t = this
        this.detailItem = row;
        if(row.vehicleDepartureLogVo!= undefined){
          if(row.vehicleDepartureLogVo.departurePic!= undefined){
            let arr = row.vehicleDepartureLogVo.departurePic.split(",");
            let departurePicList = [];
            let p = {};
            arr.map(function(item){
              departurePicList.push(item);
            });
            t.departurePicList = departurePicList;
          }
          if(row.vehicleDepartureLogVo.arrivalPic!= undefined){
            let Arr = row.vehicleDepartureLogVo.arrivalPic.split(",");
            let arrivalPicList = [];
            let p = {};
            Arr.map(function(item){
              arrivalPicList.push(item);
            });
            t.arrivalPicList = arrivalPicList;
          }
        }
        this.dialogDetailVisible = true;
      },
      //监听change事件
			 getProvinceRegions(item) {
					this.listQuery.zoneId = item
					this.baranchQuery.zoneId = item
          this.listQuery.liftCompany = undefined;
					getAllBranchCompanyByZoneId(this.baranchQuery)
					.then(response => {
					  this.allBranchCompany = response.data;
					})
			 },
			 getAllBranchCompanyChange(item) {
				 this.listQuery.liftCompany = item
			 },
      querySearch(queryString, cb) {
        let selectArry = [];
        let iitem = {
          value: "全部",
          name: "全部"
        };
        selectArry.push(iitem);
        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);
        };
      },
      /**
       * 分公司搜索
       * */
      handleSelect2(item){
        this.listQuery.subordinateBranch = item.id;
        console.log(item);
      },
      /**
       * 提车分公司
       * */
      handleSelect1(item){
        this.form4Lift.liftCompany = item.id;
        console.log(item);
      },
      cancelDialog4Lift(formName) {
        this.dialogForm4LiftVisible = false;
        this.$refs[formName].resetFields();
      },
      cancelDialog4Ret(formName) {
        this.dialogForm4RetVisible = false;
        this.$refs[formName].resetFields();
      },
      /**
       * 出车弹框-确定
       * */
      liftOk(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            var arr = [];
            this.departurePicList.map(function(item){
              arr.push(item.url);
            });
            this.departureForm.departurePic=arr.join(",");
            departureVehicle(this.departureForm).then(response => {
              if (response.status == 200) {
                this.dialogForm4LiftVisible = false;
                this.$notify({
                  title: '成功',
                  message: '出车成功',
                  type: 'success',
                  duration: 2000
                });
                this.cleanDepartureForm();
                this.getList();
              } else {
                this.$notify({
                  title: '失败',
                  message: response.message,
                  type: 'error',
                  duration: 2000
                });
              }
            });
          } else {
            return false;
          }
        });
      },
      handleBookInfo(row) {
        this.$refs.bookInfoViewer.handleBookInfo(row.vehicle);
      },
      handleAccItemInfo(row) {
        this.selectedAccItem = row.accItemAndAmount;
        this.$refs.accItemSelector.show();
      },
      checkIfBooked(bookedDate, [startDateStr, endDateStr]) {
        if (this.$utils.isInteger(bookedDate) &&
          this.$utils.isString(startDateStr) && startDateStr !== '' &&
          this.$utils.isString(endDateStr) && endDateStr !== '') {
          let startDate = toEast8Date(startDateStr);
          let endDate = toEast8Date(endDateStr);
          for (; this.$utils.toDateString(startDate, 'yyyy-MM-dd') <= this.$utils.toDateString(endDate, 'yyyy-MM-dd');
                 startDate.setDate(startDate.getDate() + 1)) {
            if ((bookedDate & (1 << (startDate.getDate() - 1))) !== 0) {
              return true;
            }
          }
        }
        return false;
      },
      checkIfApply(code) {
        return code === BOOK_RECORD_STATUS_APPLY;
      },
      checkIfProved(code) {
        return code === BOOK_RECORD_STATUS_PROVED;
      },
      checkIfLift(code) {
        return code === BOOK_RECORD_STATUS_LIFT;
      },
      getList() {
        this.listLoading = true;
        for(var key in this.listQuery){
          if(this.listQuery[key]==''){
            this.listQuery[key]=undefined
          }
        }
        page(this.listQuery)
          .then(response => {
            this.list = response.data.data;
            this.total = response.data.totalCount;
            this.listLoading = false;
          })
      },
      getBookRecordStatus: function (code) {
        if (!this.$utils.isInteger(code)) {
          return '未知';
        }
        return this.bookRecordStatus[code + ''].val;
      },
      getAllBookRecordStatus: function () {
        return this.bookRecordStatus;
      },
      getDatePeriodStr([startDate, endDate]) {
        if (this.$utils.isString(startDate) && startDate !== '' &&
          this.$utils.isString(endDate) && endDate !== '') {
          return formatDate(toEast8Date(startDate), 'yyyy-MM-dd') + ' 至 ' + formatDate(toEast8Date(endDate), 'yyyy-MM-dd');
        }
        return '未知';
      },
      /**
       * 搜索按钮
       * */
      handleFilter() {
        this.listQuery.page = 1;
        this.$refs.queryForm.validate(valid => {
          if (valid) {
            this.getList();
          } else {
            return false;
          }
        });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleProve(row) {
        this.$confirm('确定批准申请?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          prove(row.id)
            .then(response => {
              if (response.code === rsCode.code.VEHICLE_BOOKED_INFO_ALREADY_CHANGED) {
                this.$notify({
                  title: '失败',
                  message: rsCode.msg[rsCode.code.VEHICLE_BOOKED_INFO_ALREADY_CHANGED],
                  type: 'error',
                  duration: 2000
                });
              } else if (response.code === rsCode.code.VEHICLE_BOOKED_RECORD_ALREADY_CHANGED) {
                this.$notify({
                  title: '失败',
                  message: rsCode.msg[rsCode.code.VEHICLE_BOOKED_RECORD_ALREADY_CHANGED],
                  type: 'error',
                  duration: 2000
                });
              } else if (response.code === rsCode.RS_CODE_SUC) {
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                });
                if(this.dialogDetailVisible){
                  this.dialogDetailVisible = false
                }
              }
              this.getList();
            });
        });
      },
      handleReject(row) {
        this.$confirm('确定拒绝申请?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          reject(row.id)
            .then(response => {
              if (response.code === rsCode.code.VEHICLE_BOOKED_INFO_ALREADY_CHANGED) {
                this.$notify({
                  title: '失败',
                  message: rsCode.msg[rsCode.code.VEHICLE_BOOKED_INFO_ALREADY_CHANGED],
                  type: 'error',
                  duration: 2000
                });
              } else if (response.code === rsCode.code.VEHICLE_BOOKED_RECORD_ALREADY_CHANGED) {
                this.$notify({
                  title: '失败',
                  message: rsCode.msg[rsCode.VEHICLE_BOOKED_RECORD_ALREADY_CHANGED],
                  type: 'error',
                  duration: 2000
                });
              } else if (response.code === rsCode.RS_CODE_SUC) {
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                });
                if(this.dialogDetailVisible){
                  this.dialogDetailVisible = false
                }
              }
              this.getList();
            });
        });
      },
      /**
       * 操作-出车
       * @param row
       */
      handleLift(row) {
        this.cleanDepartureForm();
        this.departurePicList = [];
        if(row.bookType==1){
          this.departureForm.use = '租车'
        }else if(row.bookType==2){
          this.departureForm.use = '用户租赁'
        }else if(row.bookType==3){
          this.departureForm.use = '维修'
        }else if(row.bookType==4){
          this.departureForm.use = '展览'
        }else if(row.bookType==5){
          this.departureForm.use = '旅游'
        }else if(row.bookType==6){
          this.departureForm.use = '保养'
        }else if(row.bookType==7){
          this.departureForm.use = '预约中'
        }else if(row.bookType==8){
          this.departureForm.use = '禁用'
        }else if(row.bookType==9){
          this.departureForm.use = '客户用车'
        }else if(row.bookType==10){
          this.departureForm.use = '其他'
        }
        this.departureForm.bookRecordId=row.id
        this.departureForm.vehicleId = row.vehicleId
        this.departureForm.numberPlate = row.numberPlate
        this.departureForm.departureDate = Date();
        this.departureForm.departureBranchCompanyId = row.liftCompany
        this.departureForm.departureBranchCompanyName = row.liftCompanyName
        this.departureForm.user = row.vehicleUsername
        this.departureForm.userTel = row.vehicleUserPhone
        // if(row.vehicleDepartureLogVo!=undefined){
        //   this.departureForm.user = row.vehicleDepartureLogVo.user?row.vehicleDepartureLogVo.user:''
        //   this.departureForm.userTel = row.vehicleDepartureLogVo.userTel?row.vehicleDepartureLogVo.userTel:''
        // }else{
        //   this.departureForm.user = ''
        //   this.departureForm.userTel = ''
        // }
        this.departureForm.expectArrivalBranchCompanyId = row.retCompany
        this.departureForm.expectArrivalBranchCompanyName = row.retCompanyName
        this.dialogForm4LiftVisible = true;
      },
      /**
       * 操作-收车
       * @param row
       */
      handleRet(row) {
        this.arrivalPicList = [];
        this.arrivalForm.vehicleId = row.vehicleId
        this.arrivalForm.numberPlate = row.numberPlate
        this.arrivalForm.arrivalDate = Date();
        this.arrivalForm.arrivalBranchCompanyId = row.retCompany
        this.arrivalForm.arrivalBranchCompanyName = row.retCompanyName
        this.dialogForm4RetVisible = true;
      },
      ret(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            var arr = [];
            this.arrivalPicList.map(function(item){
              arr.push(item.url);
            });
            this.arrivalForm.arrivalPic=arr.join(",");
            arrivalVehicle(this.arrivalForm).then(response => {
              if (response.status == 200) {
                this.dialogForm4RetVisible = false;
                this.$notify({
                  title: '成功',
                  message: '收车成功',
                  type: 'success',
                  duration: 2000
                });
                this.cleanArrivalForm();
                this.getList();
              } else {
                this.$notify({
                  title: '失败',
                  message: response.message,
                  type: 'error',
                  duration: 2000
                });
              }
            });
          } else {
            return false;
          }
        });
      },
      handleUnbook(row) {
        this.$confirm('确定取消预定?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          unbook(row.id)
            .then(response => {
              if(response.status==500){
                this.$notify({
                  title: '失败',
                  message: response.message,
                  type: 'error',
                  duration: 2000
                });
              }else if (response.code === rsCode.code.VEHICLE_BOOKED_INFO_ALREADY_CHANGED) {
                this.$notify({
                  title: '失败',
                  message: rsCode.msg[rsCode.code.VEHICLE_BOOKED_INFO_ALREADY_CHANGED],
                  type: 'error',
                  duration: 2000
                });
              } else if (response.code === rsCode.code.VEHICLE_BOOKED_RECORD_ALREADY_CHANGED) {
                this.$notify({
                  title: '失败',
                  message: rsCode.msg[rsCode.code.VEHICLE_BOOKED_RECORD_ALREADY_CHANGED],
                  type: 'error',
                  duration: 2000
                });
              } else if (response.code === rsCode.RS_CODE_SUC) {
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                });
              }
              this.getList();
            }).catch(response=>{
              console.log(response)
            }

            );
        });
      },
      /**
       * 清空出车弹框数据
       */
      cleanDepartureForm() {
        this.departureForm = {
          vehicleId: null,
          departureBranchCompanyId: null,//出发地分公司id
          departureBranchCompanyName: '',//出发地分公司名称
          use: null,
          user: null,
          userTel: null,
          checkMan: null,
          checkManTel: null,
          mileage: null,
          numberPlate: null,
          expectArrivalBranchCompanyId: null,//目的地分公司id
          expectArrivalBranchCompanyName: '',//目的地分公司名称
          bookRecordId:null,
          departurePic:'',
          remark:null,
          bookStartDate: null,
        };
      },
      cleanArrivalForm(){
        this.arrivalForm = {
          vehicleId: null,
          numberPlate: null,
          arrivalDate: null,
          recycleMan: null,
          recycleManTel: null,
          mileage: null,
          arrivalBranchCompanyId: null,
          arrivalBranchCompanyName: '',
          arrivalPic:'',
          remark:'',
          bookRecordId:null
        }
      },
    }
  }
</script>

<style scoped>
   .order-details .el-form-item{
     margin-bottom: 0px !important;
   }
   .order-details hr {
      height: 1px;
      background-color: #ccc;
      border: none;
    }
    .image {
      width: 100%;
      height: 110px;
      display: block;
    }
    .el-card__body,.el-card{padding: 0px;width: 110px;height: 110px;text-align: center;}
</style>
