<template>
  <a-card size="small" :bordered="false" class="accountManagementList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="36">
            <a-col :md="6" :sm="24">
              <a-form-item label="创建时间">
                <a-range-picker
                  :value="time"
                  allowClear
                  :disabledDate="disabledDate"
                  format="YYYY-MM-DD"
                  style="width: 100%;"
                  @change="dateChange"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="调拨单号">
                <a-input allowClear placeholder="请输入调拨单号" v-model.tirm="queryParam.danhao"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="货架名称">
                <a-select placeholder="请搜索货架名称" allowClear style="width: 100%;">
                  <a-select-option v-for="item in goodsShelveList">
                    {{ }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="产品编码">
                  <a-input allowClear placeholder="请输入产品编码" v-model.tirm="queryParam.code"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="产品名称">
                  <a-input allowClear placeholder="请输入产品名称" v-model.tirm="queryParam.name"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24" style="padding-top: 4px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="outboundOrderList-refresh" >查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="outboundOrderList-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px" v-if="activeKey==0">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-tabs v-model="activeKey" type="card" size="small" @change="callback">
        <a-tab-pane :key="0" tab="全部">
        </a-tab-pane>
        <a-tab-pane :key="1">
          <span slot="tab">
            待退库
            <a-badge count="5" :offset="[0,-20]"/>
          </span>
        </a-tab-pane>
        <a-tab-pane :key="2" tab="已完成">
        </a-tab-pane>
        <a-tab-pane :key="3" tab="已取消">
        </a-tab-pane>
      </a-tabs>
      <!-- <a-tabs v-model="activeKey" type="card" @change="callback" size="small">
        <a-tab-pane :key="item.code" :tab="item.name" v-for="item in tabsList">
          <span slot="tab">
            <a-badge count="5" :offset="[0,-20]"/>
          </span>
        </a-tab-pane>
      </a-tabs> -->

      <a-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+77+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y:tableHeight }"
        :showPagination="false"
        :defaultLoadData="false"
        :expandIconColumnIndex="-1"
        :expandIconAsCell="false"
        :defaultExpandAllRows="true"
        bordered>
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            class="button-primary"
            @click="handleEdit(record)"
            id="shelfMonitoringList-warehousing-btn">退库</a-button>
          <a-button
            size="small"
            type="link"
            class="button-primary"
            @click="handleCancel(record)"
            id="shelfMonitoringList-warehousing-btn">取消调回单</a-button>
        </template>
        <a-table
          class="inner-table"
          slot="expandedRowRender"
          slot-scope="text"
          :columns="innerColumns"
          :data-source="innerData"
          :pagination="false"
        >
        </a-table>
      </a-table>
    </a-spin>
    <!-- 申请提现 -->
    <confirmModal :isOpenModal="showModal" :currentData="currentData" @close="showModal=false" @refresh="$refs.table.refresh(true)"></confirmModal>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import confirmModal from './confirmModal.vue'
import getDate from '@/libs/getDate.js'
export default {
  components: { STable, confirmModal },
  data () {
    return {
      spinning: false,
      disabled: false,
      tableHeight: 0,
      showModal: false,
      advanced: false,
      currentData: null,
      activeKey: 0, // 默认的tab
      tabsList: [{ code: 0, name: '全部' }, { code: 1, name: '待退库' }, { code: 2, name: '已完成' }, { code: 3, name: '已取消' }],
      time: [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ],
      goodsShelveList: [], // 货架数据
      queryParam: {
        beginDate: getDate.getCurrMonthDays().starttime, // 开始时间
        endDate: getDate.getCurrMonthDays().endtime, // 结束时间
        danhao: '',
        name: '',
        code: ''
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '15%', align: 'center' },
        { title: '调回单号', dataIndex: 'name', width: '30%', align: 'center' },
        { title: '货架名称', dataIndex: 'remarks', width: '10%', align: 'center', scopedSlots: { customRender: 'remarks' } },
        { title: '状态', width: '10%', align: 'center' },
        { title: '调回总量', width: '10%', align: 'center' },
        { title: '实退总量', width: '10%', align: 'center' },
        { title: '操作', width: '10%', align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      innerColumns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'createDate', width: '15%', align: 'center' },
        { title: '产品名称', dataIndex: 'createDate', width: '15%', align: 'center' },
        { title: '调回数量', dataIndex: 'createDate', width: '15%', align: 'center' },
        { title: '实退数量', dataIndex: 'createDate', width: '15%', align: 'center' },
        { title: '单位', dataIndex: 'createDate', width: '15%', align: 'center' }
      ],
      innerData: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // this.disabled = true
        // this.spinning = true
        return new Promise(function (resolve, reject) {
          const data = {
            list: [{ id: 1, createDate: '2020-08-09' }]
          }
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = i + 1
          }
          // this.spinning = false
          // this.disabled = false
          resolve(data)
        })
        // return employeeList(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.spinning = false
        //   return data
        // })
      }
    }
  },
  methods: {
    // 时间改变
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 禁止选择今天之后的日期
    disabledDate (current) {
      return current && current > moment().endOf('day')
    },
    // 切换tab
    callback (key) {
      console.log(key)
      this.activeKey = key
    },
    // 重置
    resetSearchForm () {
      this.time = []
    },
    // 申请提现
    handleTX () {
      this.showModal = true
    },
    // 退库
    handleEdit (row) {
      this.showModal = true
      this.currentData = row
    },
    // 取消掉回单
    handleCancel (row) {
      this.$confirm({
        title: '提示',
        content: '确定取消调回单吗？',
        centered: true,
        onOk: () => {
          //
        }
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 265
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      setTimeout(() => {
        _this.setTableH()
      }, 400)
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  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">
  .accountManagementList-wrap{
    .inner-table{
      background-color: #fff;
      .ant-table{
         margin: 10px 150px;
         border-left: 1px solid #eee;
         border-bottom: 1px solid #eee;
      }
    }
  }
</style>
