<template>
  <a-card size="small" :bordered="false" class="ledgerRecordList-wrap">
    <!-- 搜索条件 -->
    <div 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="分账时间">
              <rangeDateTime 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" showIcon style="margin-bottom:15px">
      <div slot="message">总计：<strong>{{ total }}</strong>条 ，分账金额：<strong>{{ (totalAmount || totalAmount == 0) ? totalAmount : '--' }}</strong> 元</div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1690 }"
      bordered>
    </s-table>
  </a-card>
</template>

<script>
import moment from 'moment'
import getDate from '@/libs/getDate.js'
import { STable, VSelect } from '@/components'
import rangeDateTime from '@/views/common/rangeDateTime.vue'
import custSatelliteList from '@/views/common/custSatelliteList.vue'
import { satelliteWHSeparateList, satelliteWHSeparateTotal } from '@/api/satelliteWH'
export default {
  components: { STable, VSelect, rangeDateTime, custSatelliteList },
  data () {
    return {
      advanced: false, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      createDate: [
        moment(getDate.getRecentTime().starttime, 'YYYY-MM-DD HH:mm:ss'),
        moment(getDate.getRecentTime().endtime, 'YYYY-MM-DD HH:mm:ss')
      ],
      // 查询参数
      queryParam: {
        customer: {
          customerSn: undefined
        },
        dealerProduct: {
          name: '',
          code: ''
        },
        beginDate: getDate.getRecentTime().starttime,
        endDate: getDate.getRecentTime().endtime
      },
      // 表头
      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
        return satelliteWHSeparateList(Object.assign(parameter, this.queryParam)).then(res => {
          const 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.list.length
          this.getTotal(Object.assign(parameter, this.queryParam))
          return data
        })
      },
      total: 0, //  总条数
      totalAmount: ''
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 合计
    getTotal (params) {
      satelliteWHSeparateTotal(params).then(res => {
        if (res.status == 200) {
          this.totalAmount = res.data || 0
        } else {
          this.totalAmount = ''
        }
      })
    },
    custSatelliteChange (val) {
      this.queryParam.customer.customerSn = val.key
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.createDate)
      this.queryParam.beginDate = getDate.getRecentTime().starttime
      this.queryParam.endDate = getDate.getRecentTime().endtime
      this.queryParam.customer.customerSn = undefined
      this.queryParam.dealerProduct.name = ''
      this.queryParam.dealerProduct.code = ''
      this.$refs.custSatelliteList.resetForm()
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less">
  .ledgerRecordList-wrap{
    .sTable{
      margin-top: 15px;
    }
  }
</style>
