<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button size="mini" type="primary" @click="combinedQuery = !combinedQuery">组合查询</el-button>
<!--      <el-button :size="sizeType" type="warning" @click="exportBtn">导出</el-button>-->
      <el-card class="box-card" v-if="combinedQuery">
      <el-input :size="sizeType" v-model="search.clientName" placeholder="客户名称" style="width: 200px;" class="filter-item"/>
        <el-input :size="sizeType" v-model="search.orderCode" placeholder="订单编号" style="width: 120px;" class="filter-item"/>
      <el-input :size="sizeType" v-model="search.chargeCode" placeholder="收款编号" style="width: 120px;" class="filter-item"/>
<!--        <el-input :size="sizeType" v-model="search.accountName" placeholder="会计科目" style="width: 150px;" class="filter-item"/>-->
<!--        <el-input :size="sizeType" v-model="search.receiveCode" placeholder="收据号" style="width: 120px;" class="filter-item"/>-->
<!--        <el-input :size="sizeType" v-model="search.voucherCode" placeholder="凭证号" style="width: 150px;" class="filter-item"/>-->
<!--        <el-input :size="sizeType" v-model="search.payMan" placeholder="交款人" style="width: 100px;" class="filter-item"/>-->
<!--        <el-input :size="sizeType" v-model="search.chargeMan" placeholder="收款人" style="width: 100px;" class="filter-item"/>-->
<!--        <el-input :size="sizeType" v-model="search.platformOrderID" placeholder="平台单号" style="width: 120px;" class="filter-item"/>-->
      <el-select :size="sizeType" v-model="search.borrowFlags">
        <el-option
          label="借"
          value="1"></el-option>
        <el-option
          label="贷"
          value="-1"></el-option>
        <el-option
          label="全部"
          value="0"></el-option>
      </el-select>
        <el-select :size="sizeType" multiple v-model="billKinds">
          <el-option
            label="订单生成应收"
            value="0"></el-option>
          <el-option
            label="订单收款"
            value="1"></el-option>
          <el-option
            label="订单作废"
            value="2"></el-option>
          <el-option
            label="订单修改金额"
            value="3"></el-option>
          <el-option
            label="红冲收款记录"
            value="4"></el-option>
      </el-select>
      <el-date-picker
        style="width: 225px"
        :size="sizeType"
        v-model="searchTime"
        type="daterange"
        range-separator="-"
        start-placeholder="入账开始日期"
        end-placeholder="入账结束日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        :default-time="['00:00:00', '23:59:59']">
      </el-date-picker>
        <el-date-picker
          style="width: 225px"
          :size="sizeType"
          v-model="searchVoucherTime"
          type="daterange"
          range-separator="-"
          start-placeholder="制单开始日期"
          end-placeholder="制单结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :default-time="['00:00:00', '23:59:59']">
        </el-date-picker>
      <el-button :size="sizeType" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
        <el-button :size="sizeType"  type="danger" @click="resetForm">
          重置
        </el-button>
      </el-card>

      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getList"/>
        </el-tooltip>
      </div>
    </div>
    <el-table
      :size="sizeType"
      :data="tableData"
      v-loading="listLoading"
      row-key="id"
      border
      style="margin-top: 10px"
      ref="orderTable"
      show-summary
      max-height="600"
      :summary-method="getSummaries"
      default-expand-all>
      <el-table-column
        label="序号"
        fixed
        align="center"
        width="50"
        type="index">
        <template slot-scope="scope">
          <span>{{(listData.page - 1) *listData.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="billMoney"
        label="单据金额">
        <template slot-scope="{row}">
          <el-tag v-if="row.borrowFlag == 1" size="mini" type="danger">借</el-tag>
          <el-tag v-else-if="row.borrowFlag == -1" size="mini" type="warning">贷</el-tag>
          {{row.billMoney}}
        </template>
      </el-table-column>
      <el-table-column
        prop="borrowFlag"
        width="80"
        label="借贷金额">
        <template slot-scope="{row}">
<!--          <el-tag v-if="row.borrowFlag == 1" size="mini" type="danger">借</el-tag>-->
<!--          <el-tag v-else-if="row.borrowFlag == -1" size="mini" type="warning">贷</el-tag>-->
          {{parseFloat(row.borrowFlag)*parseFloat(row.billMoney)}}
        </template>
      </el-table-column>
      <el-table-column
        prop="createMan"
        width="70"
        label="操作人"></el-table-column>
      <el-table-column
        width="120"
        prop="createTime"
        label="入账日期">
        <template slot-scope="{row}">
          <span v-if="row.createTime">{{ row.createTime | getTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="billMemo"
        show-overflow-tooltip
        label="备注"></el-table-column>
      <el-table-column
        prop="billKind"
        width="100"
        label="单据类型">
        <template slot-scope="{row}">
          <el-tag v-if="row.billKind == 0" size="mini" type="primary">订单生成应收</el-tag>
          <el-tag v-else-if="row.billKind == 1" size="mini" type="success">订单收款</el-tag>
          <el-tag v-else-if="row.billKind == 2" size="mini" type="danger">订单作废</el-tag>
          <el-tag v-else-if="row.billKind == 3" size="mini" type="warning">订单修改金额</el-tag>
          <el-tag v-else-if="row.billKind == 4" size="mini" type="info">红冲</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderCode"
        width="110"
        label="订单编号">
        <template slot-scope="{row}">
          <a href="javascript:;" style="color: #20a0ff" @click="getOrderStep(row)">{{ row.orderCode }}</a>
        </template>
      </el-table-column>
      <el-table-column
        prop="clientName"
        show-overflow-tooltip
        label="客户名称">
      </el-table-column>
      <el-table-column
        prop="orderName"
        show-overflow-tooltip
        label="订单名称">
        <template slot-scope="{row}">
          <span style="color: #20a0ff;cursor: pointer" @click="openDetail(row.idOrder)" class="ellipsis"> {{ row.orderName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderCreateMan"
        width="70"
        label="制单人"></el-table-column>
      <el-table-column
        width="120"
        prop="orderCreateTime"
        label="制单日期">
        <template slot-scope="{row}">
          <span v-if="row.orderCreateTime">{{ row.orderCreateTime | getTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderMoney"
        label="订单金额"></el-table-column>
      <el-table-column
        prop="orderPayKind"
        show-overflow-tooltip
        label="付款方式"></el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getList"/>
    <el-dialog title="订单详情" :visible.sync="detailVisible" class="orderDetailDialog" width="1200px" top="10px">
      <order-detail ref="orderDetail"></order-detail>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog" :noDoubleClick="true"></cancledialog>
    <order-step ref="orderStep"></order-step>
  </div>
</template>

<script>
import OrderDetail from '@/components/orderDetail/orderDetail'
import { getOrderTradeBillList } from '@/api/orderCharge/orderTradeBill'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import {changeMoneyToChinese, doLocalPrint} from '@/utils/index'
import OrderStep from "@/components/orderStep/orderStep";
import {formatMoney} from '@/filters/index'
const  searchForm = {
  clientName: '',
  financeCode: '',
  chargeCode: '',
  orderCode: '',
  accountName: '',
  voucherCode: '',
  chargeMan: '',
  platformOrderID: '',
  dataKind: '',
  beginTime: '',
  endTime: '',
  orderBeginTime: '',
  orderEndTime: '',
  receiveCode: '',
  payMan: '',
  borrowFlags: '',
  billKinds: '',
}
export default {
  name: 'orderTradeBill',
  components: {
    OrderStep,
    OrderDetail,Cancledialog
  },
  data() {
    return {
      combinedQuery: false,
      sizeType: 'mini',
      billKinds: [],
      detailVisible: false,
      search: Object.assign({}, searchForm),
      choosedList: [],
      tableData: [],
      collection: {},

      listLoading: false,
      listData: {
        total: 0,
        page: 1,
        pageSize: 20
      },
      searchTime: [],
      searchVoucherTime: [],

      stateOptions: [
        {
          label: '订单定金',
          value: 0
        },
        {
          label: '订单收款',
          value: 1
        },
      ],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 显示字符串中间加星号
     */
    plusXing(str, frontLen, endLen) {
      // var len = str.length - frontLen - endLen
      var xing = ''
      for (var i = 0; i < 6; i++) {
        xing += '*'
      }
      if(str.length>10){
      return str.substr(0, frontLen) + xing + str.substr(str.length - endLen)
      }else{
        return str
      }
    },

    resetForm() {
      this.search = Object.assign({}, searchForm)
      this.searchTime = [];
      this.searchVoucherTime = [];
    },
    getList() {
      var _that = this
      var params = _that.search
      _that.search.billKinds = _that.billKinds.join(',')
      if (_that.searchTime == null || _that.searchTime < 0) {
        _that.search.beginTime = ''
        _that.search.endTime = ''
      } else {
        _that.search.beginTime = _that.searchTime[0]
        _that.search.endTime = _that.searchTime[1]
      }
      if (_that.searchVoucherTime == null || _that.searchVoucherTime < 0) {
        _that.search.orderBeginTime = ''
        _that.search.orderEndTime = ''
      } else {
        _that.search.orderBeginTime = _that.searchVoucherTime[0]
        _that.search.orderEndTime = _that.searchVoucherTime[1]
      }
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      if (_that.listData.pageSize == 10) {
        _that.sizeType = 'medium'
      } else {
        _that.sizeType = 'mini'
      }
      // console.log(data);
      getOrderTradeBillList(data).then(response => {
        // console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.collection = response.collection
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    getSummaries(param) {
      var _that = this
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return
        }
        if (column.label == '订单金额') {
          sums[index] = formatMoney(_that.collection.orderMoney_Sum)
        } else if (column.label == '借贷金额') {
          sums[index] = formatMoney(_that.collection.readBillMoney_sum)
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    // 获取脚印
    getOrderStep(row) {
      const _that = this
      _that.$refs.orderStep.idOrder = row.idOrder
      _that.$refs.orderStep.getOrderStep()
    },

    // 刷新表格
    tableRefresh() {
      // 刷新变革
      this.getList()
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getList()
    },
    // 查看订单详情
    openDetail(idOrder) {
      var _that = this
      _that.detailVisible = true
      _that.$nextTick(() => {
        _that.$refs.orderDetail.idOrder = idOrder
        _that.$refs.orderDetail.getInfo()
      })
    },


    // 导出
    // exportBtn() {
    //   const _that = this
    //   var data = {
    //     code: 'OrderChargeData_Exporter',
    //     exportName: '收款记录导出',
    //     params: _that.search
    //   }
    //   exportData(data).then(res =>{
    //     console.log(res);
    //     if(res.httpCode == 200){
    //       _that.$message.success('导出成功，请前至系统管理-我的任务中进行查看和下载')
    //     }
    //   })
    // },
  }
}
</script>

<style lang="scss" scoped>
.filter-item{
  padding: 5px 0;
}
</style>
