<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-form :inline="true" :model="searchForm">
        <!-- <el-form-item label="司机名称" >
          <el-input v-model="searchForm.driver_name" placeholder="请输入司机名称" />
        </el-form-item> -->
        <el-form-item label="车辆负责人" >
          <el-input v-model="searchForm.head_name" placeholder="请输入车辆负责人" />
        </el-form-item>
        <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>司机总数：{{total}}</span> -->
      <span>待结算运费合计：{{post_sum}}</span>
      <span class="ml-40">已结算运费合计：{{already_sum}}</span>
    </div>
    <el-table v-loading="listLoading" :data="firmList" border fit  highlight-current-row style="width: 100%;">
      <!-- <el-table-column label="ID" prop="delivery_id" align="center" width="80"></el-table-column> -->
      <!-- <el-table-column label="司机名称" prop="driver_name" 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="head_name" align="center"></el-table-column>
      <!-- <el-table-column label="联系电话" prop="driver_phone" align="center"></el-table-column> -->
      <el-table-column label="待结算运费" prop="wait_post" align="center"></el-table-column>
      <el-table-column label="已结算运费" prop="already_post" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEntry(scope.row.head_id)"> 结算运费 </el-button>
          <router-link :to="'/financialManagement/driver/shiDetails/'+scope.row.head_id">
            <el-button size="mini" type="primary"> 运费明细 </el-button>
          </router-link>
        </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="64px" :rules="formRules" ref="postForm" >
        <el-form-item label="金额：" prop="order_fee">
          <el-input v-model.number="entryInfo.order_fee" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item label="备注：" prop="order_note">
          <el-input type="textarea" :rows="6" v-model="entryInfo.order_note" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <el-button style="margin-left:140px" type="primary" @click="submitForm">保存</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { checkRqInput, checkNum } from '@/utils/validate'
import { listDriverOrder, saveDriverOrder, exportDriverOrder } from '@/api/order/index'
import { baseUrl } from '@/utils/env.js'

export default {
  data() {
    return {
      firmList: [],  // 列表
      total: 0,  // 总数
      post_sum: 0, // 待结算运费合计
      already_sum: 0, // 已结算运费合计
      listLoading: true,
      // 搜索相关数据
      searchForm: {
        page: 1,
        page_len: 10,
        head_name: ''
      },
      dialogEntry: false, // 录入信息弹窗
      entryInfo: {}, // 录入信息
      // 校验规则
      formRules: {
        order_fee: [ checkRqInput, checkNum ],
        order_note: [ checkRqInput ]
      },
      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(listDriverOrder(this.searchForm), (res) => {
        this.firmList = res.data.result
        this.total = res.data.data_sum
        this.post_sum = res.data.post_sum
        this.already_sum = res.data.already_sum
        this.listLoading = false
      })
    },
    // 结算运费
    handleEntry(id) {
      this.entryInfo.head_id = id
      this.dialogEntry = true
    },
    // 保存结算运费
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          this.$http(saveDriverOrder(this.entryInfo), res => {
            this.$message({
              message: '成功',
              type: 'success'
            })
            this.fetchData()
            this.dialogEntry = false
          })
        } else {
          return false
        }
      })
    },
    // 导出
    handleDownload() {
      this.$http(exportDriverOrder(this.searchForm), (res) => {
        window.location.href = this.baseUrl + res.data.url
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.$refs['postForm'].resetFields();
    },
  }
}
</script>

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