<template>
    <div>
        <head-todo
        :selectedIds="selectedIds"
        @getList="getList"
        @handleOpenForm="handleOpenAddForm">
        </head-todo>

        <center-list
            :tableData="tableList"
            :currentPage="currentPage"
            :pageSize="pageSize"
            :total="total"
            @handleSelectChange="cacheSelectedIds"
            @getList="getList"
            @handleOpenForm="handleOpenEditForm"
            @handleOpenDetail="handleOpenDetail">
        </center-list>
        <add-form
          :addFormVisible="addFormVisible"
          :mode="mode"
          :tableData="FormList"
          :total="Formtotal"
          :rowData="rowData"
          :headData="headData"
          @handleFormClose="handleFormClose"
          @handleChageTable="handleChageTable"
          @saveOK="getList">
        </add-form>
    </div>
</template>
<script>
import HeadTodo from './components/head-todo'
import CenterList from './components/center-list'
import AddForm from './components/add-form'
export default {
  name: 'LibraryBatch',
  components: {
    HeadTodo,
    CenterList,
    AddForm
  },
  data () {
    return {
      tableList: [],
      total: 0,
      query: {
        'page.currentPage': 1,
        'page.limitCount': 10,
      },
      selectedIds: '',
      addFormVisible: false,
      rowData: {},
      FormList: [],
      headData: {},
      Formtotal: 0,
      mode: 'create',
      pageSize: 10,
      currentPage: 1
    }
  },
  created () {
    this.getList()
  },
  mounted () {

  },
  methods: {
    getList (query) {
      const url = this.$API.warehouseBasic.libraryBatch.getList
      const param = { ...this.query, ...query }
      this.$https.post(url, param).then(res => {
        this.tableList = res.rows
        this.total = res.total
        this.currentPage = res.currentPage
        this.pageSize = res.limit
      })
    },

    getForm (query) {
      const url = this.$API.warehouseBasic.libraryBatch.addGetList
      const param = { ...this.query, ...query }
      this.$https.post(url, param).then(res => {
        this.FormList = res.rows
        this.Formtotal = res.total
      })
    },

    getHead (query) {
      const url = this.$API.warehouseBasic.libraryBatch.getEditHead
      const param = { ...query }
      this.$https.post(url, param).then(res => {
        this.headData = res
      })
    },
    // 新增
    handleOpenAddForm (mode) {
      this.mode = mode
      this.rowData = {}
      this.getForm()
      this.addFormVisible = true
    },

    // 编辑
    handleOpenEditForm (id, mode) {
      this.mode = mode
      const query = { lotCode: id }
      this.getForm(query)
      this.getHead(query)
      this.addFormVisible = true
    },
    // 详情
    handleOpenDetail (mode, data, param) {
      const query = param
      this.rowData = data
      this.mode = mode
      this.getForm(query)
      this.addFormVisible = true
    },

    // 关闭新增弹窗
    handleFormClose () {
      this.addFormVisible = !this.addFormVisible
    },

    //  改变数据
    handleChageTable (row, index) {
      this.$set(this.FormList, index, row)
    },

    // checkbox勾选储存id
    cacheSelectedIds (ids) {
      this.selectedIds = ids
    }
  }
}
</script>
