<!--车辆排班记录-->
<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">
          <!--bookType 1-租车、2-分公司使用、3-维修、4、展览、5、旅游、6、保养、7、预约中、8、禁用、9、客户用车、10、其他-->
          <span>{{scope.row.bookType==1?"租房车":scope.row.bookType==3?"维修":scope.row.bookType==8?"禁用":scope.row.bookType==9?"客户用车":scope.row.bookType==10?"其他":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 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="150" fixed="right">
        <template scope="scope">
          <el-button size="small"  class="el-button el-button--text el-button--small" @click="toShowDialog(scope.row)" v-if="scope.row.bookType !=2 && scope.row.bookType !=3">详情</el-button>
          <el-button size="small"  class="el-button el-button--text el-button--small" @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>
    <el-dialog title="重新安排" :visible.sync="reAnpai" class="user-car">
      <el-form :model="form"  ref="form" :rules="rules" label-width="140px">
        <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>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="handelOk('form')" v-if="vehicleInfo_btn_apply">确 定</el-button>
      </div>
    </el-dialog>

    <!--保养弹框-->
    <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 {mapGetters} from 'vuex';
  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 {
    bookPlan
  } from 'api/vehicle/vehicleSchedulManage';
  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 {
        dialogVisible: false,//记录详情
        reAnpai: false,//重新安排
        pickerOptions0: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7;
          }
        },
        form:{
          times: [],//起止日期
          vehicleId: undefined,//车型id
          bookRecordId: undefined,//预定记录id
        },
        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: {},//旅游订单详情-当前行
        rules: {
          times: {
            required: true,
            message: '请选择时间',
          }
        },
        vehicleInfo_btn_apply:false,
      }
    },
    created() {
      this.vehicleInfo_btn_apply = this.elements['vehicleInfo:btn_apply'];
    },
    computed: {
      ...mapGetters([
        'elements',
      ]),
    },
    mounted() {
      this.listQuery.selectedMonth = this.$route.query.selectedMonth;
      this.listQuery.numberPlate = this.$route.query.numberPlate;
      this.getList();
    },
    methods: {
      handleFilter() {
        this.getList();
      },
      /**
       * 重新排期
       * */
      getLogInfo(row){
        this.form.vehicleId = row.vehicleId;
        this.form.bookRecordId = row.id;
        this.form.times = [row.bookStartDate, row.bookEndDate];
        this.reAnpai = true;
      },
      /**
       * 弹框-取消
       * */
      cancel() {
        this.reAnpai = false;
      },
      /**
       * 确定用车
       * */
      handelOk(formName){
        const set = this.$refs;
        console.log(this.form.times);
        set[formName].validate(valid => {
          if (valid) {
            let params = {
              vehicleId:this.form.vehicleId,
              bookStartDate: formatDate(this.form.times[0], "yyyy-MM-dd hh")+":00:00",//预定开始时间
              bookEndDate: formatDate(this.form.times[1], "yyyy-MM-dd hh")+":00:00",//预定结束时间
              vehicleBookRecordId: this.form.bookRecordId,//预订记录id
            };
            bookPlan(params).then(response => {
              if (response.status === 200) {
                this.reAnpai = false;
                this.getList();
              } else {
                this.$notify({
                  title: '失败',
                  message: '操作失败！',
                  type: 'error',
                  duration: 2000
                });
              }
            })
          }
        })
      },
      /**
       * 点击事件
       * */
      toShowDialog( cc, iitem){
        let item = {};
        let ii = {
          vehicleBookRecord: cc,
          bookType:cc.bookType
        };
//      <!--bookType 1-租车、2-分公司使用、3-维修、4、展览、5、旅游、6、保养、7、预约中、8、禁用、9、客户用车、10、其他-->
        item.numberPlate = this.listQuery.numberPlate;//车牌号
        item.parkCompanyName = cc.parkCompanyName;//当前停靠公司
        item.subordinateBranchName = cc.subordinateBranchName;//所属公司
        if(ii.vehicleBookRecord.status == 1){
          //审核中显示预定中
          this.zhanlan = true;
          this.currentItem = {item: item, ii: ii, title: "预定用车"};
        } else{
          if(cc.bookType == 6){
            //保养
            this.baoyang = true;
            this.currentItem = {item: item, iitem: iitem, ii: ii};
          } else if(cc.bookType == 3){
            //维修
            this.zhanlan = true;
            this.currentItem = {item: item, ii: ii,title: "维修",upkeepName:'维修'};
          } else if(cc.bookType == 4){
            //展览
            this.zhanlan = true;
            this.currentItem = {item: item, ii: ii, title: "展览用车",upkeepName:'展览'};
          } else if(cc.bookType == 7){
            this.zhanlan = true;
            this.currentItem = {item: item, ii: ii, title: "预定用车",upkeepName:'预定'};
          } else if(cc.bookType == 1){
            if(!cc.orderNo){
              this.$notify({
                title: '警告',
                message: '订单号缺失',
                type: 'warning',
                duration: 2000
              });
              return false;
            } else{
              this.getOneVechi(cc.orderNo);
            }
            this.currentItem = {item: item, iitem: iitem, ii: ii, title: "租车订单"};
          } else if(cc.bookType == 9){
            this.zhanlan = true;
            this.currentItem = {item: item, ii: ii,title: "客户用车",upkeepName:'客户用车'};
          } 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: "禁用"};
          } else if(cc.bookType == 10){
            this.zhanlan = true;
            this.currentItem = {item: item, ii: ii,title: "其他",upkeepName:'其他'};
          }
        }
      },
      /**
       * 根据订单号获取订单详情
       * */
      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>

