<!-- 出库记录 -->
<template>
  <div class="container">
    <my-table
      ref="table"
      :data="data"
      :columns="columns"
      :table-filter="tableFilter"
      :table-page="params"
      @event-handle="eventHandle"
      @on-change="changePage"
    ></my-table>
    <my-form v-model="show" :data="formData"></my-form>
  </div>
</template>

<script>
import { exportExcel } from '@/libs/util'
import { GetOutStockPageList } from '@/api/stock'
import axios from 'axios'
import MyForm from './my-form'
import MyTable from '_c/my-table/my-table2'
import { GetUserPageList } from '@/api/user'
// import poptipTxet from './poptip-text.vue'
export default {
  name: 'store_out_record',
  components: {
    MyTable,
    MyForm
  },
  data () {
    return {
      key: '',
      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 () {
        //           GetOutBillDetail({
        //             fromBillNo: params.row.applyBillNo,
        //             billType: params.row.billType
        //           }).then(res => {
        //             if (res.code === 0) {
        //               _this.formData = { ...params.row, ...res.result }
        //               _this.show = true
        //             }
        //           })
        //         }
        //       }
        //     }, params.row.applyBillNo)
        //   }
        // },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '领料人',
          key: 'realName',
          minWidth: 150
        },
        // {
        //   title: '申领数量',
        //   key: 'applyTakeQuantity',
        //   minWidth: 150
        // },
        {
          title: '出库类型',
          key: 'stockChangeType',
          minWidth: 150,
          render: (h, params) => {
            return h('span', {}, params.row.stockChangeType == 20 ? '出库' : params.row.stockChangeType == 26 ? '退货出库' : '报废出库' )
          }
        },
        {
          title: '出库时间',
          key: 'createdTime',
          minWidth: 150
        },
        {
          title: '出库件数',
          key: 'tagQuantity',
          minWidth: 150,
          render: (h, params) => {
            const arr = params.row.detailsList
            return h('div', [
              h(
                'Poptip',
                {
                  props: {
                    placement: 'right',
                    transfer: true
                  }
                },
                [
                  h(
                    'a',
                    {
                      on: {
                        click: () => {}
                      }
                    },
                    params.row.detailsList.length
                  ),
                  h(
                    'div',
                    {
                      slot: 'content',
                      class: 'api',
                      style: {
                        // textAlign: 'center',
                        padding: '10px'
                      }
                    },
                    arr.map((item, index) => {
                      return [
                        h('p', [
                          h(
                            'span',
                            { style: { marginRight: '30px' } },
                            'RFID：' + item.rfidCode
                          ),
                          h('span', '数量：' + item.changeNum)
                        ])
                      ]
                    })
                  )
                ]
              )
            ])
          }
        },
        {
          title: '出库数量',
          key: 'quantity',
          minWidth: 150,
          render: (h, params) => {
            let _this = this
            var total = 0
            for (let i = 0; i < params.row.detailsList.length; i++) {
              const element = params.row.detailsList[i];
              total = total + Number(element.changeNum)
            }
            return h('span', {}, total)
          }
        },
        {
          title: '备注',
          key: 'remark',
          align: 'center',
          minWidth: 150
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字',
          width:'140px'
        },
        {
          name: 'Select',
          value: 'stockChangeType',
          width: '150px',
          placeholder: '请选择出库类型',
          list: [
            { label: '出库', value: 20 },
            { label: '退货出库', value: 26 },
            { label: '报废出库', value: 27 }
          ]
        },
        {
          name: 'Select',
          value: 'userId',
          placeholder: '请选择领料人',
          width: '150px',
          list: []
        },
        {
          name: 'DatePicker',
          type: 'datetimerangeCopy',
          placeholder: '请选择时间',
          width: '300px',
          value: 'date',
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '导出Excel',
          e: 'export'
        },
        {
          name: 'span',
          text: '出库总数量：0'
        },
        {
          name: 'span',
          text: '出货件数：0'
        }
        // ,
        // {
        //   name: 'RadioGroup',
        //   value: 'takeState',
        //   list: [
        //     { label: '未出库', value: 1 },
        //     { label: '已出库', value: 2 }
        //   ]
        // }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0,
        beginTime:this.$dayjs(new Date).format('YYYY-MM-DD') + ' 00:00',
        endTime:this.$dayjs(new Date).format('YYYY-MM-DD') + ' 23:45',
      }
    }
  },
  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) {
      console.log('????????')
      console.log(option)
      if(option.date[0] && option._evnet == 'datetimerangeCopy'){
        this.params.beginTime = option.date[0]
        this.params.endTime = option.date[1]
      }
      switch (option._evnet) {
        case '"datetimerangeCopy"':
          
          
          break
        case 'search':
          this.params.pageIndex = 1
          this.params.key = option.key
          this.params.takeState = option.takeState
          this.params.stockChangeType = option.stockChangeType
          this.params.userId = option.userId
          console.log(this.params)
          this.getList(this.params)
          break
        case 'export':
          
          axios.post('/cloudApi/stockWater/outPage', {
            stockChangeType:this.params.stockChangeType,
            keyword:this.params.key,
            operUserId:this.params.userId,
            beginTime:this.params.beginTime || this.$dayjs(new Date).format('YYYY-MM-DD') + ' 00:00:00',
            endTime:this.params.endTime || this.$dayjs(new Date).format('YYYY-MM-DD') + ' 23:59:59',
            pageNum: 1,
            pageSize: 99999
          }).then(res=>{
            if(res.data.code == 200){
              let data = res.data.data.records
              if (data.length < 1) return this.$Message.error('数据为空！')
              exportExcel(this.columns, data, '出库记录')
            }
          })
          
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getUserPageList () { 
      axios
				.post('/cloudApi/uUser/list', {
					deptIdList: ['A012', 'A008', 'A007', 'A017', 'A005'],
				})
				.then((res) => {
					let list = res.data.data.map((item) => {
						return {
							label: item.realname,
							value: item.id,
						}
					})
					this.tableFilter[2].list = list
				})
      // GetUserPageList({
      //   pageIndex: 1,
      //   pageSize: 9999,
      //   takeUser: true
      // }).then(res => {
      //   if (res.code === 0) {
      //     let list = res.result.list.map(item => {
      //       return {
      //         label: item.realName,
      //         value: item.id
      //       }
      //     })
      //     this.tableFilter[2].list = list
      //   }
      // })
    },
    getList (params) {
      console.log(params)
      axios.post('/cloudApi/stockWater/outPage', {
        stockChangeType:this.params.stockChangeType,
        keyword:this.params.key,
        operUserId:this.params.userId,
        beginTime:this.params.beginTime,
        endTime:this.params.endTime,
        pageNum: this.params.pageIndex,
        pageSize: this.params.pageSize
      }).then(res=>{
        if(res.data.code == 200){
          
          this.params.total = res.data.data.total
          this.data = res.data.data.records
        }
      })
      axios.post('/cloudApi/stockWater/outStatistics', {
        stockChangeType:this.params.stockChangeType,
        keyword:this.params.key,
        operUserId:this.params.userId,
        beginTime:this.params.beginTime,
        endTime:this.params.endTime,
      }).then(res=>{
        if(res.data.code == 200){
          console.log(res.data)
          this.tableFilter[6].text = res.data.data.sum ? '出库总数量：' +  res.data.data.sum : '出库总数量：' +  0
          this.tableFilter[7].text = '出库件数：' + res.data.data.count || 0
        }
      })
      // GetOutStockPageList({
      //   ...this.params,
      //   key: this.params.key || this.key
      // }).then(res => {
      //   if (res.code === 0) {
      //     this.tableFilter[6].text = '出库总数量：' + res.result.totalQty || 0
      //     this.tableFilter[7].text = '出库件数：' + res.result.totalCount || 0
      //     this.data = res.result.outStockPageList.list
      //     this.params.total = res.result.outStockPageList.totalCount
      //   }
      // })
    }
  },
  mounted () {
    this.key = this.$route.query.materialCode
    this.getList()
    this.getUserPageList()
    this.$nextTick(() => {
      this.$refs.table.setModel('date', [new Date(), new Date()])
    })
  }
}
</script>

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