<template>
  <a-drawer
    title="账单流水"
    placement="right"
    class="billFlowModal-modal jg-page-wrap"
    :visible="isShow"
    @close="isShow=false"
    width="78%"
    :get-container="false"
    :wrap-style="{ position: 'absolute' }">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="7" :sm="24">
              <a-form-item label="创建时间">
                <rangeDate id="billFlowModal-createDate" ref="rangeDate" v-model="time" @change="createDateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户名称">
                <custList id="billFlowModal-custList" ref="custSatelliteList" @change="custSatelliteChange"></custList>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="关联单号">
                <a-input id="billFlowModal-dealerSalesNo" v-model.trim="queryParam.dealerSalesNo" allowClear placeholder="请输入关联单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="收支类型">
                <v-select
                  v-model="queryParam.inOutType"
                  ref="inOutType"
                  id="billFlowModal-inOutType"
                  code="SHOP_PROMO_IN_OUT_TYPE"
                  placeholder="请选择收支类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="7" :sm="24">
              <a-form-item label="交易类型">
                <v-select
                  v-model="queryParam.tradeType"
                  id="billFlowModal-tradeType"
                  code="BIZ_TRADE_TRADE_TYPE"
                  placeholder="请选择交易类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="交易状态">
                <v-select
                  v-model="queryParam.dealState"
                  id="billFlowModal-dealState"
                  code="BIZ_TRADE_DEAL_STATE"
                  placeholder="请选择交易状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button style="margin-bottom: 18px;" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="billFlowModal-refresh">查询</a-button>
              <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="billFlowModal-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <div class="alert-message">
          收入金额：<strong>{{ detailData&&(detailData.skAmount || detailData.skAmount==0) ? toThousands(detailData.skAmount) : '--' }}</strong>，
          其中到账金额：<strong>{{ detailData&&(detailData.skdzAmount || detailData.skdzAmount==0) ? toThousands(detailData.skdzAmount) : '--' }}</strong>，
          在途金额：<strong>{{ detailData&&(detailData.skztAmount || detailData.skztAmount==0) ? toThousands(detailData.skztAmount) : '--' }}</strong>，
          已退款：<strong>{{ detailData&&(detailData.skytkAmount || detailData.skytkAmount ==0) ? toThousands(detailData.skytkAmount) : '--' }}</strong>；
          支出金额：<strong>{{ detailData&&(detailData.kkAmount || detailData.kkAmount==0) ? toThousands(detailData.kkAmount) : '--' }}</strong>，
          其中退款金额：<strong>{{ detailData&&(detailData.tkAmount || detailData.tkAmount==0) ? toThousands(detailData.tkAmount) : '--' }}</strong>，
          提现金额：<strong>{{ detailData&&(detailData.txAmount || detailData.txAmount==0) ? toThousands(detailData.txAmount) : '--' }}</strong>，
          提现服务费：<strong>{{ detailData&&(detailData.txfwfAmount || detailData.txfwfAmount==0) ? toThousands(detailData.txfwfAmount) : '--' }}</strong>
        </div>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :style="{ height: 510+'px' }"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 450 }"
        :defaultLoadData="false"
        bordered>
        <!-- 金额 -->
        <template slot="realAmount" slot-scope="text, record">
          <div v-if="record.realAmount">
            {{ record.tradeType=='SK'? '+'+(record.realAmount).toFixed(2):'-'+(record.realAmount).toFixed(2) }}
          </div>
          <div v-else>{{ record.realAmount==0?(record.realAmount).toFixed(2):'--' }}</div>
        </template>
        <!-- 交易状态 -->
        <template slot="status" slot-scope="text, record">
          <div v-if="record.dealState&&record.dealStateDictValue">
            <span :style="{color:(record.dealState=='KK'||record.dealState=='YTK')?'#Ed1c24':record.dealState=='DZ'?'#2ac30f':''}">{{ record.dealStateDictValue }}</span>
          </div>
          <div v-else>--</div>
        </template>
      </s-table>
    </a-spin>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import custList from '@/views/common/custList.vue'
import { bizTradeList, bizTradeQueryGroupCount } from '@/api/merchantNew.js'
export default {
  name: 'BillFlowModal',
  components: { STable, VSelect, rangeDate, custList },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      disabled: false,
      time: [], // 创建时间
      queryParam: {
        beginDate: '', // 创建开始时间
        endDate: '', // 创建结束时间
        customerName: undefined, // 客户名称
        customerSn: undefined, // 客户Sn
        dealerSalesNo: '', // 关联单号
        inOutType: undefined, // 收支类型
        tradeType: undefined, // 交易类型
        dealState: undefined // 交易状态
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'customerName', width: '12%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '关联单号', dataIndex: 'dealerSalesNo', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '金额（￥）', dataIndex: 'realAmount', width: '8%', align: 'right', scopedSlots: { customRender: 'realAmount' } },
        { title: '交易类型', dataIndex: 'tradeTypeDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '交易状态', dataIndex: 'dealStateDictValue', width: '8%', align: 'center', scopedSlots: { customRender: 'status' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const _this = this
        const parames = Object.assign(parameter, _this.queryParam)
        parames.dealerSn = _this.$store.state.user.authOrgs[0].orgSn
        return bizTradeList(parames).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            _this.getCount(parames)
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            _this.disabled = false
          }
          _this.spinning = false
          return data
        })
      },
      detailData: null // 统计
    }
  },
  methods: {
    // 选择客户
    custSatelliteChange (obj, row) {
      if (row) {
        this.queryParam.customerSn = row && row.customerSn || undefined
        this.queryParam.customerName = undefined
      } else {
        this.queryParam.customerName = obj || undefined
        this.queryParam.customerSn = undefined
      }
    },
    // 统计
    getCount (val) {
      this.spinning = true
      bizTradeQueryGroupCount(val).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        }
        this.spinning = false
      })
    },
    //  创建时间  change
    createDateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 重置
    resetSearchForm () {
      this.time = []
      this.$refs.rangeDate.resetDate([])
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.customerName = undefined
      this.queryParam.customerSn = undefined
      this.queryParam.dealerSalesNo = ''
      this.queryParam.inOutType = undefined
      this.queryParam.tradeType = undefined
      this.queryParam.dealState = undefined
      this.$refs.custSatelliteList.resetForm()
      this.$refs.table.refresh(true)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      const _this = this
      if (!newValue) {
        _this.$emit('close')
        _this.resetSearchForm()
      } else {
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 200)
      }
    }
  }
}
</script>

<style lang="less">
  .billFlowModal-new-modal{
    .btn-cont {
      margin: 0 0 10px 0;
      text-align: left;
    }
  }
</style>
