<!--车辆排班记录-->
<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input style="width: 200px;" placeholder="车牌号"
                v-model="listQuery.numberPlate"></el-input>
      <el-date-picker
        v-model="listQuery.selectedMonth"
        type="month"
        format = "yyyy-MM"
        placeholder="选择月">
      </el-date-picker>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
    </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="200">
        <template scope="scope">
          <div>起：{{scope.row.bookStartDate}}</div>
          <div>止：{{scope.row.bookEndDate}}</div>
        </template>
      </el-table-column>
      <el-table-column width="150" align="center" label="用途">
        <template scope="scope">
          <!--/*1-租车、2-分公司使用、3-维修、4、展览、5、旅游、6、保养、7、预约中、8、禁用*/-->
          <span>{{scope.row.bookType==1?"租房车":scope.row.bookType==4?"展览":scope.row.bookType==5?"房车游":scope.row.bookType==6?"保养":scope.row.bookType==7?"预约中":scope.row.bookType==8?"禁用":scope.row.bookType==2?"分公司使用":'未知用途'}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="订单号">
        <template scope="scope">
          <span>{{scope.row.orderNo}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="出车分公司">
        <template scope="scope">
          <span>{{scope.row.mileage}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="还车分公司">
        <template scope="scope">
          <span>{{scope.row.retCompanyName}}</span>
        </template>
      </el-table-column>
      <el-table-column width="150" align="center" label="使用人">
        <template scope="scope">
          <span>{{scope.row.vehicleUsername}}</span>
        </template>
      </el-table-column>
      <el-table-column width="150" align="center" label="使用人联系方式">
        <template scope="scope">
          <span>{{scope.row.vehicleUserPhone}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300">
        <template scope="scope">
          <el-button size="small" type="success" @click="toShowDialog(scope.row)" v-if="scope.row.bookType !=2 && scope.row.bookType !=3">详情</el-button>
          <el-button size="small" type="success" @click="getLogInfo(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, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>

    <!--保养弹框-->
    <vehicleMaintenanceModal v-if="baoyang" :currentItem="currentItem" v-on:baoyangDialogEvent = "baoyangDialogEvent"></vehicleMaintenanceModal>
    <!--展览、预定用车弹框-->
    <vehicleExhibitionModal v-if="zhanlan" :currentItem="currentItem" v-on:zhanlanDialogEvent = "zhanlanDialogEvent"></vehicleExhibitionModal>
    <!--安排用车弹框-->
    <vehiclePlanModal v-if="anpai" :currentItem="currentItem" v-on:anpaiDialogEvent = "anpaiDialogEvent"></vehiclePlanModal>
    <!--租车详情弹框-->
    <!--rentOrder-->
    <rentOrderDetailModal :form="rentOrderInfo" :rentCostDetail="rentCostDetail" v-if="rentOrder" v-on:rentOrderDetailDialogEvent="rentOrderDetailDialogEvent"></rentOrderDetailModal>
    <!--旅游订单详情-->
    <tourOrderDetailModal :tourRow="tourRow" v-if="tourDialogVisible" v-on:tourOrderDetailDialogEvent="tourOrderDetailDialogEvent"></tourOrderDetailModal>
    <!--禁用弹框-->
    <disableModal  v-if="jinyong" :currentItem="currentItem" v-on:jinyongDialogEvent = "jinyongDialogEvent"></disableModal>
  </div>
</template>
<script>
  import vehicleMaintenanceModal from './vehicleMaintenanceModal';//车辆保养弹框
  import vehicleExhibitionModal from './vehicleExhibitionModal';//车辆展览弹框
  import vehiclePlanModal from './vehiclePlanModal';//安排用车
  import rentOrderDetailModal from "../../order/modal/rentOrderDetailModal";//租车订单详情
  import tourOrderDetailModal from "../../order/modal/tourOrderDetailModal";//房车游订单详情
  import disableModal from "./disableModal";//禁用弹框
  import {
    getVehicleOrderDetail
  } from 'api/vehicle/vehicleSchedulManage';
  import {
    getBookRecord
  } from 'api/vehicle/vehicleSchedulManage';
  import rsCode from '../../../utils/rsCode';
  import {
    formatDate
  } from 'utils/dateFormattor';

  import {
    toEast8Date,
    deepCopyDate,
    newEast8Date,
    convertDate2Str,
    timestamp2Date,
    getTimeByDay,
    getCurrentMonth,
    getMonth,
    getymdTimeByDay,
    getCurrentYearMonth
  } from 'utils/dateUtils';
  export default {
    name: 'vehicleRecords',
    components: {
      vehicleExhibitionModal,
      vehicleMaintenanceModal,
      vehiclePlanModal,
      rentOrderDetailModal,
      tourOrderDetailModal,
      disableModal
    },
    data() {
      return {
        currentItem: {},//当前操作表单
        dialogVisible: false,//记录详情
        form: {
          vehicleId: null,
          numberPlate: null,
          operator: null,
          date: null,
          mileage: null,
          amount: null,
          approvers: null,
          branchCompanyId: null,
          branchCompanyName: null,
          upkeepItems: []
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          selectedMonth: "",
          numberPlate: undefined
        },
        tableKey: 0,
        dialogFormVisible: false,
        baoyang: false,//保养弹框,
        zhanlan: false,//展览弹框
        rentOrder: false,//租车订单详情
        anpai: false,//安排用车弹框
        jinyong: false,//禁用弹框
        currentItem: {},//待操作数据
        rentOrderInfo: {},//租车订单详情
        rentCostDetail: {},//租车订单费用明细
        tourDialogVisible: false,//旅游订单弹框
        tourRow: {},//旅游订单详情-当前行
      }
    },
    created() {
    },
    mounted() {
      this.listQuery.selectedMonth = this.$route.query.selectedMonth;
      this.listQuery.numberPlate = this.$route.query.numberPlate;
      this.getList();
    },
    methods: {
      handleFilter() {
        this.getList();
      },
      /**
       * 点击事件
       * */
      toShowDialog( cc, iitem){
        let item = {};
        let ii = {
          vehicleBookRecord: cc
        };
//      <!--bookType 1-租车、2-分公司使用、3-维修、4、展览、5、旅游、6、保养、7、预约中、8、禁用-->
        item.numberPlate = this.listQuery.numberPlate;//车牌号
        item.parkCompanyName = cc.parkCompanyName;//当前停靠公司
        item.subordinateBranchName = cc.subordinateBranchName;//所属公司
        if(cc.bookType == 6){
          //保养
          this.baoyang = true;
          this.currentItem = {item: item, iitem: iitem, ii: ii};
        } else if(cc.bookType == 4){
          //展览
          this.zhanlan = true;
          this.currentItem = {item: item, ii: ii, title: "展览用车"};
        } else if(cc.bookType == 7){
          this.zhanlan = true;
          this.currentItem = {item: item, ii: ii, title: "预定用车"};
        } else if(cc.bookType == 1){
          this.getOneVechi(cc.orderNo);
          this.currentItem = {item: item, iitem: iitem, ii: ii, title: "租车订单"};
        } else if(cc.bookType == 5) {
          //房车游订单详情
          this.tourRow = {no: cc.vehicleBookRecord.orderNo};
          this.tourDialogVisible = true
        } else if(cc.bookType == 8) {
          //禁用
          this.jinyong = true;
          this.currentItem = {item: item, iitem: iitem, ii: ii, title: "禁用"};
        }
      },
      /**
       * 根据订单号获取订单详情
       * */
      getOneVechi: function(orderNo) {
        let params = {
          orderNo: orderNo
        };
        getVehicleOrderDetail(params).then(res => {
          if (res.status == 200) {
            let a = res.data;
            a.orderRentVehicleDetail.startTime = timestamp2Date(a.orderRentVehicleDetail.startTime);
            a.orderRentVehicleDetail.endTime = timestamp2Date(a.orderRentVehicleDetail.endTime);
            let arr = a.picture ? a.picture.split(",") : [];
            a.picture = arr.length > 0 ? arr[0] : "";
            a.payTime = timestamp2Date(a.payTime);
            this.rentOrderInfo = a;

            this.costDetail = JSON.parse(this.rentOrderInfo.orderRentVehicleDetail.costDetail);
            var cost = '';
            this.costDetail.children.map(function(a) {
              cost += a.key + ":" +a.detail+" ";
            });
            this.rentCostDetail = cost;
            if(this.rentOrderInfo.orderRentVehicleDetail.driverType==1) {
              this.serviceCost = this.rentOrderInfo.orderRentVehicleDetail.dayNum * 600
            }
            this.rentOrder = true;
          } else {
            this.$notify({
              title: '失败',
              message: '操作失败！',
              type: 'error',
              duration: 2000
            });
          }
        });
      },
      /**
       * 获取排班记录列表数据
       */
      getList() {
        this.listLoading = true;
        if(this.listQuery.selectedMonth  && (typeof this.listQuery.selectedMonth == "object")){
          this.listQuery.selectedMonth = formatDate(this.listQuery.selectedMonth, "yyyy-MM");//预定开始时间;
        }
        getBookRecord(this.listQuery).then(response => {
          this.list = response.data.data;
          this.total = response.data.totalCount;
          this.listLoading = false;
        });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      /**
       * 关闭保养弹框后逻辑处理
       * */
      baoyangDialogEvent(e){
        this.baoyang = false;
        if(e){//关闭编辑
          //编辑成功-重新加载列表
          this.getList();
        }
      },
      /**
       * 关闭展览弹框后逻辑处理
       * */
      zhanlanDialogEvent(e){
        this.zhanlan = false;
        if(e){//关闭编辑
          //编辑成功-重新加载列表
          this.getList();
        }
      },
      /**
       * 禁用弹框关闭后逻辑处理
       * */
      jinyongDialogEvent(e){
        this.jinyong = false;
        if(e){//关闭编辑
          //编辑成功-重新加载列表
          this.getList();
        }
      },
      /**
       * 安排用车弹框关闭逻辑处理
       * */
      anpaiDialogEvent(e){
        this.anpai = false;
        if(e){//关闭编辑
          //编辑成功-重新加载列表
          this.getList();
        }
      },
      /**
       * 租车订单详情弹框关闭
       * */
      rentOrderDetailDialogEvent(e){
        this.rentOrder = false;
      },
    }
  }
</script>

