<template>
    <div>
        <div class="mrg-b">
            <el-row>
                <el-col :span="22">
                <el-button v-auth="50200070105" @click="handlePicking" type="primary">拣货确认</el-button>
                <el-button v-auth="50200070106" @click="handlePickingCancel" type="primary">拣货取消</el-button>
                <!-- <el-button v-auth="50200070115" type="primary">附件管理</el-button> -->
                <el-dropdown class="mrg-l" @command="toPrint">
                    <el-button v-auth="50200070112" type="primary">
                        打印<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50200070113" command="18010060102">打印拣货单(拣货顺序)</el-dropdown-item>
                        <el-dropdown-item v-auth="50200070114" command="18010060103">打印拣货单(商品顺序)</el-dropdown-item>
                        <!--<el-dropdown-item v-auth="50300030101" command="shipping">打印按重量拣货单</el-dropdown-item>-->
                    </el-dropdown-menu>
                </el-dropdown>
                </el-col>
                <el-col :span="2">
                  <el-col class="mrg-l">
                      <el-button v-auth="50200070102" type="primary" @click="handleMoreSearch">更多</el-button>
                      <el-button v-auth="50200070101" icon="el-icon-refresh" class="query__item" type="primary" @click="handleGetList" plain circle></el-button>
                      <el-button
                      v-if="false"
                    icon="el-icon-download"
                    type="primary"
                    @click="handleExcel"
                    plain
                    circle
                  ></el-button>
                  </el-col>
                </el-col>
            </el-row>
        </div>
         <div class="inputBox">
          <el-form inline :model="query" ref="query" label-position="left">
            <el-form-item label="出库单号" prop="soNo">
              <el-input v-model="query.soNo"  @keyup.enter.native="handleSearch" placeholder="快捷查询(出库单号)"></el-input>
            </el-form-item>
            <el-form-item
              label="拣货任务ID"
              prop="allocId">
              <el-input v-model="query.allocId" @keyup.enter.native="handleAllocId"></el-input>
            </el-form-item>
            <el-form-item
              label="状态"
              prop="status">
              <el-select multiple v-model="status" @change="val => handleStatus(val)">
                <el-option label="完全分配" value="40"></el-option>
                <el-option label="完全拣货" value="60"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="货主"
              prop="ownerCode">
              <PublicQuery
              title="货主编码"
              baseUrl="/oub_web/oubweb"
              cfgKey="oubWmsCustomer"
              :valueField.sync="query.ownerCode"
              @change="handleAllocId">
              </PublicQuery>
            </el-form-item>
          </el-form>
        </div>

        <more-query
        :moreQueryVisible="moreQueryVisible"
        @close="moreQueryVisible=false"
        @toGetList="getQueryList">
        </more-query>

        <picking-cancel
        :pickingVisible="pickingVisible"
        @close="pickingVisible=false"
        @pickingCancel="pickingCancel">
        </picking-cancel>
    </div>
</template>
<script>
import MoreQuery from './more-query'
import PickingCancel from './picking-cancel'
export default {
  components: {
    MoreQuery,
    PickingCancel
  },
  data () {
    return {
      query: {},
      moreQueryVisible: false, // 更多查询窗口显示条件
      pickingVisible: false,
      status: [],
      moreData: {}
    }
  },
  props: {
    selectedIds: String,
    rows: Array
  },
  methods: {
    handleGetList () {
      this.$refs.query.resetFields()
      this.status = []
      this.$emit('getList')
    },
    getParam () {
      const query = { ...this.query, soNo: `%${this.query.soNo || ''}%`, allocId: `%${this.query.allocId || ''}%` }
      return query
    },
    handleStatus (val) {
      this.query.status = val.join(',')
      this.$emit('getList', this.getParam())
    },
    handleAllocId () {
      this.$emit('getList', this.getParam())
    },
    // 检查是否有勾选的id
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },

    // 搜索查询
    handleSearch () {
      this.$emit('getList', this.getParam())
    },

    // 更多查询数据
    getQueryList (query) {
      this.moreQueryVisible = false
      this.moreData = query
      this.$emit('getList', query)
    },

    // 更多搜索查询
    handleMoreSearch () {
      this.moreQueryVisible = true
    },

    // 打印
    toPrint (command, vmNode) {
      if (this.checkSelections()) {
        const param = {
          title: vmNode.$el.innerHTML,
          qs: `_functionCode=${command}&SONO=${this.selectedIds.split(',').join(';')}`
        }
        this.$print(param)
      }
    },

    // 拣货确认
    handlePicking () {
      const url = this.$API.stockOut.picking.getPicking
      const param = { taskPkEntitys: JSON.stringify(this.rows) }
      const result = this.rows.every(item => {
        return item.formatCode !== 'NON_STANDARD_WEIGHT'
      })
      if (!result) {
        this.$message({
          type: 'info',
          message: `不能拣货确认`
        })
        return false
      }
      this.$confirm(`是否执行拣货确认操作？`, `拣货确认`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList', {}, this.type)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消拣货确认`
        })
      })
    },

    // 拣货取消
    handlePickingCancel () {
      if (this.checkSelections()) {
        this.pickingVisible = true
      }
    },
    // 拣货取消提交
    pickingCancel (form) {
      const url = this.$API.stockOut.picking.getPickingCancel
      const taskPkEntitys = this.rows.map(item => {
        return { ...item, ...form }
      })
      const param = { taskPkEntitys: JSON.stringify(taskPkEntitys) }
      this.$https.post(url, param).then(res => {
        this.$emit('getList', {}, this.type)
        this.pickingVisible = false
      })
    },
    handleExcel () {
      const url = this.$API.stockOut.picking.getList
      const options = this.moreData
      this.$exportExcel(url, options)
    },
  }
}
</script>
<style lang="scss" scoped>
.inputBox {
      width: 100%;
      border: 1px solid #d1d5db;
      margin-top: 10px;
      margin-bottom: 10px;
      border-radius: 10px;
      padding-bottom: 10px;
      padding-left: 10px;
      .el-form-item {
        margin-bottom: 0px;
        margin-top: 10px;
      }
    }
    .mrg-b{
        margin-bottom: 20px;
    }
    .mrg-l{
        margin-left:10px;
    }
  .query {
    &__operations {
      float: left;
    }
    &__retrieve {
      float: right;
      .el-input {
        max-width: 180px;
      }
    }
    &__item {
      margin-left: 10px;
    }
  }
</style>
