<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;">客户名称：{{ detailsData && detailsData.supplier && detailsData.supplier.supplierName }}</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-dealerName" v-model.trim="queryParam.dealerName" allowClear placeholder="请输入业务单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="对账单号">
                <a-input id="accountStateBillDetail-bookNo" v-model.trim="queryParam.bookNo" allowClear placeholder="请输入对账单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="借贷类型">
                <v-select
                  v-model="queryParam.status"
                  ref="status"
                  id="accountStateBillDetail-status"
                  code="FINANCE_BOOK_STATE"
                  placeholder="请选择借贷类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="业务类型">
                <v-select
                  v-model="queryParam.status"
                  ref="status"
                  id="accountStateBillDetail-status"
                  code="FINANCE_BOOK_STATE"
                  placeholder="请选择业务类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="对单状态">
                <v-select
                  v-model="queryParam.status"
                  ref="status"
                  id="accountStatement-status"
                  code="FINANCE_BOOK_STATE"
                  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"
                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" v-if="totalData">
        <div slot="message">
          余额：<strong>{{ totalData&&(totalData.totalCancelAmount || totalData.totalCancelAmount==0) ? toThousands(totalData.totalCancelAmount) : '--' }}</strong>；
          借方：<strong>{{ totalData&&(totalData.totalUnpushedAmount || totalData.totalUnpushedAmount==0) ? toThousands(totalData.totalUnpushedAmount) : '--' }}</strong>；
          贷方：<strong>{{ totalData&&(totalData.totalDispatchAmount || totalData.totalDispatchAmount==0) ? toThousands(totalData.totalDispatchAmount) : '--' }}</strong>；
        </div>
      </a-alert>
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable"
          ref="table"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :pageSize="20"
          :defaultLoadData="false"
          bordered>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { financeBookQueryPage, financeBookDeleteBySn } from '@/api/financeBook.js'
export default {
  name: 'AccountStatementBillDetail',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      spinning: false,
      exportLoading: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        bookNo: '',
        dealerName: '',
        status: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      creatDate: [], //  创建时间
      advanced: true,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return financeBookQueryPage(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
            }
            this.total = data.count || 0
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      total: 0, // 合计
      totalData: null
    }
  },
  // 根据权限显示列表字段
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '年', dataIndex: 'sparePartsNo', width: '3%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '月', dataIndex: 'sparePartsNo', width: '3%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '日', dataIndex: 'sparePartsNo', width: '3%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务单号', dataIndex: 'sparePartsNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对账单号', dataIndex: 'sparePartsNo', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务类型', dataIndex: 'product.code', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '业务状态', dataIndex: 'product.code', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '费用/调拨类型', dataIndex: 'product.unit', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '摘要', dataIndex: 'sparePartsBatchNo', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客诉索赔编码', dataIndex: 'currentStockQty', width: '8%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '借方', dataIndex: 'productCost', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? toThousands(text, 2) : '--') } },
        { title: '贷方', dataIndex: 'productCost', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? toThousands(text, 2) : '--') } },
        { title: '备注', dataIndex: 'remarks', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '对单状态', dataIndex: 'remarks', 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 () {

    },
    //  重置
    resetSearchForm () {
      this.creatDate = []
      this.$refs.rangeDate.resetDate(this.creatDate)
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.dealerName = ''
      this.queryParam.bookNo = ''
      this.queryParam.status = undefined
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 110
    }
  },
  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()
      this.resetSearchForm()
    }
  },
  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>

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