<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="客户单位" >
          <el-input v-model="searchForm.client_company" placeholder="请输入客户单位" />
        </el-form-item>
        <el-form-item label="账单类型">
          <el-input v-model="searchForm.order_type" placeholder="请输入账单类型" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="searchForm.order_note" 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" v-if="!tag" icon="el-icon-search" @click="fetchData()">搜索</el-button>
          <el-button type="primary" v-else icon="el-icon-search" @click="getName()">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <router-link to="/financialManagement/client/payNew">
            <el-button type="primary" icon="el-icon-edit">新增</el-button>
          </router-link>
        </el-form-item>
        <el-form-item v-show="!client_id">
          <el-button type="primary" v-if="!tag" icon="el-icon-user" @click="getName">按客户名称汇总</el-button>
          <el-button type="primary" v-else icon="el-icon-user" @click="fetchData">取消按客户名称汇总</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="mb-10 f700">
      <span>账单金额合计：{{fee_sum}}</span>
    </div>
    <el-table v-show="!tag" 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="client_name" align="center"></el-table-column>
      <el-table-column label="客户单位" prop="client_company" align="center"></el-table-column>
      <el-table-column label="账单金额" prop="order_fee" align="center">
        <template slot-scope="scope">
          {{scope.row.symbol_mark==true?'+'+scope.row.order_fee:scope.row.order_fee}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="联系名称" prop="client_man" align="center"></el-table-column> -->
      <!-- <el-table-column label="联系电话" prop="client_phone" align="center"></el-table-column> -->
      <el-table-column label="账单类型" prop="order_type" align="center"></el-table-column>
      <el-table-column label="操作员" prop="operate_man" align="center"></el-table-column>
      <el-table-column label="备注" prop="order_note" align="center"></el-table-column>
      <el-table-column label="账单时间" prop="create_time" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="scope">
          <router-link :to="'/financialManagement/client/payNew/'+scope.row.order_id">
            <el-button class="ml-10" type="primary" size="mini">编辑</el-button>
          </router-link>
          <el-button class="ml-10" size="mini" type="danger" @click="handleDelete(scope.row.order_id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- new -->
    <el-table v-show="tag" v-loading="listLoading" :data="firmList" border fit 
      :span-method="objectSpanMethod" 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="client_name" align="center"></el-table-column>
      <el-table-column label="客户单位" prop="client_company" align="center"></el-table-column>
       <el-table-column label="账单金额" prop="order_fee" align="center">
        <template slot-scope="scope">
          {{scope.row.symbol_mark==true?'+'+scope.row.order_fee:scope.row.order_fee}}
        </template>
      </el-table-column>
      <el-table-column label="账单类型" prop="order_type" align="center"></el-table-column>
      <el-table-column label="操作员" prop="operate_man" align="center"></el-table-column>
      <el-table-column label="备注" prop="order_note" align="center"></el-table-column>
      <el-table-column label="账单时间" prop="create_time" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="scope">
          <router-link :to="'/financialManagement/client/payNew/'+scope.row.order_id">
            <el-button class="ml-10" type="primary" size="mini">编辑</el-button>
          </router-link>
          <el-button class="ml-10" size="mini" type="danger" @click="handleDelete(scope.row.order_id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 翻页 -->
    <pagination v-show="total>0"
      v-if="!tag" 
      :total="total" 
      :page.sync="searchForm.page" 
      :page_len.sync="searchForm.page_len"
      :limit.sync="searchForm.limit" 
      @pagination="fetchData" />
    <pagination v-show="total>0" 
      v-else
      :total="total" 
      :page.sync="searchForm.page" 
      :page_len.sync="searchForm.page_len"
      :limit.sync="searchForm.limit" 
      @pagination="getName" />
  </div>
</template>

<script>
import { listClientOrder, listClientOrderNew, saveDriverOrder, detailClientAccount, deleteClientAccount } from '@/api/order/index'

export default {
  data() {
    return {
      firmList: [],  // 列表
      total: 0,  // 总数
      fee_sum: 0, // 账单金额合计
      listLoading: true,
      // 搜索相关数据
      searchForm: {
        page: 1,
        page_len: 10
      },
      // 限制开始时间
      pickerOptionsStart: {},  
      pickerOptionsEnd: {},
      tag: 0,  // 标记是否按客户名称汇总  0：否 1：是
      client_id: this.$route.query.client_id ? this.$route.query.client_id : 0
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    getName() {
      this.tag = 1
      this.firmList = []
      this.$http(listClientOrderNew(this.searchForm), (res) => {
        let firmList = res.data.result
        firmList.map((val, index) => {
          // 没有合并直接整理到数组中
          if(val.group.length==0) {
            let obj = Object.assign({}, val, {})
            delete obj.group
            obj.flag = true  // 记录是否在此项开始
            obj.sum = 1  // 一共多少项相同项(默认一项)
            this.firmList.push(obj)
          } else {
            for(let i=0;val.group.length>i;i++) {
              let obj = Object.assign({}, val, {})
              if(i==0) {
                obj.flag = true // 记录是否在此项开始
                obj.sum = obj.group.length // 一共多少项相同项
              }
              delete obj.group
              for(let item in val.group[i]) {
                obj[item] = val.group[i][item];
              }
              this.firmList.push(obj)
            }
          }
        })
        // this.firmList = res.data.result
        this.total = res.data.data_sum
        this.fee_sum = res.data.fee_sum
        this.listLoading = false
      })
    },
    // 获取列表
    fetchData(obj={}) {
      this.tag = 0
      if(obj.page) {
        this.searchForm.page = obj.page
        this.searchForm.page_len = obj.page_len
      }
      this.listLoading = true
      if(this.$route.query.client_id) {
        this.searchForm.client_id = this.$route.query.client_id
        this.$http(detailClientAccount(this.searchForm), (res) => {
          this.firmList = res.data.result
          this.total = res.data.data_sum
          this.listLoading = false
        })
      } else {
        this.$http(listClientOrder(this.searchForm), (res) => {
          this.firmList = res.data.result
          this.total = res.data.data_sum
          this.fee_sum = res.data.fee_sum
          this.listLoading = false
        })
      }
    },
    // 整合同项
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex==0||columnIndex==1) {
        if (row.flag) {
          return {
            rowspan: row.sum,  // 由row.falg标志开始一共几项合并为一项
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    },
    // 删除
    handleDelete(id) {
      this.$confirm('删除', `确认删除吗`, () => {
        this.$http(deleteClientAccount(id), res => {
          if(res.code == 0) {
            this.$message.success(res.message)
            this.tag?this.getName():this.fetchData()
          }
        })
      })
    },
    // 结束时间限制开始时间
    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()
        }
      })
    },
  }
}
</script>

<style lang='scss' scoped>
</style>
