<template>
  <div>
  <a-card size="small" :bordered="false" class="searchBoxNormal">
    <!-- 搜索条件 -->
    <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="盘点单号">
              <a-input id="inventoryConfirmationList-checkWarehouseNo" v-model.trim="queryParam.checkWarehouseNo" allowClear placeholder="请输入盘点单号"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="仓库">
              <chooseWarehouse ref="warehouse" isPermission v-model="queryParam.warehouseSn"></chooseWarehouse>
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="inventoryConfirmationList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="inventoryConfirmationList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
  </a-card>
  
  <a-card size="small" :bordered="false" class="inventoryConfirmationList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 标签页 -->
      <a-tabs :default-active-key="queryParam.state" @change="handleTabChange" class="inventoryConfirmationList-tabs">
        <a-tab-pane v-for="item in tabPaneData" :key="item.type">
          <span slot="tab">{{ item.name }}</span>
        </a-tab-pane>
      </a-tabs>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+70+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :pageSize="30"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 单号 -->
        <template slot="checkWarehouseNo" slot-scope="text, record">
          <span v-if="$hasPermissions('B_inventoryConfirmationDetail')" class="link-bule" @click="handleDetail(record)">{{ record.checkWarehouseNo }}</span>
          <span v-else>{{ record.checkWarehouseNo }}</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            class="button-info"
            v-if="$hasPermissions('B_inventoryConfirmationConfirm')"
            @click="handleConfirm(record)"
            id="inventoryConfirmationList-audit-btn">盘点确认</a-button>
          <a-button
            size="small"
            type="link"
            class="button-success"
            v-if="$hasPermissions('B_inventoryConfirmationCheck')"
            @click="handleCheck(record)"
            id="inventoryConfirmationList-audit-btn">重新盘点</a-button>
          <span v-if="!($hasPermissions('B_inventoryConfirmationConfirm') && $hasPermissions('B_inventoryConfirmationCheck'))">--</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import { verifyQueryPage, checkWarehouseReCheck, checkWarehouseFinanceAudit } from '@/api/checkWarehouse'
export default {
  name: 'InventoryConfirmationList',
  mixins: [commonMixin],
  components: { STable, VSelect, chooseWarehouse },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        deleteFlag: '0',
        state: 'WAIT_FINANCE_AUDIT',
        checkWarehouseNo: '',
        warehouseSn: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      tabPaneData: [
        { name: '未完结', type: 'WAIT_FINANCE_AUDIT' },
        { name: '已完结', type: 'FINISH' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return verifyQueryPage(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
        })
      }
    }
  },
  computed: {
    columns () {
      let arr = []
      if (this.queryParam.state == 'WAIT_FINANCE_AUDIT') { // 未完结
        arr = [
          { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
          { title: '盘点单号', dataIndex: 'checkWarehouseNo', scopedSlots: { customRender: 'checkWarehouseNo' }, width: '15%', align: 'center' },
          { title: '盘点状态', dataIndex: 'checkStateDictValue', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '监盘状态', dataIndex: 'checkSuperviseStateDictValue', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '盘点人', dataIndex: 'checkPersonName', align: 'center', width: '14%', customRender: function (text) { return text || '--' }, ellipsis: true },
          { title: '仓库', dataIndex: 'warehouseName', align: 'center', width: '14%', customRender: function (text) { return text || '--' }, ellipsis: true },
          { title: '盘点时间', dataIndex: 'checkTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '监盘人', dataIndex: 'checkSupervisePersonName', align: 'center', width: '14%', customRender: function (text) { return text || '--' }, ellipsis: true },
          { title: '监盘时间', dataIndex: 'checkSuperviseTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
        ]
      } else if (this.queryParam.state == 'FINISH') { // 已完结
        arr = [
          { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
          { title: '盘点单号', dataIndex: 'checkWarehouseNo', scopedSlots: { customRender: 'checkWarehouseNo' }, width: '14%', align: 'center' },
          { title: '盘点状态', dataIndex: 'checkStateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '监盘状态', dataIndex: 'checkSuperviseStateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '盘点人', dataIndex: 'checkPersonName', align: 'center', width: '16%', customRender: function (text) { return text || '--' }, ellipsis: true },
          { title: '仓库', dataIndex: 'warehouseName', align: 'center', width: '16%', customRender: function (text) { return text || '--' }, ellipsis: true },
          { title: '盘点时间', dataIndex: 'checkTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '监盘人', dataIndex: 'checkSupervisePersonName', align: 'center', width: '16%', customRender: function (text) { return text || '--' }, ellipsis: true },
          { title: '监盘时间', dataIndex: 'checkSuperviseTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
          { title: '财务确认时间', dataIndex: 'financeAuditTime', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
        ]
      }
      return arr
    }
  },
  methods: {
    // 选择类型
    handleTabChange (key) {
      this.queryParam.state = key
      this.$refs.table.refresh(true)
    },
    // 详情
    handleDetail (row) {
      if (this.$hasPermissions('B_inventoryConfirmationDetail')) {
        this.$router.push({ name:"inventoryConfirmationDetail",params:{sn:row.checkWarehouseSn} })
      }
    },
    // 盘点确认
    handleConfirm (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认盘点结果正确无误吗？',
        centered: true,
        closable: true,
        onOk () {
          _this.spinning = true
          checkWarehouseFinanceAudit({ checkWarehouseSn: row.checkWarehouseSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh(true)
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 重新盘点
    handleCheck (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要重新盘点吗？',
        centered: true,
        closable: true,
        onOk () {
          _this.spinning = true
          checkWarehouseReCheck({ checkWarehouseSn: row.checkWarehouseSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh(true)
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    resetSearchForm () {
      this.queryParam.checkWarehouseNo = ''
      this.queryParam.warehouseSn = undefined
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      this.tableHeight = window.innerHeight - 290
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.$refs.table.refresh(true)
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.$refs.table.refresh(true)
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .inventoryConfirmationList-wrap{
    .ant-card-body{
      &:last-child{
        padding-top: 0;
      }
    }
    // tab样式
    .inventoryConfirmationList-tabs.ant-tabs.ant-tabs-card .ant-tabs-card-bar .ant-tabs-nav-container{
      height: 30px!important;
    }
    .inventoryConfirmationList-tabs.ant-tabs.ant-tabs-card .ant-tabs-card-bar .ant-tabs-tab-active{
      height: 30px!important;
    }
    .inventoryConfirmationList-tabs.ant-tabs.ant-tabs-card .ant-tabs-card-bar .ant-tabs-tab{
      line-height: 26px!important;
      height: 30px!important;
    }
  }
</style>
