<template>
  <a-card size="small" :bordered="false" class="ledgerRecordList-wrap">
    <!-- 搜索条件 -->
    <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 ref="rangeDate" :value="createDate" @change="dateChange" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="客户名称">
              <custSatelliteList ref="custSatelliteList" @change="custSatelliteChange"></custSatelliteList>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="ledgerRecordList-name" v-model.trim="queryParam.dealerProduct.name" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码">
                <a-input id="ledgerRecordList-code" v-model.trim="queryParam.dealerProduct.code" allowClear placeholder="请输入产品编码"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="ledgerRecordList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="ledgerRecordList-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- alert -->
    <a-alert type="info" style="margin-bottom:10px">
      <div slot="message">
        总计：<strong>{{ total }}</strong>条 ，
        分账金额：<strong>{{ (totalAmount || totalAmount == 0) ? totalAmount+'元' : '--' }}</strong>
      </div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable fixPagination"
      ref="table"
      :style="{ height: tableHeight+84.5+'px' }"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1690 }"
      :defaultLoadData="false"
      bordered>
    </s-table>
  </a-card>
</template>

<script>
import getDate from '@/libs/getDate.js'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import custSatelliteList from '@/views/common/custSatelliteList.vue'
import { satelliteWHSeparateList, satelliteWHSeparateTotal } from '@/api/satelliteWH'
export default {
  components: { STable, VSelect, rangeDate, custSatelliteList },
  data () {
    return {
      advanced: true, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      createDate: [
        getDate.getRecentday().starttime,
        getDate.getRecentday().endtime
      ],
      // 查询参数
      queryParam: {
        customer: {
          customerSn: undefined
        },
        dealerProduct: {
          name: '',
          code: ''
        },
        beginDate: getDate.getRecentday().starttime + ' 00:00:00',
        endDate: getDate.getRecentday().endtime + ' 23:59:59'
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '分账时间', dataIndex: 'separateTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'customer.customerName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '关联工单号', dataIndex: 'separateBizNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'dealerProduct.name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'dealerProduct.code', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '数量', dataIndex: 'productQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '分账金额(￥)', dataIndex: 'separateAmount', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '分账状态', dataIndex: 'stateDictValue', width: 150, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '备注', dataIndex: 'remark', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return satelliteWHSeparateList(Object.assign(parameter, this.queryParam)).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.disabled = false
            this.total = data.count || 0
            this.getTotal(Object.assign(parameter, this.queryParam))
          }
          this.spinning = false
          return data
        })
      },
      total: 0, //  总条数
      totalAmount: ''
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] ? date[0] + ' 00:00:00' : ''
      this.queryParam.endDate = date[1] ? date[1] + ' 23:59:59' : ''
    },
    // 合计
    getTotal (params) {
      satelliteWHSeparateTotal(params).then(res => {
        if (res.status == 200) {
          this.totalAmount = res.data
        } else {
          this.totalAmount = ''
        }
      })
    },
    custSatelliteChange (val) {
      this.queryParam.customer.customerSn = val.key
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.createDate)
      this.queryParam.beginDate = getDate.getRecentday().starttime + ' 00:00:00'
      this.queryParam.endDate = getDate.getRecentday().endtime + ' 23:59:59'
      this.queryParam.customer.customerSn = undefined
      this.queryParam.dealerProduct.name = ''
      this.queryParam.dealerProduct.code = ''
      this.$refs.custSatelliteList.resetForm()
      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 - 230
    }
  },
  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()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
