<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.driver_account" placeholder="请输入车辆号码" />
        </el-form-item>
        <el-form-item>
          <el-button class="ml-10" type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
          <el-button class="ml-10" type="primary" icon="el-icon-edit" @click="addNew()">新增</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 class="ml-40">运费总收入合计：{{post_comb}}</span>
      <span class="ml-40">运费总成本合计：{{cost_comb}}</span>
      <span class="ml-40">运费总利润合计：{{profit_comb}}</span>
    </div>
    <el-table v-loading="listLoading" :data="firmList" border fit  highlight-current-row style="width: 100%;" @cell-click="cellClick">
      <el-table-column label="ID" prop="driver_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 ml-5" 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="driver_account" align="center"></el-table-column>
      <el-table-column label="运费总收入" prop="post_sum" align="center"></el-table-column>
      <el-table-column label="运费总成本" prop="cost_sum" align="center"></el-table-column>
      <el-table-column label="运费总利润" prop="profit_sum" align="center"></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" />
    <!-- 子列表 -->
    <div class="mb-10 f700">车辆成本记录(条)：{{chilTotal}}</div>
    <el-table v-loading="msgListLoading" :data="msgList" border style="width: 90%;">
      <el-table-column label="ID" prop="cost_id" align="center" width="80"></el-table-column>
      <el-table-column label="日期" prop="date_time" align="center" width="100"></el-table-column>
      <el-table-column label="项目" prop="cost_name" align="center"></el-table-column>
      <el-table-column label="数量" prop="cost_count" align="center" width="100"></el-table-column>
      <el-table-column label="单位" prop="cost_unit" align="center" width="100"></el-table-column>
      <el-table-column label="金额" prop="order_fee" align="center" width="100"></el-table-column>
      <el-table-column label="备注" prop="order_note" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="260">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleDelete(scope.row.cost_id)"> 删除 </el-button>
          <el-button size="mini" type="primary" @click="addNew(scope.row.cost_id)"> 编辑 </el-button>
          <el-button size="mini" type="primary" @click="checkVoucher(scope.row.cost_id)"> 查看凭证 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 翻页 -->
    <pagination v-show="chilTotal>0" 
      :total="chilTotal" 
      :page.sync="childSearch.page" 
      :page_len.sync="childSearch.page_len"
      :limit.sync="childSearch.limit" 
      @pagination="childClick" />
    <!-- 新增弹窗 -->
    <el-dialog :title="title" :visible.sync="dialog" center width="540px" @close="closeDialog">
      <el-form :model="entryInfo" label-width="100px" :rules="formRules" ref="postForm">
        <el-form-item label="车牌号码：" prop="driver_id">
          <el-select v-model="entryInfo.driver_id" placeholder="请选择车牌号码" clearable :disabled="entryInfo.cost_id?true:false">
            <el-option v-for="(item, index) in driverList" 
              :key="index"
              :label="item.driver_account" :value="item.data_id" >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期：" prop="date_time">
          <el-date-picker
            v-model="entryInfo.date_time"
            type="date"
            placeholder="请选择开始时间"
            value-format="yyyy-MM-dd"/>
        </el-form-item>
        <el-form-item label="项目：" prop="cost_name">
          <el-input placeholder="请输入项目" v-model="entryInfo.cost_name"></el-input>
        </el-form-item>
        <el-form-item label="数量：">
          <el-input placeholder="请输入数量" v-model="entryInfo.cost_count"></el-input>
        </el-form-item>
        <el-form-item label="单位：">
          <el-input placeholder="请输入单位" v-model="entryInfo.cost_unit"></el-input>
        </el-form-item>
        <el-form-item label="金额：" prop="order_fee">
          <el-input placeholder="请输入金额" v-model="entryInfo.order_fee"></el-input>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input placeholder="请输入备注" v-model="entryInfo.order_note"></el-input>
        </el-form-item>
      </el-form>
      <el-button style="margin-left:200px" type="primary" @click="submit">保存</el-button>
    </el-dialog>
    <el-dialog title="凭证详情" :visible.sync="dialogVoucher" center width="720px">
      <div class="flex flex-wrap" style="width:700px">
        <div v-for="(item, index) in voucherDetail" 
          :key="item+index" :data-img="voucherDetail" 
          @click="showImg($event,index)">
          <img class="poi flex ml-10 mt-10" style="width:100px;" :src="baseUrl+item"/>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProfit, saveCost, listCost, detailCost, deleteCost, getData, detailCredential, exportProfit } from '@/api/order/index'
import { checkRqInput, checkNum } from '@/utils/validate'
import imgDialog from '../../../../components/imgPreview/ImgPreview'
import { baseUrl } from '@/utils/env.js'

export default {
  components: {
    imgDialog
  },
  data() {
    return {
      firmList: [],  // 列表
      msgList: [], // 商品信息
      total: 0,  // 总页数
      post_comb: 0, // 运费总收入合计
      cost_comb: 0, //
      profit_comb: 0, // 
      listLoading: true,
      chilTotal: 0, // 子列表总数
      msgListLoading: false,
      enterprise_id:1,
      // 搜索相关数据
      searchForm: {
        page: 1,
        page_len: 10
      },
      // 子列表
      childSearch: {
        page: 1,
        page_len: 10
      },
      // 弹窗
      driverList: [], // 司机列表
      dialog: false, 
      entryInfo: {
        cost_count: '',
        cost_unit: '',
        order_note: ''
      }, // 
      title: '新增司机成本记录',
      // 校验规则
      formRules: {
        driver_id: [ checkRqInput ],
        date_time: [ checkRqInput ],
        cost_name: [ checkRqInput ],
        order_fee: [ checkRqInput, checkNum ],
      },
      baseUrl,
      dialogVoucher: false, // 回执弹窗控制
      voucherDetail: [], // 照片回执详情
    }
  },
  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(listProfit(this.searchForm), (res) => {
        this.firmList = res.data.result
        this.total = res.data.data_sum
        this.post_comb = res.data.post_comb
        this.cost_comb = res.data.cost_comb
        this.profit_comb = res.data.profit_comb
        this.listLoading = false
      })
    },
    // 点击列表展示
    cellClick(row,column,event,cell) {
      this.msgListLoading = true
      this.childSearch.driver_id = row.driver_id
      this.$http(listCost(this.childSearch), (res) => {
        this.msgList = res.data.result
        this.chilTotal = res.data.data_sum
        this.msgListLoading = false
      })
    },
    // 子列表分页
    childClick(obj={}) {
      if(obj.page) {
        this.childSearch.page = obj.page
        this.childSearch.page_len = obj.page_len
      }
      this.msgListLoading = true
      this.$http(listCost(this.childSearch), (res) => {
        this.msgList = res.data.result
        this.chilTotal = res.data.data_sum
        this.msgListLoading = false
      })
    },
    // 获取司机列表
    fetchDriver() {
      this.$http(getData("data-00"), (res) => {
        this.driverList = res.data
      })
    },
    // 删除
    handleDelete(cost_id) {
      this.$confirm('提示', `是否删除该记录?`, () => {
        this.$http(deleteCost(cost_id), res => {
          this.childClick()
          this.fetchData()
        })
      })
    },
    // 新增/编辑
    addNew(cost_id=0) {
      cost_id ? this.title = '编辑司机成本记录' : this.title = '新增司机成本记录'
      this.fetchDriver()
      if(cost_id) {

        this.$http(detailCost(cost_id,enterprise_id), res => {
          this.entryInfo = res.data
          this.dialog = true
        })
      } else {
        this.entryInfo = {
          cost_count: '',
          cost_unit: '',
          order_note: ''
        }
      }
      this.dialog = true
    },
    // 提交
    submit() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          this.$http(saveCost(this.entryInfo), res => {
            this.$message({
              message: '成功',
              type: 'success'
            })
            this.fetchData()
            if(this.childSearch.driver_id) {   // 有司机ID刷新
              this.childClick()
            }
            this.dialog = false
          })
        } else {
          return false
        }
      })
    },
    // 查看凭证
    checkVoucher(cost_id) {
      this.$http(detailCredential(cost_id), res => {
        this.voucherDetail = res.data.cost_credential
        if(res.data.cost_credential.length==0) {
          this.$message({
            message: '没有凭证~~',
            type: 'warning'
          })
          return ''
        }
        this.dialogVoucher = true
      })
    },
    // 展示图片列表
    showImg(e,msg){
      let curIdx = ''
      this.voucherDetail.forEach( (item,index) => {
        if (index == msg) {
          curIdx = index
        }
      })
      this.$imgPreview(e,curIdx)
    },
    // 关闭弹窗
    closeDialog() {
      this.$refs['postForm'].resetFields();
    },
    // 导出
    handleDownload() {
      this.$http(exportProfit(this.searchForm), (res) => {
        window.location.href = this.baseUrl + res.data.url
      })
    },
  }
}
</script>

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