<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-form>
        <el-row>
          <el-col :span="8">
            <el-form-item>
              <el-date-picker
                v-model="listQuery.time"
                type="date"
                :editable="false"
                format="yyyy-MM-dd"
                prop = "time"
                placeholder="请选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item >
              <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="车牌号"
                        v-model="listQuery.numberPlate"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

    </div>
    <div v-show="statisticVisiable">
      <el-row style="text-align: center;margin-top: 10px;margin-bottom: 10px">
        <el-col :span="8">
          <span class="statistic_number">{{statisticData.departureCount}}</span>
          <br><br>
          <span class="statistic_title">出行次数</span>
        </el-col>
        <el-col :span="8">
          <span class="statistic_number">{{statisticData.departureDay}}</span>
          <br><br>
          <span class="statistic_title">出行天数</span>
        </el-col>
        <el-col :span="8">
          <span class="statistic_number">{{statisticData.departureMileage}}</span>
          <br><br>
          <span class="statistic_title">公里</span>
        </el-col>
      </el-row>
    </div>
    <el-button class="filter-item" type="primary" :icon=" showMoreMoreCol ? 'caret-left' : 'caret-right' " v-waves
               @click="showMoreMoreCol=!showMoreMoreCol">
      更多列
    </el-button>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row
              style="width: 100%">
      <el-table-column
        type="index"
        label="编号"
        width="65">
      </el-table-column>
      <!--<el-table-column align="center" label="编号" width="65">-->
        <!--<template scope="scope">-->
          <!--<span>{{scope.row.id}}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column align="center" label="车牌号" width="120">
        <template scope="scope">
          <span>{{scope.row.numberPlate}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="出车时间">
              <template scope="scope">
                <span>{{scope.row.departureTime}}</span>
              </template>
        </el-table-column>
       <el-table-column width="100" align="center" label="使用人">
               <template scope="scope">
                 <span>{{scope.row.user}}</span>
               </template>
       </el-table-column>
       <el-table-column width="200" align="center" label="出车地点">
                              <template scope="scope">
                                <span>{{scope.row.departureName}}</span>
                              </template>
        </el-table-column>
       <el-table-column width="200" align="center" label="还车时间">
               <template scope="scope">
                 <span>{{scope.row.arrivalTime}}</span>
               </template>
        </el-table-column>
      <el-table-column width="100" align="center" label="还车人">
        <template scope="scope">
          <span>{{scope.row.recycleMan}}</span>
        </template>
      </el-table-column>
        <el-table-column width="200" align="center" label="还车地点">
                       <template scope="scope">
                         <span>{{scope.row.arrivalName}}</span>
                       </template>
         </el-table-column>
        <el-table-column width="150" align="center" label="本次出行天数">
                <template scope="scope">
                  <span>{{scope.row.departureDay}}</span>
                </template>
              </el-table-column>
        <el-table-column width="150" align="center" label="本次出行公里">
                <template scope="scope">
                  <span>{{scope.row.mileage}}</span>
                </template>
        </el-table-column>
       <el-table-column width="100" align="center" label="验车人">
               <template scope="scope">
                 <span>{{scope.row.checkMan}}</span>
               </template>
        </el-table-column>
      <el-table-column width="100" align="center" label="用途">
        <template scope="scope">
          <span>{{scope.row.use}}</span>
        </template>
      </el-table-column>

      <el-table-column width="200" align="center" label="使用人电话" v-if="showMoreMoreCol" >
        <template scope="scope">
          <span>{{scope.row.userTel}}</span>
        </template>
      </el-table-column>

      <el-table-column width="200" align="center" label="验车人电话" v-if="showMoreMoreCol" >
        <template scope="scope">
          <span>{{scope.row.checkManTel}}</span>
        </template>
      </el-table-column>


      <el-table-column width="100" align="center" label="开始公里数" v-if="showMoreMoreCol" >
        <template scope="scope">
          <span>{{scope.row.mileageStart}}</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="结束公里数" v-if="showMoreMoreCol" >
        <template scope="scope">
          <span>{{scope.row.mileageEnd}}</span>
        </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="textMap[dialogStatus]" :visible.sync="dialogFormVisible">-->
    <!--      <el-form :model="form" :rules="rules" ref="form" label-width="100px">-->
    <!--        <el-form-item label="姓名" prop="name">-->
    <!--          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button @click="cancel('form')">取 消</el-button>-->
    <!--        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>-->
    <!--        <el-button v-else type="primary" @click="update('form')">确 定</el-button>-->
    <!--      </div>-->
    <!--    </el-dialog>-->
  </div>
</template>
<style>
  .statistic_number {
    font-size: x-large;
    font-weight: bolder;
  }

  .statistic_title {
    font-size: x-large;
  }
</style>
<script>
  import {mapGetters} from 'vuex';
  import {
    page,
    statistic
  } from '../../../api/vehicle/departureLog';
  import {
    toEast8Date,
    deepCopyDate,
    newEast8Date
  } from 'utils/dateUtils';
  export default {
    name: 'vehicleDepartureLog',
    data() {
      return {
        showMoreMoreCol: false,
        form: {
          id: null,
          numberPlate: null,
          vehicleId: null,
          use: null,
          user: null,
          userTel: null,
          checkMan: null,
          checkManTel: null,
          recycleMan: null,
          recycleManTel: null,
          departureTime: null,
          arrivalTime: null,
          mileageStart: null,
          mileageEnd: null,
          state: null
        },
        rules: {
          name: [
            {
              required: true,
              message: '请输入用户',
              trigger: 'blur'
            }
          ]
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          time: "",
          numberPlate: undefined
        },
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
        dialogFormVisible: null,
        statisticVisiable: true,
        statisticData: {
          departureCount: null,
          departureDay: null,
          departureMileage: null
        }
      }
    },
    created() {
      const numberPlate = this.$route.query.numberPlate;
      if (numberPlate) {
        this.listQuery.numberPlate = numberPlate;
      }
      this.handleFilter();
    },
    computed: {
      ...mapGetters([
        'elements'
      ])
    },
    methods: {
      handleFilter() {
        this.getList();
        if (this.listQuery.numberPlate) {
          statistic({numberPlate: this.listQuery.numberPlate}).then(response => {
            this.statisticData = response.data;
          });
          this.statisticVisiable = true;
        }
      },
      dateToString: function(date){
        var year = date.getFullYear();
        var month =(date.getMonth() + 1).toString();
        var day = (date.getDate()).toString();
        if (month.length == 1) {
          month = "0" + month;
        }
        if (day.length == 1) {
          day = "0" + day;
        }
        var dateTime = year + "-" + month + "-" + day;
        return dateTime;
      },
      getList() {
        this.listLoading = true;
        if (!this.listQuery.numberPlate) {
          this.statisticVisiable = false;
        }
        if(this.listQuery.time){
          this.listQuery.time = this.dateToString(this.listQuery.time);
        }
        page(this.listQuery)
          .then(response => {
            this.list = response.data.list;
            this.total = response.data.total;
            this.listLoading = false;
          })
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      }
    }
  }
</script>
