<template>
    <div class="mrg-b">
      <el-row>
          <el-col :span="22">
          <el-button v-auth="50200100105" type="primary" @click="handleConfirm">发货确认</el-button>
          <el-button v-auth="50200100106" type="primary" @click="handleCancel">发货取消</el-button>
          <!-- <el-button v-auth="50200100321" type="primary" @click="handleEnclosure">附件管理</el-button> -->
          <el-dropdown class="mrg-l" @command="toPrint">
              <el-button v-auth="50200100107" type="primary">
                  打印<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-auth="50200100108" command="18010060124">打印发货确认单</el-dropdown-item>
                  <el-dropdown-item v-auth="50200100112" command="18010060126">Print ShipmentOrder</el-dropdown-item>
                  <el-dropdown-item v-auth="50200100114" command="18010060125">打印送货单</el-dropdown-item>
              </el-dropdown-menu>
          </el-dropdown>
          </el-col>
          <el-col :span="2">
          <!-- <el-col :span="15">
              <el-input v-model="query.quickCondition" @keyup.enter.native="handleSearch" placeholder="快捷查询(出库单号)"></el-input>
          </el-col> -->
          <el-col class="mrg-l">
              <el-button v-auth="50200100102" type="primary" @click="handleMoreSearch">更多</el-button>
               <el-button v-auth="50200100101" 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 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="状态"
              prop="status">
              <el-select multiple v-model="status" @change="val => handleStatus(val)">
                <el-option
                  v-for="item in $keys.stockOut.status"
                  :key="item.id"
                  :label="item.name"
                  :value="item.key"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="出库单类型"
              prop="soType">
              <el-select multiple v-model="soType" @change="val => handleSotype(val)">
                <el-option
                  v-for="item in $keys.stockOut.soType"
                  :key="item.id"
                  :label="item.name"
                  :value="item.key"
                ></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="handleSearch">
              </PublicQuery>
            </el-form-item>
          </el-form>
        </div>
        <more-query
            ref="queryForm"
            :moreQueryVisible="moreQueryVisible"
            @close="moreQueryVisible=false"
            @toGetList="getQueryList">
        </more-query>
    </div>
</template>
<script>
import MoreQuery from './more-query'
export default {
  components: {
    MoreQuery
  },
  data () {
    return {
      query: {},
      moreQueryVisible: false, // 更多查询窗口显示条件
      soType: [],
      status: [],
      moreData: {}
    }
  },
  props: {
    selectedIds: String
  },
  methods: {
    handleGetList () {
      this.$refs.query.resetFields()
      this.soType = []
      this.status = []
      this.$emit('getList')
    },
    getParam () {
      const query = { ...this.query, soNo: `%${this.query.soNo || ''}%` }
      return query
    },
    handleStatus (val) {
      this.query.status = val.join(',')
      this.$emit('getList', this.getParam())
    },
    handleSotype (val) {
      this.query.soType = val.join(',')
      // const query = { ...this.query, soNo: `%${this.query.soNo || ''}%` }
      this.$emit('getList', this.getParam())
    },
    // 检查是否有勾选
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },

    // 搜索查询
    handleSearch () {
      // const query = { ...this.query, soNo: `%${this.query.soNo || ''}%` }
      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(';')}`
        }
        if (command === '18010060125') {
          const USER_INFO = JSON.parse(window.localStorage.getItem('userInfo') || '{}')
          param.qs = `_functionCode=${command}&SONO=${this.selectedIds.split(',').join(';')}&ORGID=${USER_INFO.orgId}&PROJECTID=${USER_INFO.projectId}`
          param.proxyDefaultParams = true
        }
        this.$print(param)
      }
    },

    // 请求
    getData (urlkey, param, text) {
      const url = this.$API.stockOut.deliverGoods[urlkey]
      this.$confirm(`是否执行${text}操作？`, `${text}`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList', {}, this.type)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消${text}`
        })
      })
    },

    // 发货确认
    handleConfirm () {
      const urlkey = 'getShipmentBySo'
      const param = { soNos: this.selectedIds, allocateInWh: '' }
      this.getData(urlkey, param, '发货确认')
    },

    // 发货取消
    handleCancel () {
      const urlkey = 'getCancelShipmentBySo'
      const param = { soNos: this.selectedIds }
      this.getData(urlkey, param, '发货取消')
    },

    handleExcel () {
      const url = this.$API.stockOut.deliverGoods.getList
      const options = this.moreData
      this.$exportExcel(url, options)
    },

    // 附件管理
    handleEnclosure () {

    }
  }
}
</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>
