<!-- 补货任务查询 -->
<template>
  <div>
    <p-card title="补货任务查询">
      <template slot="extra">
        <!-- <p-button type="primary" icon="check" style="margin: 0  5px" @click="finishOk">补货完成</p-button> -->
        <p-button type="primary" icon="printer"  @click="print">打印</p-button>
        <!-- <p-button type="primary" icon="redo" @click="hanlderRest">重置</p-button> -->
      </template>
      <div class="plg-inner-form-mb">
        <p-grid-form ref="search" :items="items" @submit="hanlderSubmit" />
      </div>
      <!-- @checkRow="checkRow" -->
      <p-table
        auto-width
        :scroll="{ x: true }"
        :columns="columns"
        :data-source="data"
        rowKey="moveTaskId"
        dragle
        @checkRow="checkRow"
        @change="changePagination"
        :pagination="pagination"
        :loading="loading"
        bordered
      >
        <!-- <template slot="status" slot-scope="text, row, index">
          {{ text == 0 ? '未开始' : text == 1 ? '进行中' : '已完成' }}
        </template> -->
        <!-- <span slot="action"
		         slot-scope="text, record,index">
		 
		     <a @click="showModal(record)">编辑</a>
		     <a-divider type="vertical"
		                style="" />
		     <a @click="del(record)">删除</a>
		 
		   </span> -->
      </p-table>
    </p-card>
  </div>
</template>

<script>
// 设置默认仓库
import { toPrintServer } from '@/views/kzyWMS/utils/printServer.js'
import { setwarehouse } from '@/views/kzyWMS/SearchModal/mixins_warehouse.js'
import { items, columns } from './config/rTask.js'
import { search_page, search_noPage, updateStatus,getAuthByUser } from './config/api.js'

export default {
  mixins: [setwarehouse],
  data() {
    return {
      items,
      columns,
      data: [],
      loading: false,
      selectedRows: [],
      pagination: {
        total: 0,
        pageSize: 10, //每页中显示10条数据
        current: 1,
        showSizeChanger: true,
        pageSizeOptions: ['10', '15', '20'], //每页中显示的数据
        showTotal: total => `共有 ${total} 条数据` //分页中显示总的数据
      }
    }
  },
  methods: {
    getData(params) {
      params = {
        pageNum: this.pagination.current,
        pageSize: this.pagination.pageSize,
        num: 'OutboundMoveTaskQuery',
        params: params
      }
        this.loading = true;
      search_page(params)
        .then(res => {
          if (res.success) {
            if (res.data) {
              this.data = res.data.list
              this.pagination.total = Number(res.data.totalCount)
              this.loading = false
            } else {
					this.data.length =0;
					this.loading = false
				}
          } else {
            this.loading = false
            this.$exception(res)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    hanlderSubmit(event, form) {
      event.preventDefault()
      // console.log("getFieldsValue:",form.getFieldsValue());
      // this.getData(form.getFieldsValue())
      this.$refs.search.form.validateFields((err, values) => {
        if (!err) {
          console.log('验证通过: ', values)
          this.getData(values)
        }
      })
    },
    // 重置
    hanlderRest(event, form) {
      this.$refs.search.form.resetFields()
    },
    print() {
		console.log(this.$store.getters.userInfo)
      if (this.selectedRows.length < 1) {
        this.$message.error('请至少勾选一条数据')
        return
      }
	  const destBldgCode = this.$refs.search.form.getFieldsValue().destBldgCode
	  const srcBldgCode = this.$refs.search.form.getFieldsValue().srcBldgCode
	  const drawer = this.$refs.search.form.getFieldsValue().drawer
	  const workerNo = this.$refs.search.form.getFieldsValue().workerNo
	  const warehouseId = this.$refs.search.form.getFieldsValue().warehouseId
	  let tablelist = this.selectedRows.map((item, i) => {
		  item.lineNum = i+1
		  return item
	  })
      const params = [
        {
          head: {
            enterpriseName: this.$store.getters.userInfo.enterpriseName,
            opUser: this.$store.getters.userInfo.employeeName,
			destBldgCode,
			drawer,
			workerNo,
			srcBldgCode
          },
          table: tablelist
        }
      ]
      toPrintServer(params, warehouseId, 'buhuo').then(res => {
          if (!res.success) {
            this.$exception(res)
            return
          }
          this.$message.success(res.message);
        })
    },
    // 补货完成
    checkRow(selectedRowKeys, selectedRows) {
      console.log('操作选中行的checkbox：', selectedRowKeys, selectedRows)
      this.selectedRows = [...selectedRows]
    },
    finishOk() {
      if (this.selectedRows.length) {
        // 0未完成   1已完成
        let filish = this.selectedRows.filter(v => v.status != '0')
        if (filish.length) {
          this.$message.warning('选择有已完成！')
        } else {
          updateStatus([...this.selectedRows]).then(res => {
            if (res.success) {
              this.$message.success('补货成功！')
              this.getData({ ...this.$refs.search.form.getFieldsValue() })
            } else {
              this.$exception(res)
            }
          })
        }
      } else {
        this.$message.warning('请选择！')
      }
    },
    changePagination(pagination, filters, sorter) {
      // 分页变化
      console.log(pagination, filters, sorter, '分页变化分页变化')
      this.pagination = pagination
      this.getData({ ...this.$refs.search.form.getFieldsValue() }) //分页查询所有列表
    }
  }, // methods
  mounted() {
    this.getData({ ...this.$refs.search.form.getFieldsValue() })
		// 获取货主信息
		getAuthByUser({companyType:'H',pageNum: 1,pageSize: 10}).then(res=>{
			if(res.success){
				this.$refs.search.form.setFieldsValue({
						cargoOwnerId : res.data.list[0].companyId,
						cargoOwnerCode : res.data.list[0].companyCode,
						cargoOwnerName : res.data.list[0].companyName,
				})
			}
		})
  }
}
</script>

<style></style>
