<template>
  <a-card size="small" :bordered="false" class="warehousingAuditList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <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="warehousingAuditList-purchaseBillNo" v-model.trim="queryParam.purchaseBillNo" allowClear placeholder="请输入采购单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="采购入库单号">
                <a-input id="warehousingAuditList-receivingBillNo" v-model.trim="queryParam.receivingBillNo" allowClear placeholder="请输入采购入库单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="入库时间">
                <rangeDate ref="rangeDate" :value="auditTime" @change="dateChange" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="入库审核状态">
                  <v-select
                    v-model="queryParam.auditState"
                    ref="auditState"
                    id="warehousingAuditList-auditState"
                    code="RECEIVING_BILL_STATUS"
                    placeholder="请选择入库审核状态"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="warehousingAuditList-refresh">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm()" :disabled="disabled" id="warehousingAuditList-reset">重置</a-button>
                <a @click="advanced=!advanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- alert -->
      <a-alert type="info" showIcon style="margin-bottom:15px">
        <div slot="message">共 <strong>{{ (productTotal&&productTotal.totalRecord) || 0 }}</strong> 条记录，其中待审核 <strong>{{ (productTotal&&productTotal.waitAuditRecord) || 0 }}</strong> 条 </div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1420, y: tableHeight }"
        bordered>
        <!-- 采购入库单号 -->
        <template slot="receivingBillNo" slot-scope="text, record">
          <span v-if="record.receivingBillNo" style="color: #ed1c24;cursor: pointer;" @click="handleDetail(record)">{{ record.receivingBillNo }}</span>
          <span v-else>--</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            v-if="record.auditState=='WAIT_PUT_WAREHOUSE_AUDIT'"
            type="link"
            class="button-info"
            @click="handleExamine(record, 1)"
            id="warehousingAudit-adopt-btn">通过</a-button>
          <a-button
            size="small"
            v-if="record.auditState=='WAIT_PUT_WAREHOUSE_AUDIT'"
            type="link"
            class="button-warning"
            @click="handleExamine(record, 2)"
            id="warehousingAudit-unadopt-btn">不通过</a-button>
          <span v-if="record.auditState!='WAIT_PUT_WAREHOUSE_AUDIT'">--</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import moment from 'moment'
import getDate from '@/libs/getDate'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { receivingList, receivingStockInAudit, receivingCount } from '@/api/receiving'
export default {
  components: { STable, VSelect, rangeDate },
  data () {
    return {
      spinning: false,
      advanced: false, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      auditTime: [
        moment(getDate.getThreeMonthDays().starttime, 'YYYY-MM-DD'),
        moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
      ], //  入库时间
      tableHeight: 0,
      // 查询参数
      queryParam: {
        beginDate: getDate.getThreeMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        purchaseBillNo: '',
        receivingBillNo: '',
        auditState: undefined
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '采购单号', dataIndex: 'purchaseBillNo', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '采购入库单号', scopedSlots: { customRender: 'receivingBillNo' }, width: 220, align: 'center' },
        { title: '总款数', dataIndex: 'totalPutCategory', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库总数量', dataIndex: 'totalPutQty', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库总成本', dataIndex: 'totalPutAmount', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '签收入库时间', dataIndex: 'stockPutTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务入库审核时间', dataIndex: 'auditTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务审核状态', dataIndex: 'auditStateDictValue', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 140, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 370
        }
        const params = Object.assign(parameter, this.queryParam)
        return receivingList(params).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
          }
          this.disabled = false
          this.getTotal(params)
          return data
        })
      },
      productTotal: null
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 合计
    getTotal (param) {
      receivingCount(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  详情
    handleDetail (row) {
      this.$router.push({ path: `/financialManagement/warehousingAudit/detail/${row.receivingBillSn}` })
    },
    //  审核
    handleExamine (row, type) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定要进行' + (type == 1 ? ' 通过 ' : ' 不通过 ') + '操作吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          receivingStockInAudit({ id: row.id, auditState: type == 1 ? 'FINISH' : 'PUT_WAREHOUSE_AUDIT_REJECT' }).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.auditTime)
      this.queryParam.beginDate = getDate.getThreeMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.purchaseBillNo = ''
      this.queryParam.receivingBillNo = ''
      this.queryParam.auditState = undefined
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less">
  .warehousingAuditList-wrap{
    .sTable{
      margin-top: 15px;
    }
  }
</style>
