<template>
  <div class="accountStateBillDetail-wrap">
    <a-page-header :ghost="false" :backIcon="false" class="accountStateBillDetail-back">
      <!-- 自定义的二级文字标题 -->
      <template slot="subTitle">
        <a id="accountStateBillDetail-btn" href="javascript:;" @click="handleBack">
          <a-icon type="left" />
          返回列表
        </a>
        <span style="margin: 0 10px;color: #666;">客户名称：{{ $route.params.name }}</span>
      </template>
    </a-page-header>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <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="6" :sm="24">
              <a-form-item label="时间">
                <rangeDate id="accountStateBillDetail-rangeDate" ref="rangeDate" :value="creatDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="业务单号">
                <a-input id="accountStateBillDetail-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入业务单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="对账单号">
                <a-input id="accountStateBillDetail-verifyAccountBillNo" v-model.trim="queryParam.verifyAccountBillNo" allowClear placeholder="请输入对账单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="借贷类型">
                <v-select
                  v-model="queryParam.changeType"
                  ref="changeType"
                  id="accountStateBillDetail-changeType"
                  code="VERIFY_ACCOUNT_CHANGE_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.bizType"
                  ref="bizType"
                  id="accountStateBillDetail-bizType"
                  code="VERIFY_ACCOUNT_BIZ_TYPE"
                  placeholder="请选择业务类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="待选状态">
                <a-select v-model="queryParam.waitSelect" allowClear placeholder="请选择待选状态" style="width: 100%" id="accountStateBillDetail-waitSelect">
                  <a-select-option value="1">
                    待选
                  </a-select-option>
                  <a-select-option value="0">
                    已选
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="对单状态">
                <v-select
                  v-model="queryParam.bizStatusColl"
                  ref="billStatus"
                  mode="multiple"
                  id="accountStatement-billStatus"
                  code="VERIFY_ACCOUNT_STATUS"
                  placeholder="请选择对单状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="accountStateBillDetail-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="accountStateBillDetail-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                v-if="$hasPermissions('B_asb_detail_export')"
                id="accountStateBillDetail-export"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
              >导出</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-alert type="info" style="margin-bottom:10px">
        <div slot="message">
          余额：<strong>{{ totalData&&(totalData.bizAmount || totalData.bizAmount==0) ? toThousands(totalData.bizAmount) : '--' }}</strong>；
          借方：<strong>{{ totalData&&(totalData.borrowAmount || totalData.borrowAmount==0) ? toThousands(totalData.borrowAmount) : '--' }}</strong>；
          贷方：<strong>{{ totalData&&(totalData.loanAmount || totalData.loanAmount==0) ? toThousands(totalData.loanAmount*-1) : '--' }}</strong>；
        </div>
      </a-alert>
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+80+'px' }"
          size="small"
          :rowKey="(record) => record.no"
          rowKeyName="no"
          :scroll="{ y: tableHeight }"
          :columns="columns"
          :data="loadData"
          :pageSize="20"
          :defaultLoadData="false"
          bordered>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { hdExportExcel } from '@/libs/exportExcel'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import {
  verifyAccountBalanceQueryDetailPage,
  verifyAccountBalanceQueryDetailCount,
  verifyAccountBalanceDetailExport
} from '@/api/verifyAccountChange'
export default {
  name: 'AccountStatementBillDetail',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      spinning: false,
      exportLoading: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        bizNo: '',
        verifyAccountBillNo: '',
        changeType: undefined,
        bizType: undefined,
        waitSelect: undefined,
        bizStatusColl: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      creatDate: [], //  创建时间
      advanced: true,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = {
          ...parameter,
          dealerSn: this.dealerSn,
          ...this.queryParam
        }
        return verifyAccountBalanceQueryDetailPage(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              if (data.list[i].bizDate) {
                const dates = data.list[i].bizDate.split('-')
                data.list[i].year = dates[0]
                data.list[i].month = dates[1]
                data.list[i].day = dates[2].split(' ')[0]
              }
              // 正为借方， 负数为贷方
              if (data.list[i].bizAmount > 0) {
                data.list[i].debitAmount = data.list[i].bizAmount
              } else {
                data.list[i].creditorAmount = data.list[i].bizAmount * -1
              }
            }
            this.total = data.count || 0
            this.disabled = false
          }
          this.spinning = false
          this.getCount(params)
          return data
        })
      },
      total: 0, // 合计
      totalData: null,
      dealerSn: null
    }
  },
  // 根据权限显示列表字段
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '年', dataIndex: 'year', width: '3%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '月', dataIndex: 'month', width: '3%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '日', dataIndex: 'day', width: '3%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务单号', dataIndex: 'bizNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对账单号', dataIndex: 'verifyAccountBillNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务类型', dataIndex: 'bizTypeDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务状态', dataIndex: 'bizStatusDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用/调拨类型', dataIndex: 'expenseAllocateType', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '摘要', dataIndex: 'digestInfo', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客诉索赔编码', dataIndex: 'productCode', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '借方', dataIndex: 'debitAmount', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } },
        { title: '贷方', dataIndex: 'creditorAmount', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } },
        { title: '备注', dataIndex: 'remarks', width: '10%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '对单状态', dataIndex: 'billStatusDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      return arr
    }
  },
  methods: {
    //  返回列表
    handleBack () {
      this.$router.push({ name: `accountStatementBillList` })
    },
    //  创建时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] ? date[0] : ''
      this.queryParam.endDate = date[1] ? date[1] : ''
    },
    // 导出
    handleExport () {
      const _this = this
      _this.exportLoading = true
      hdExportExcel(verifyAccountBalanceDetailExport, { dealerSn: this.dealerSn, ..._this.queryParam }, '（' + _this.$route.params.name + '）余额-', function () {
        _this.exportLoading = false
      })
    },
    // 统计
    getCount (params) {
      verifyAccountBalanceQueryDetailCount(params).then(res => {
        if (res.status == 200) {
          this.totalData = res.data
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.creatDate = []
      this.$refs.rangeDate.resetDate(this.creatDate)
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''

      this.queryParam.bizNo = ''
      this.queryParam.verifyAccountBillNo = ''
      this.queryParam.changeType = undefined
      this.queryParam.bizType = undefined
      this.queryParam.waitSelect = undefined
      this.queryParam.bizStatusColl = undefined

      this.$refs.table.refresh(true)
    },
    // 默认对单状态
    setDefaultStatus () {
      const type = this.$route.params.type
      if (type == '0') {
        this.queryParam.bizStatusColl = undefined
        this.queryParam.waitSelect = undefined
      }
      if (type == '1') {
        this.queryParam.bizStatusColl = ['WAIT_VERIFY', 'FINISH']
        this.queryParam.waitSelect = undefined
      }
      if (type == '2') {
        this.queryParam.bizStatusColl = ['WAIT_SUBMIT', 'WAIT_CONFIRM']
        this.queryParam.waitSelect = undefined
      }
      if (type == '3') {
        this.queryParam.bizStatusColl = undefined
        this.queryParam.waitSelect = '1'
      }
      this.$refs.table.refresh(true)
    },
    pageInit (flag) {
      const _this = this
      this.dealerSn = this.$route.params.sn
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      _this.setDefaultStatus()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 320
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .accountStateBillDetail-wrap{
    .accountStateBillDetail-back{
      padding-top: 10px;
      margin-bottom: 6px;
    }
  }
</style>
