<template>
  <a-card size="small" :bordered="false" class="warehousingAuditList-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="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_AUDIT_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" style="margin-bottom:10px">
        <div slot="message">
          共 <strong>{{ (productTotal&&(productTotal.totalRecord || productTotal.totalRecord==0)) ? productTotal.totalRecord : '--' }}</strong> 条记录，
          其中待审核 <strong>{{ (productTotal&&(productTotal.waitAuditRecord || productTotal.waitAuditRecord==0)) ? productTotal.waitAuditRecord : '--' }}</strong> 条
        </div>
      </a-alert>
      <!-- 列表 -->
      <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="receivingBillNo" slot-scope="text, record">
          <span v-if="record.receivingBillNo" :class="$hasPermissions('M_warehousingAudit_detail')?'active':'common'" @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' && $hasPermissions('B_receivingAudit')"
            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'&& $hasPermissions('B_receivingAudit')"
            type="link"
            class="button-warning"
            @click="handleExamine(record, 2)"
            id="warehousingAudit-unadopt-btn">不通过</a-button>
          <span v-if="record.auditState!='WAIT_PUT_WAREHOUSE_AUDIT'|| !$hasPermissions('B_receivingAudit')">--</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import getDate from '@/libs/getDate'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { receivingAuditList, receivingStockInAudit, receivingCount } from '@/api/receiving'
export default {
  name: 'WarehousingAuditList',
  components: { STable, VSelect, rangeDate },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      advanced: true, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      auditTime: [
        moment(getDate.getThreeMonthDays().starttime + ' 00:00:00', 'YYYY-MM-DD'),
        moment(getDate.getCurrMonthDays().endtime + ' 23:59:59', 'YYYY-MM-DD')
      ], //  入库时间
      tableHeight: 0,
      // 查询参数
      queryParam: {
        beginDate: getDate.getThreeMonthDays().starttime + ' 00:00:00',
        endDate: getDate.getCurrMonthDays().endtime + ' 23:59:59',
        purchaseBillNo: '',
        receivingBillNo: '',
        auditState: 'WAIT_PUT_WAREHOUSE_AUDIT'
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '采购单号', dataIndex: 'purchaseBillNo', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '发货单号', scopedSlots: { customRender: 'receivingBillNo' }, width: '15%', align: 'center' },
        { title: '总款数', dataIndex: 'totalPutCategory', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库总数量', dataIndex: 'totalPutQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库总成本', dataIndex: 'totalPutAmount', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '签收入库时间', dataIndex: 'stockPutTime', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务入库审核时间', dataIndex: 'auditTime', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '财务审核状态', dataIndex: 'auditStateDictValue', 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
        const params = Object.assign(parameter, this.queryParam)
        return receivingAuditList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getTotal(params)
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              if (data.list[i].auditState == 'WAIT_PUT_WAREHOUSE_AUDIT') {
                data.list[i].auditStateDictValue = '待审核'
              } else if (data.list[i].auditState == 'FINISH') {
                data.list[i].auditStateDictValue = '已审核'
              } else if (data.list[i].auditState == 'PUT_WAREHOUSE_AUDIT_REJECT') {
                data.list[i].auditStateDictValue = '审核不通过'
              }
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      productTotal: null
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] ? date[0] + ' 00:00:00' : ''
      this.queryParam.endDate = date[1] ? date[1] + ' 23:59:59' : ''
    },
    // 合计
    getTotal (param) {
      receivingCount(param).then(res => {
        if (res.status == 200 && res.data) {
          this.productTotal = res.data
        } else {
          this.productTotal = null
        }
      })
    },
    //  详情
    handleDetail (row) {
      if (this.$hasPermissions('M_warehousingAudit_detail')) {
        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.resetData()
      this.$refs.table.refresh(true)
    },
    // 重置数据
    resetData () {
      this.$refs.rangeDate.resetDate(this.auditTime)
      this.queryParam.beginDate = getDate.getThreeMonthDays().starttime + ' 00:00:00'
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime + ' 23:59:59'
      this.queryParam.purchaseBillNo = ''
      this.queryParam.receivingBillNo = ''
      this.queryParam.auditState = 'WAIT_PUT_WAREHOUSE_AUDIT'
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 238
    }
  },
  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.resetData()
      if (this.$route.query.pageParams) {
        this.queryParam.auditState = this.$route.query.pageParams.auditState
      }
      this.pageInit()
      this.$refs.table.refresh(true)
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .warehousingAuditList-wrap{
    .active{
      color: #ed1c24;
      cursor: pointer;
    }
    .common{
      color: rgba(0, 0, 0);
    }
  }
</style>
