<!-- 仓库维护 -->
<template>
  <div class="container">
    <my-table :data="data" :columns="columns" :table-filter="tableFilter" :table-page="params" @event-handle="eventHandle" @on-change="changePage"></my-table>
    <my-modal-form v-model="show" :title="title" :config="formConfig" :rule-validate="ruleValidate" :data="formData" :type="formType" @confirm="cfm"></my-modal-form>
  </div>
</template>

<script>
import { GetStockHousePageList, StockHouseAdd, StockHouseUpdate, StockHouseDelete } from '@/api/baseData'
import MyTable from '_c/my-table/my-table'
import MyModalForm from '_c/my-modal-form/my-modal-form'
export default {
  name: 'warehouse_maintenance',
  components: {
    MyTable,
    MyModalForm
  },
  data () {
    return {
      title: '',
      show: false,
      data: [],
      deleteId: '',
      formType: '',
      formData: {},
      ruleValidate: {
        name: [
          { required: true, message: '仓库名称不能为空', trigger: 'blur' }
        ]
      },
      formConfig: [
        {
          type: 'input',
          label: '仓库名称',
          key: 'name',
          placeholder: '请输入仓库名称'
        },
        {
          type: 'input',
          comType: 'textarea',
          label: '仓库说明',
          key: 'remark',
          placeholder: '请输入仓库说明'
        }
      ],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '仓库名称',
          key: 'name',
          minWidth: 150
        },
        {
          title: '仓库描述',
          key: 'remark',
          minWidth: 250
        },
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          align: 'center',
          width: '200px',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '20px'
                  },
                  on: {
                    click: () => {
                      this.$router.push({
                        name: 'subarea_manage',
                        query: {
                          stockHouseId: params.row.id,
                          name: params.row.name
                        }
                      })
                    }
                  }
                },
                '分区管理'
              ),
              h(
                'Icon',
                {
                  props: {
                    custom: 'iconfont icon-edit',
                    size: '20',
                    color: '#087BE1'
                  },
                  style: {
                    marginRight: '20px'
                  },
                  on: {
                    click: () => {
                      for (const key in params.row) {
                        this.formData[key] = params.row[key]
                      }
                      this.title = '修改'
                      this.formType = 'edit'
                      this.show = true
                    }
                  }
                },
                '修改'
              ),
              h(
                'Icon',
                {
                  props: {
                    custom: 'iconfont icon-delete',
                    size: '20',
                    color: 'red'
                  },
                  on: {
                    click: () => {
                      const v = this
                      this.$MyModal.show({ text: '是否删除？' }).$on('confirm', () => {
                        this.$post('/api/storage-restructure/stockHouse/delete',
                        {id: params.row.id}).then(res=>{
                          if(res.code == 200){
                            this.$Message.info('删除成功')
                            v.getList()
                          }
                        })
                      })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '添加',
          align: 'right',
          e: 'add'
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    cfm (type, formData) {
      if (type === 'add') {
        this.$post('/api/storage-restructure/stockHouse/add',formData).then(res=>{
          if(res.code == 200){
            this.show = false
            this.$Message.info('新增成功')
            this.getList()
          }
        })
      } else if (type === 'edit') {
        this.$post('/api/storage-restructure/stockHouse/edit',formData).then(res=>{
          if(res.code == 200){
            this.show = false
            this.$Message.info('编辑成功')
            this.getList()
          }
        })
        
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.warehouseName = option.key
          this.getList()
          break
        case 'add':
          this.title = '添加仓库'
          this.formType = 'add'
          this.show = true
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      this.$post('/api/storage-restructure/stockHouse/page',this.params).then(res=>{
        this.data = res.data.records
        this.params.total = res.data.total
      })
      // GetStockHousePageList(this.params).then(res => {
      //   if (res.code === 0) {
      //     this.data = res.result.list
      //     this.params.total = res.result.totalCount
      //   }
      // })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>

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