<template>
  <a-drawer
    title="全部单据"
    placement="right"
    class="chooseBillModal-new-modal jg-page-wrap"
    :visible="isShow"
    @close="isShow=false"
    width="70%"
    :get-container="false"
    :wrap-style="{ position: 'absolute' }">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="7" :sm="24">
              <a-form-item label="单据号">
                <a-input id="chooseBillModal-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入单据号"/>
              </a-form-item>
            </a-col>
            <a-col :md="7" :sm="24">
              <a-form-item label="单据类型">
                <v-select
                  v-model="queryParam.bizType"
                  ref="bizType"
                  id="chooseBillModal-bizType"
                  code="SETTLE_BIZ_TYPE"
                  placeholder="请选择单据类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="10" :sm="24">
              <a-form-item label="关联单据创建时间">
                <rangeDate id="chooseBillModal-createDate" ref="createDate" @change="createDateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="7" :sm="24">
              <a-form-item label="审核时间">
                <rangeDate id="chooseBillModal-auditDate" ref="rangeDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-button style="margin-bottom: 18px;" type="primary" @click="searchForm" :disabled="disabled" id="chooseBillModal-refresh">查询</a-button>
              <a-button style="margin: 0 0 18px 8px" @click="resetSearchForm" :disabled="disabled" id="chooseBillModal-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="btn-cont">
        <a-button type="primary" id="chooseBillModal-modal-save" @click="handleSave">批量添加</a-button>
        <a-button
          v-if="nowChoose.length"
          type="primary"
          ghost
          style="margin-left: 15px;"
          id="chooseBillModal-modal-reSel"
          @click="handleReSel">重新选择</a-button>
        <span v-if="totalNums" style="margin-left: 15px;">已选{{ totalNums }}项</span>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :row-selection="{columnWidth: 40, getCheckboxProps: record => ({ props: { disabled: record.checked } })}"
        @rowSelection="rowSelectionFun"
        :columns="columns"
        :data="loadData"
        :showPagination="false"
        :scroll="{ y: 400 }"
        :defaultLoadData="false"
        bordered>
      </s-table>

    </a-spin>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { settleInfoAllList } from '@/api/settle'
export default {
  name: 'ChooseBillModal',
  components: { STable, VSelect, rangeDate },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowChoose: { // 已选数据
      type: Array,
      default: () => {
        return []
      }
    },
    settleClientSn: { // 结算账户sn
      type: String,
      default: ''
    },
    settleClientType: { // 结算账户类型
      type: String,
      default: ''
    }
  },
  computed: {
    // 已选总记录数
    totalNums () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRows && this.rowSelectionInfo.selectedRows.length
    }
  },
  data () {
    const _this = this
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      disabled: false,
      queryParam: {
        auditBeginDate: '', // 审核开始时间
        auditEndDate: '', // 审核结束时间
        bizNo: '', // 单据号
        bizType: undefined, // 单据类型
        bizCreateBeginDate: undefined, // 创建开始时间
        bizCreateEndDate: undefined // 创建结束时间
      },
      allData: [], // 全部单据数据
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '6%', align: 'center' },
        { title: '单据号', dataIndex: 'bizNo', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单据类型', dataIndex: 'bizTypeDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '关联单据创建时间', dataIndex: 'bizCreateDate', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'auditTime', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '总款数', dataIndex: 'bizTotalCategory', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总数量', dataIndex: 'bizTotalQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '金额', dataIndex: 'totalAmount', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '余额', dataIndex: 'unsettleAmount', width: '10%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '状态', dataIndex: 'checked', width: '6%', align: 'center', customRender: function (text) { return text ? '已选' : '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const _this = this
        return settleInfoAllList(Object.assign(this.queryParam, { settleClientType: this.settleClientType, settleClientSn: this.settleClientSn, notEqZero: 1 })).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.length; i++) {
              data[i].no = i + 1
              data[i].settleAmount = data[i].unsettleAmount
              const has = _this.nowChoose.find(item => item.id == data[i].id)
              data[i].checked = !!has
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      rowSelectionInfo: null // 已选数据
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 重新选择
    handleReSel () {
      this.$emit('ok', [])
      this.resetSearchForm()
    },
    // 关联单据创建时间
    createDateChange (date) {
      this.queryParam.bizCreateBeginDate = date[0]
      this.queryParam.bizCreateEndDate = date[1]
    },
    //  审核时间  change
    dateChange (date) {
      this.queryParam.auditBeginDate = date[0]
      this.queryParam.auditEndDate = date[1]
    },
    // 确定
    handleSave () {
      if (this.totalNums) {
        this.$emit('ok', this.rowSelectionInfo && this.rowSelectionInfo.selectedRows)
        this.resetSearchForm()
      } else {
        this.$message.info('请选择单据')
      }
    },
    searchForm () {
      this.clearTable()
      this.$refs.table.refresh(true)
    },
    clearTable () {
      this.$refs.table.clearSelected() // 清空表格选中项
      this.selectedRowKeys = []
      this.selectedRows = []
    },
    // 重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.$refs.createDate.resetDate()
      this.queryParam.auditBeginDate = ''
      this.queryParam.auditEndDate = ''
      this.queryParam.bizCreateBeginDate = ''
      this.queryParam.bizCreateEndDate = ''
      this.queryParam.bizNo = ''
      this.queryParam.bizType = undefined
      this.$refs.table.refresh(true)
      this.clearTable()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      const _this = this
      if (!newValue) {
        _this.$emit('close')
        // 重置
        _this.clearTable()
        _this.resetSearchForm()
      } else {
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 200)
      }
    }
  }
}
</script>

<style lang="less">
  .chooseBillModal-new-modal{
    .btn-cont {
      margin: 0 0 10px 0;
      text-align: left;
    }
  }
</style>
