<template>
  <a-card size="small" :bordered="false" class="warehousingAuditList-wrap">
    <!-- 搜索条件 -->
    <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-purchaseNo" v-model.trim="queryParam.purchaseNo" allowClear placeholder="请输入采购单号"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="采购入库单号">
              <a-input id="warehousingAuditList-purchaseNo" v-model.trim="queryParam.purchaseNo" allowClear placeholder="请输入采购入库单号"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="入库时间">
              <a-range-picker
                style="width:100%"
                id="warehousingAuditList-creatDate"
                :disabledDate="disabledDate"
                v-model="createDate"
                :format="dateFormat"
                :placeholder="['开始时间', '结束时间']" />
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="财务审核状态">
                <v-select
                  v-model="queryParam.billStatus"
                  ref="billStatus"
                  id="warehousingAuditList-billStatus"
                  code="PAYMENT_TYPE"
                  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>6</strong> 条记录，其中待审核 <strong>14</strong> 条 </div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1460 }"
      bordered>
      <!-- 采购单号 -->
      <template slot="purchaseBillNo" slot-scope="text, record">
        <span style="color: #ed1c24;cursor: pointer;">{{ record.purchaseBillNo }}</span>
      </template>
      <!-- 采购入库单号 -->
      <template slot="purchaseNos" slot-scope="text, record">
        <span style="color: #ed1c24;cursor: pointer;">{{ record.purchaseNo }}</span>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" class="button-info" @click="handleExamine(record, 1)" id="warehousingAudit-adopt-btn">通过</a-button>
        <a-button size="small" type="link" class="button-warning" @click="handleExamine(record, 2)" id="warehousingAudit-unadopt-btn">不通过</a-button>
        <a-button size="small" type="link" class="button-success" @click="handleDetail(record)" id="warehousingAudit-detail-btn">详情</a-button>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable, VSelect } from '@/components'
import getDate from '@/libs/getDate'
import { stockPutList } from '@/api/stockPut'
export default {
  components: { STable, VSelect },
  data () {
    return {
      advanced: false, //  高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      createDate: [
        moment(getDate.getLastThreeMonthDays().starttime, this.dateFormat),
        moment(getDate.getLastThreeMonthDays().endtime, this.dateFormat)
      ], //  创建时间
      dateFormat: 'YYYY-MM-DD',
      tableHeight: 0,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '采购单号', dataIndex: 'putBizNo', align: 'center' },
        { title: '采购入库单号', scopedSlots: { customRender: 'purchaseNos' }, width: 220, align: 'center' },
        { title: '总款数', dataIndex: 'productTotalCategory', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库总数量', dataIndex: 'productTotalQty', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库总成本', dataIndex: 'productTotalCost', width: 110, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '入库时间', dataIndex: 'putTime', 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' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 180, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 440
        }
        // 创建时间
        if (this.createDate && this.createDate.length > 0) {
          this.queryParam.beginDate = moment(this.createDate[0]).format(this.dateFormat)
          this.queryParam.endDate = moment(this.createDate[1]).format(this.dateFormat)
        } else {
          this.queryParam.beginDate = undefined
          this.queryParam.endDate = undefined
        }
        return stockPutList(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
          }
          this.disabled = false
          return data
        })
      }
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    //  详情
    handleDetail (row) {
      this.$router.push({ path: `/financialManagement/warehousingAudit/detail/${row.id}` })
    },
    //  审核
    handleExamine (row, type) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '操作后不可恢复，确定要进行' + (type == 1 ? ' 通过 ' : ' 不通过 ') + '操作吗？',
        centered: true,
        onOk () {
          // delectRolePower({
          //   id: row.id
          // }).then(res => {
          //   console.log(res, 'res1111')
          //   if (res.status == 200) {
          //     _this.$message.success(res.message)
          //     _this.$refs.table.refresh()
          //   }
          // })
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.orderBundleNo = ''
      this.queryParam.orderBundle.custMobile = ''
      this.queryParam.bundleName = ''
      this.queryParam.itemName = ''
      this.oldTime = undefined
      this.newTime = undefined
      this.$refs.table.refresh(true)
    }
  }
}
</script>
<style lang="less">
  .warehousingAuditList-wrap{
    .sTable{
      margin-top: 15px;
    }
  }
</style>
