<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="车辆负责人" >
          <el-input v-model="searchForm.head_name" placeholder="请输入车辆负责人" />
        </el-form-item>
        <!-- <el-form-item label="车辆号码" >
          <el-input v-model="searchForm.driver_account" placeholder="请输入车辆号码" />
        </el-form-item> -->
        <template>
          <div style="display:inline-block;">
            <span class="g66 f700 font-14">结算时间</span>
              <el-date-picker
                v-model="searchForm.start_time"
                :picker-options="pickerOptionsStart"
                type="datetime"
                placeholder="请选择开始时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                @change="changeEnd"/>
              <span>~</span>
              <el-date-picker
                v-model="searchForm.end_time"
                :picker-options="pickerOptionsEnd"
                type="datetime"
                placeholder="请选择结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                @change="changeStart"/>
          </div>
        </template>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
          <el-button class="ml-10" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="mb-10 f700">
      <span >结算金额合计：{{fee_sum}}</span>
    </div>
    <el-table v-loading="listLoading" :data="firmList" border fit  highlight-current-row style="width: 100%;">
      <el-table-column label="ID" prop="order_id" align="center" width="80"></el-table-column>
      <!-- <el-table-column label="司机名称" prop="driver_head" align="center">
        <template slot-scope="scope">
          <div class="flex flex-center">
            <img class="img" v-if="scope.row.driver_head" :src="scope.row.driver_head" alt="">
            <span class="ml-5">{{scope.row.driver_name}}</span>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="结算时间" prop="create_time" align="center"></el-table-column>
      <el-table-column label="车辆负责人" prop="head_name" align="center"></el-table-column>
      <el-table-column label="结算金额" prop="order_fee" align="center"></el-table-column>
      <el-table-column label="备注" prop="order_note" align="center"></el-table-column>
      <el-table-column label="操作员" prop="operate_man" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEntry(scope.row)"> 编辑金额 </el-button>
        </template>
      </el-table-column>  
    </el-table>
    <!-- 翻页 -->
    <pagination v-show="total>0" 
      :total="total" 
      :page.sync="searchForm.page" 
      :page_len.sync="searchForm.page_len"
      :limit.sync="searchForm.limit" 
      @pagination="fetchData" />
    <!-- 编辑金额 -->
    <el-dialog title="编辑金额" :visible.sync="dialogEntry" center width="400px" @close="closeDialog">
      <el-form :model="entryInfo" label-width="70px" :rules="formRules" ref="postForm" >
        <el-form-item label="金额：" prop="order_fee">
          <el-input v-model="entryInfo.order_fee" placeholder="请输入金额"/>
        </el-form-item>
      </el-form>
      <el-button style="margin-left:140px" type="primary" @click="submitForm">保存</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { checkNum, checkRqInput, checkNum0 } from '@/utils/validate'
import { listDriverTiny, updateDriverOrder, exportDriverTiny } from '@/api/order/index'
import { baseUrl } from '@/utils/env.js'

export default {
  data() {
    return {
      firmList: [],  // 列表
      total: 0,  // 总页数
      fee_sum: 0, // 结算金额合计
      listLoading: true,
      // 搜索相关数据
      searchForm: {
        page: 1,
        page_len: 10,
        enterprise_id:1
      },
      dialogEntry: false,
      entryInfo: {
        order_fee: '',
      }, // 
      // 校验规则
      formRules: {
        order_fee: [ checkRqInput, checkNum0 ],
      },
      // 限制开始时间
      pickerOptionsStart: {},  
      pickerOptionsEnd: {},
      baseUrl,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取企业列表
    fetchData(obj={}) {
      if(obj.page) {
        this.searchForm.page = obj.page
        this.searchForm.page_len = obj.page_len
      }
      this.listLoading = true
      this.$http(listDriverTiny(this.searchForm), (res) => {
        this.firmList = res.data.result
        this.total = res.data.data_sum
        this.fee_sum = res.data.fee_sum
        this.listLoading = false
      })
    },
    // 修改金额弹窗
    handleEntry(row) {
      this.dialogEntry = true
      this.entryInfo = {
        order_id: row.order_id,
        order_fee: row.order_fee
      }
    },
    // 修改金额
    submitForm() {
      this.dialogEntry = false
      this.$http(updateDriverOrder(this.entryInfo), (res) => {
        this.fetchData()
      })
    },
    // 导出
    handleDownload() {
      this.$http(exportDriverTiny(this.searchForm), (res) => {
        window.location.href = this.baseUrl + res.data.url
      })
    },
    // 结束时间限制开始时间
    changeStart() {
      let self = this
      if (!this.searchForm.end_time) {
        this.pickerOptionsStart = {
          disabledDate: {}
        }
        return
      }
      this.pickerOptionsStart = Object.assign({}, self.pickerOptionsStart, {
        disabledDate(time) {
          return time.getTime() > new Date(self.searchForm.end_time).getTime()
        }
      })
    },
    // 开始时间控制结束时间
    changeEnd() {
      let self = this
      if (!this.searchForm.start_time) {
        this.pickerOptionsEnd = {
          disabledDate: {}
        }
        return
      }
      this.pickerOptionsEnd = Object.assign({}, self.pickerOptionsEnd, {
        disabledDate(time) {
          return time.getTime() < new Date(self.searchForm.start_time).getTime()
        }
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.$refs['postForm'].resetFields();
    },
  }
}
</script>

<style lang='scss' scoped>
  .img {
    margin-left: 2px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
</style>
