<template>
  <a-card size="small" :bordered="false" class="inventoryCheckAuditList-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="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="盘点单号">
                <a-input id="inventoryCheckAuditList-checkWarehouseNo" v-model.trim="queryParam.checkWarehouseNo" allowClear placeholder="请输入盘点单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="盘点完成时间">
                <rangeDate ref="rangeDate" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="inventoryCheckAuditList-refresh">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="inventoryCheckAuditList-reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 单号 -->
        <template slot="checkWarehouseNo" slot-scope="text, record">
          <span style="color: #ed1c24;cursor: pointer;" v-if="$hasPermissions('B_inventoryCheckingDetail')" @click="handleDetail(record)">{{ record.checkWarehouseNo }}</span>
          <span v-else>{{ record.checkWarehouseNo }}</span>
        </template>
        <!-- 盈亏数量 -->
        <template slot="totalLossQty" slot-scope="text, record">
          <span>{{ (record.totalLossQty || record.totalLossQty==0) ? Math.abs(record.totalLossQty) : '--' }}</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            v-if="record.state=='WAIT_FINANCE_AUDIT' && $hasPermissions('B_inventoryCheckAuditAudit')"
            type="link"
            class="button-info"
            @click="handleExamine(record, true)"
            id="warehousingAudit-adopt-btn">通过</a-button>
          <a-button
            size="small"
            type="link"
            v-if="record.state == 'WAIT_FINANCE_AUDIT' && $hasPermissions('B_inventoryCheckingAgain')"
            class="button-warning"
            @click="handleAgain(record)"
          >重盘</a-button>
          <span v-if="!(record.state=='WAIT_FINANCE_AUDIT' && ($hasPermissions('B_inventoryCheckAuditAudit') || $hasPermissions('B_inventoryCheckingAgain')))">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 详情 -->
    <inventoryChecking-detail-modal :openModal="openDetailModal" :itemSn="itemSn" @close="handleDetailClose" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import inventoryCheckingDetailModal from '@/views/inventoryManagement/inventoryChecking/detailModal.vue'
import { checkWarehouseAuditList, checkWarehouseFinanceAudit, checkWarehouseReInventory } from '@/api/checkWarehouse'
export default {
  name: 'InventoryCheckingList',
  components: { STable, VSelect, rangeDate, inventoryCheckingDetailModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      // 查询参数
      queryParam: {
        beginDate: undefined,
        endDate: undefined,
        checkWarehouseNo: ''
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '盘点单号', scopedSlots: { customRender: 'checkWarehouseNo' }, width: '15%', align: 'center' },
        { title: '总款数', dataIndex: 'productTotalCategory', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总数量', dataIndex: 'totalStockQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总成本', dataIndex: 'totalStockCost', width: '11%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盘盈数量', dataIndex: 'totalProfitQty', width: '7%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盘亏数量', scopedSlots: { customRender: 'totalLossQty' }, width: '7%', align: 'center' },
        { title: '盘点完成时间', dataIndex: 'checkTime', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务审核时间', dataIndex: 'financeAuditTime', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务审核状态', dataIndex: 'stateDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return checkWarehouseAuditList(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
              data.list[i].financeFinishTime = data.list[i].financeAuditTime
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      productTotal: null,
      openDetailModal: false,
      itemSn: null
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    //  详情
    handleDetail (row) {
      this.itemSn = row.checkWarehouseSn
      this.openDetailModal = true
    },
    handleDetailClose () {
      this.itemSn = null
      this.openDetailModal = false
    },
    //  审核
    handleExamine (row, type) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定要进行' + (type ? ' 通过 ' : ' 不通过 ') + '操作吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          checkWarehouseFinanceAudit({ id: row.id, auditFlag: type }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 重盘
    handleAgain (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '重新盘点后，盘点单状态回退到待提交。确认重新盘点吗？',
        centered: true,
        onOk () {
          checkWarehouseReInventory({ id: row.id }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = undefined
      this.queryParam.endDate = undefined
      this.queryParam.checkWarehouseNo = ''
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 195
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
