<!-- 领料管理 -->
<template>
  <div class="container">
    <my-table
      :data="data"
      :columns="(['supervisor','storekeeper','manager','admin'].includes(this.access) && params.takeState === 1) ? columns : columns1"
      :table-filter="tableFilter"
      :table-page="params"
      @event-handle="eventHandle"
      @on-change="changePage">
    </my-table>
    <my-form v-model="show" @getlist="getList" :type="formType" :data="formData"></my-form>
  </div>
</template>

<script>
import { GetApplyTakePageList, CancelApplyTake } from '@/api/stock'
import MyTable from '_c/my-table/my-table'
import MyForm from './my-form'
export default {
  name: 'apply_manage',
  components: {
    MyTable,
    MyForm
  },
  data () {
    return {
      access: this.$store.state.user.access,
      formType: '',
      formData: {},
      show: false,
      data: [],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '领料单号',
          key: 'applyBillNo',
          minWidth: 200,
          render: (h, params) => {
            let _this = this
            return h('a', {
              on: {
                click () {
                  _this.formType = 'form'
                  _this.formData = { ...params.row }
                  _this.show = true
                }
              }
            }, params.row.applyBillNo)
          }
        },
        {
          title: '申请部门',
          key: 'deptName',
          minWidth: 100
        },
        {
          title: '申请人',
          key: 'userName',
          minWidth: 150
        },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '申请数量',
          key: 'takeQty',
          minWidth: 100
        },
        {
          title: '领料状态',
          key: 'takeState',
          align: 'center',
          minWidth: 100
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          width: '100px',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  style: {
                  },
                  on: {
                    click: () => {
                      this.$MyModal.show({ text: '是否取消？' }).$on('confirm', () => {
                        CancelApplyTake({
                          takeIds: [ params.row.id ]
                        }).then(res => {
                          if (res.code === 0) {
                            this.$Message.info(res.msg)
                            this.getList()
                          }
                        })
                      })
                    }
                  }
                },
                '取消'
              )
            ])
          }
        }
      ],
      columns1: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '领料单号',
          key: 'applyBillNo',
          minWidth: 200,
          render: (h, params) => {
            let _this = this
            return h('a', {
              on: {
                click () {
                  _this.formType = 'form'
                  _this.formData = { ...params.row }
                  _this.show = true
                }
              }
            }, params.row.applyBillNo)
          }
        },
        {
          title: '申请部门',
          key: 'deptName',
          minWidth: 100
        },
        {
          title: '申请人',
          key: 'userName',
          minWidth: 150
        },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '申请数量',
          key: 'takeQty',
          minWidth: 100
        },
        {
          title: '领料状态',
          key: 'takeState',
          align: 'center',
          fixed: 'right',
          minWidth: 100
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'RadioGroup',
          value: 'takeState',
          list: [
            { label: '待领料', value: 1 },
            { label: '已完成', value: 2 }
          ]
        },
        {
          name: 'Button',
          type: 'primary',
          text: '领料申请',
          e: 'add',
          align: 'right'
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0,
        takeState: 1
      }
    }
  },
  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.applyBillNo = option.key
          this.params.takeState = option.takeState
          this.getList()
          break
        case 'add':
          this.formType = 'add'
          this.show = true
          break
        case 'back':
          this.$router.go(-1)
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      GetApplyTakePageList(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>
