<!-- 入库记录 -->
<template>
  <div class="container">
    <my-table :data="data" :columns="columns" :table-filter="tableFilter" :table-page="params" @event-handle="eventHandle" @on-change="changePage"></my-table>
    <!-- 采购申请单 -->
    <purchase-form v-model="show" :data="formData"></purchase-form>
    <!-- 采购入库单 -->
    <store-in-form v-model="storeInShow" :data="formData"></store-in-form>
  </div>
</template>

<script>
import { exportExcel } from '@/libs/util'
import { GetInStockPageList, GetInStockBill, GetApplyBill } from '@/api/stock'
import StoreInForm from './store-in-form'
import MyTable from '_c/my-table/my-table'
import PurchaseForm from './purchase-form'
export default {
  name: 'store_in_record',
  components: {
    MyTable,
    StoreInForm,
    PurchaseForm
  },
  data () {
    return {
      formData: {},
      show: false,
      storeInShow: false,
      data: [],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '申购单号',
          key: 'applyPurchaseCode',
          minWidth: 150,
          render: (h, params) => {
            let _this = this
            return h('a', {
              on: {
                click () {
                  // this.$post('/api/storage-restructure/applyPurchase/amountStatistics',{
                  //   applyBillNo: params.row.applyPurchaseCode,
                  // }).then(res=>{
                  //     this.totalData = res.data
                  //     this.company = this.totalData.statisticsSupplier[0]
                  //     this.$Spin.hide()
                  // })
                  GetApplyBill({
                    applyBillNo: params.row.applyPurchaseCode
                  }).then(res => {
                    if (res.code === 0) {
                      _this.formData = { ...params.row, ...res.result }
                      _this.show = true
                    }
                  })
                }
              }
            }, params.row.applyPurchaseCode)
          }
        },
        {
          title: '入库单号',
          key: 'contractDetailsCode',
          minWidth: 150,
          render: (h, params) => {
            let _this = this
            return h('a', {
              on: {
                click () {
                  GetInStockBill({
                    inOutStorageNo: params.row.inOutStorageNo,
                    applyBillNo: params.row.applyBillNo
                  }).then(res => {
                    if (res.code === 0) {
                      _this.formData = res.result
                      _this.storeInShow = true
                    }
                  })
                }
              }
            }, params.row.contractDetailsCode)
          }
        },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '申购数量',
          key: 'quantity',
          minWidth: 150
        },
        {
          title: '已入库数量',
          key: 'arrivalQuantity',
          minWidth: 150
        },
        {
          title: '未入库数量',
          key: 'afloatQuantity',
          minWidth: 150
        },
        {
          title: '状态',
          key: 'completeStatus',
          minWidth: 150
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '导出Excel',
          e: 'export'
        },
        {
          name: 'RadioGroup',
          value: 'isComplete',
          list: [
            { label: '已完成', value: 1 },
            { label: '未完成', value: 0 }
          ]
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0,
        isComplete: 1
      },
      key: ''
    }
  },
  methods: {
    cfm (type, formData) {
      if (type === 'add') {
        FacPriceAdd({
          ...formData,
          materialCode: formData.code
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      } else if (type === 'edit') {
        FacPriceChange({
          ...formData,
          factoryPriceId: formData.id
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.key = option.key
          this.params.isComplete = option.isComplete
          this.getList()
          break
        case 'export':
        this
          .$post('/api/storage-restructure/contractDetails/inPage', {
            ...this.params,
            pageIndex: 1,
            pageSize: 99999
          }).then(res => {
            if (res.code === 200) {
              let data = res.data.records
              if (data.length < 1) return this.$Message.error('数据为空！')
              exportExcel(this.columns, data, '入库记录')
            }
          })
          
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      this
        .$post('/api/storage-restructure/contractDetails/inPage', {
          ...this.params,
          status: this.params.isComplete
        }).then(res => {
          if (res.code === 200) {
            this.data = res.data.records
            this.params.total = res.data.total
          }
        })
    }
  },
  mounted () {
    this.key = this.$route.query.materialCode
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
</style>
