<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="{ x: 1150, y: tableHeight }"
        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 :style="{ color: record.totalLossQty>0?'red':record.totalLossQty<0?'green':'', fontWeight: record.totalLossQty!=0?'bold':'' }">{{ (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"
            v-if="record.state=='WAIT_FINANCE_AUDIT' && $hasPermissions('B_inventoryCheckAuditAudit')"
            type="link"
            class="button-warning"
            @click="handleExamine(record, false)"
            id="warehousingAudit-unadopt-btn">不通过</a-button>
          <span v-if="!(record.state=='WAIT_FINANCE_AUDIT' && $hasPermissions('B_inventoryCheckAuditAudit'))">--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 详情 -->
    <inventoryChecking-detail-modal :openModal="openDetailModal" :itemSn="itemSn" @close="handleDetailClose" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import inventoryCheckingDetailModal from '@/views/inventoryManagement/inventoryChecking/detailModal.vue'
import { checkWarehouseAuditList, checkWarehouseFinanceAudit } from '@/api/checkWarehouse'
export default {
  components: { STable, VSelect, rangeDate, inventoryCheckingDetailModal },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      // 查询参数
      queryParam: {
        beginDate: undefined,
        endDate: undefined,
        checkWarehouseNo: ''
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '盘点单号', scopedSlots: { customRender: 'checkWarehouseNo' }, align: 'center' },
        { title: '总款数', dataIndex: 'productTotalCategory', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总数量', dataIndex: 'totalStockQty', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '总成本', dataIndex: 'totalStockCost', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盘盈数量', dataIndex: 'totalProfitQty', width: 80, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '盘亏数量', scopedSlots: { customRender: 'totalLossQty' }, width: 80, align: 'center' },
        { title: '盘点完成时间', dataIndex: 'checkTime', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务审核时间', dataIndex: 'financeAuditTime', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务审核状态', dataIndex: 'stateDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 110, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return checkWarehouseAuditList(Object.assign(parameter, this.queryParam)).then(res => {
          const 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
            }
          })
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = undefined
      this.queryParam.endDate = undefined
      this.queryParam.checkWarehouseNo = ''
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 195
    }
  },
  mounted () {
    const _this = this
    this.$nextTick(() => { // 页面渲染完成后的回调
      _this.setTableH()
    })
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      setTimeout(() => {
        _this.setTableH()
      }, 400)
    }
  }
}
</script>
