<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="车牌号"
                v-model="listQuery.numberPlate"></el-input>
        <el-input type="number" style="width: 200px;" v-model.number="listQuery.code" class="filter-item" placeholder="请输入车辆编码"></el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" type="primary" v-waves @click="reloadPage">重置</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="120">
        <template slot-scope="scope">
          <span>{{scope.row.numberPlate}}</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="保养人">
        <template slot-scope="scope">
          <span>{{scope.row.operator}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="保养日期">
        <template slot-scope="scope">
          <span>{{scope.row.date}}</span>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="公里数">
        <template slot-scope="scope">
          <span>{{scope.row.mileage}}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="总费用">
        <template slot-scope="scope">
          <span>{{scope.row.amount}}</span>
        </template>
      </el-table-column>
      <el-table-column  align="center" label="审批人">
        <template slot-scope="scope">
          <span>{{scope.row.approvers}}</span>
        </template>
      </el-table-column>
      <el-table-column width="250" align="center" label="保养分公司">
        <template slot-scope="scope">
          <span>{{scope.row.branchCompanyName}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="65" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" class="el-button el-button--text el-button--small" style="color:red;" @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="dialogFormVisible">
      <el-form :model="form" ref="form" label-width="100px">
        <el-form-item label="车牌号" prop="name">
          <el-input v-model="form.numberPlate" disabled></el-input>
        </el-form-item>
        <el-form-item label="保养人" prop="operator">
          <el-input v-model="form.operator"></el-input>
        </el-form-item>
        <el-form-item label="公里数" prop="mileage">
          <el-input v-model="form.mileage"></el-input>
        </el-form-item>
        <el-form-item label="总费用" prop="amount">
          <el-input v-model="form.amount"></el-input>
        </el-form-item>
        <el-form-item label="审批人" prop="approvers">
          <el-input v-model="form.approvers"></el-input>
        </el-form-item>
        <el-form-item label="保养分公司" prop="approvers">
          <el-input v-model="form.branchCompanyName"></el-input>
        </el-form-item>
        <el-form-item label="保养项目" prop="upkeepItems">
          <el-checkbox v-for="item in form.upkeepItems" :label="item.id" :key="item.id" checked>{{item.itemName}}</el-checkbox>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import {
    logPage,
    getLog
  } from '../../../api/vehicle/upkeep';
  import rsCode from '../../../utils/rsCode';

  export default {
    name: 'vehicleUpkeepLog',
    data() {
      return {
        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,
          numberPlate: undefined,
          code: undefined
        },
        tableKey: 0,
        dialogFormVisible: false
      }
    },
    created() {
      let query =  localStorage.getItem("vehicleUpkeepLog");
      if(query !="null" && query){
        this.listQuery = JSON.parse(query);
      }
      this.getList();
    },
    methods: {
      getLogInfo(row) {
        getLog(row.id).then(response => {
          if (response.code === rsCode.RS_CODE_SUC) {
            this.dialogFormVisible = true;
            this.form = response.data;
          }
        });
      },
      /**
       * 重置
       * */
      reloadPage() {
        this.state1='';
        this.listQuery = {
          page: 1,
          limit: 20,
          numberPlate: undefined,
          code: undefined
        };
        this.handleFilter();
      },
      handleFilter() {
        this.listQuery.page = 1;
        if(this.listQuery){
          localStorage.setItem("vehicleUpkeepLog", JSON.stringify(this.listQuery));
        }
        this.getList();
      },
      getList() {
        this.listLoading = true;
        logPage(this.listQuery).then(response => {
          this.list = response.data.list;
          this.total = response.data.total;
          this.listLoading = false;
        });
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        if(this.listQuery){
          localStorage.setItem("vehicleUpkeepLog", JSON.stringify(this.listQuery));
        }
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        if(this.listQuery){
          localStorage.setItem("vehicleUpkeepLog", JSON.stringify(this.listQuery));
        }
        this.getList();
      }
    }
  }
</script>
