<template>
  <a-card size="small" :bordered="false" class="onlinePayInvoiceList-wrap jg-page-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="5" :sm="24">
              <a-form-item label="货架名称">
                <shelfList id="onlinePayInvoice-shelfsn" style="max-width:300px;" v-model="queryParam.shelfSn"></shelfList>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="关联客户">
                <custList id="onlinePayInvoice-customerName" ref="custList" @change="custChange" v-model="queryParam.customerName"></custList>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="onlinePayInvoice-refresh" >查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="onlinePayInvoice-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <div class="alert-message">
          账单总金额：<strong>{{ detailData&&(detailData.totalAmount || detailData.totalAmount==0) ? toThousands(detailData.totalAmount) : '--' }}</strong>；
          其中收款金额：<strong>{{ detailData&&(detailData.skAmount || detailData.skAmount==0) ? toThousands(detailData.skAmount) : '--' }}</strong>；
          退款中金额：<strong>{{ detailData&&(detailData.tkzAmount || detailData.tkzAmount==0) ? toThousands(detailData.tkzAmount) : '--' }}</strong>；
          已退款金额：<strong>{{ detailData&&(detailData.tkAmount || detailData.tkAmount==0) ? toThousands(detailData.tkAmount) : '--' }}</strong>；
        </div>
      </div>
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+77+'px' }"
        size="small"
        :rowKey="(record) => record.no"
        rowKeyName="no"
        :columns="columns"
        :data="loadData"
        :scroll="{ y:tableHeight }"
        :defaultLoadData="false"
        bordered>
        <template slot="action" slot-scope="text,record">
          <a-button
            size="small"
            type="link"
            class="button-primary"
            v-if="$hasPermissions('M_onlinePayInvoiceDetail')"
            @click="handleUndetail(record)"
            :id="'onlinePayInvoice-detail-btn-'+record.id">明细</a-button>
          <div v-else>--</div>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import shelfList from '@/views/common/shelfList'
import custList from '@/views/common/custList.vue'
import { queryShelfSettleRulePage, queryShelfSettleRuleCount } from '@/api/merchantNew'
export default {
  name: 'OnlinePayInvoiceList',
  components: { STable, shelfList, custList },
  mixins: [commonMixin],
  data () {
    const _this = this
    return {
      advanced: true, // 高级搜索 展开/关闭
      spinning: false,
      disabled: false,
      tableHeight: 0, // 表格高度
      time: [], // 时间
      queryParam: {
        shelfSn: undefined, // 货架sn
        customerSn: undefined,
        customerName: undefined, // 客户名称
        dealerSn: undefined// 登录用户的sn
      },
      detailData: null,
      // 列
      columns: [
        { title: '序号', dataIndex: 'no', width: '10%', align: 'center' },
        { title: '货架名称', dataIndex: 'shelfName', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '关联客户', dataIndex: 'customerName', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '账单总金额', dataIndex: 'totalAmount', width: '15%', align: 'right', customRender: function (text) { return _this.toThousands(text, 2) || '--' } },
        { title: '收款金额', dataIndex: 'skAmount', width: '15%', align: 'right', customRender: function (text) { return _this.toThousands(text, 2) || '--' } },
        { title: '退款中金额', dataIndex: 'tkzAmount', width: '15%', align: 'right', customRender: function (text) { return _this.toThousands(text, 2) || '--' } },
        { title: '已退款金额', dataIndex: 'tkAmount', width: '15%', align: 'right', customRender: function (text) { return _this.toThousands(text, 2) || '--' } },
        { title: '操作', width: '15%', align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const _this = this
        _this.disabled = true
        _this.spinning = true
        _this.queryParam.dealerSn = _this.$store.state.user.authOrgs[0].orgSn
        const paramsPage = Object.assign(parameter, this.queryParam) //  有分页
        return queryShelfSettleRulePage(paramsPage).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getCount(paramsPage)
            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
        })
      }
    }
  },
  methods: {
    // 选择客户
    custChange (obj, row) {
      console.log(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
      }
    },
    // 重置
    resetSearchForm () {
      this.queryParam = {
        shelfSn: undefined, // 货架sn
        customerSn: undefined,
        customerName: undefined // 客户名称
      }
      this.$refs.custList.resetForm()
      this.$refs.table.refresh(true)
    },
    // 合计
    getCount (val) {
      queryShelfSettleRuleCount(val).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        }
      })
    },
    // 明细
    handleUndetail (row) {
      this.$router.push({ name: 'onlinePayInvoiceDetail', params: { sn: row.customerSn, shelfSn: row.shelfSn, shelfName: row.shelfName } })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 225
    },
    // 初始化页面
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      setTimeout(() => {
        _this.setTableH()
      }, 400)
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
