<template>
  <a-modal
    centered
    class="chooseBillModal-modal"
    :footer="null"
    :maskClosable="false"
    title="全部单据"
    v-model="isShow"
    @cancle="isShow=false"
    width="80%">
    <!-- 搜索条件 -->
    <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="单据号">
              <a-input id="chooseBillModal-bizNo" v-model.trim="queryParam.bizNo" allowClear placeholder="请输入单据号"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :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="6" :sm="24">
            <a-form-item label="审核时间">
              <a-range-picker
                style="width:100%"
                id="chooseBillModal-auditTime"
                :disabledDate="disabledDate"
                v-model="auditTime"
                :format="dateFormat"
                :placeholder="['开始时间', '结束时间']" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button style="margin-bottom: 18px;" type="primary" @click="$refs.table.refresh(true)" :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>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :showPagination="false"
      :scroll="{ x: '100%', y: 400 }"
      :defaultLoadData="false"
      bordered>
    </s-table>
    <div class="btn-cont">
      <a-button type="primary" id="chooseBillModal-modal-save" @click="handleSave">确定</a-button>
      <a-button id="chooseBillModal-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
    </div>
  </a-modal>
</template>

<script>
import moment from 'moment'
import { STable, VSelect } from '@/components'
import { settleInfoAllList } from '@/api/settle'
export default {
  name: 'ChooseBillModal',
  components: { STable, VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowChoose: {
      type: Array,
      default: () => {
        return []
      }
    },
    settleClientSn: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      disabled: false,
      auditTime: [], //  审核时间
      dateFormat: 'YYYY-MM-DD',
      queryParam: {
        bizNo: '',
        bizType: undefined
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '单据号', dataIndex: 'bizNo', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '单据类型', dataIndex: 'bizTypeDictValue', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核时间', dataIndex: 'auditTime', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '金额', dataIndex: 'totalAmount', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '余额', dataIndex: 'unsettleAmount', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        // 审核时间
        if (this.auditTime && this.auditTime.length > 0) {
          this.queryParam.auditBeginDate = moment(this.auditTime[0]).format(this.dateFormat) + '00:00:00'
          this.queryParam.auditEndDate = moment(this.auditTime[1]).format(this.dateFormat) + '23:59:59'
        } else {
          this.queryParam.auditBeginDate = undefined
          this.queryParam.auditEndDate = undefined
        }
        return settleInfoAllList(Object.assign(this.queryParam, { settleClientSn: this.settleClientSn })).then(res => {
          const data = res.data
          this.selectedRowKeys = []
          this.selectedRows = []
          for (var i = 0; i < data.length; i++) {
            data[i].no = i + 1
            data[i].settleAmount = data[i].unsettleAmount
            const ind = this.nowChoose.findIndex(item => item.id == data[i].id)
            if (ind != -1) {
              this.selectedRowKeys.push(data[i].id)
              this.selectedRows.push(data[i])
            }
          }
          this.disabled = false
          return data
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  computed: {
    hasSelected () {
      return this.selectedRowKeys.length > 0
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    // 确定
    handleSave () {
      this.$emit('ok', this.selectedRows)
      this.isShow = false
    },
    // 重置
    resetSearchForm () {
      this.queryParam.bizNo = ''
      this.queryParam.bizType = undefined
      this.auditTime = []
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys, selectedRows)
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      const _this = this
      if (!newValue) {
        _this.$emit('close')
        _this.selectedRowKeys = []
        _this.selectedRows = []
        _this.resetSearchForm()
      } else {
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 200)
      }
    }
  }
}
</script>

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