<template>
    <div>
        <div class="mrg-b">
            <el-row>
                <el-col :span="21">
                <el-button v-auth="50400030126" type="primary" @click="handleAdd">新增</el-button>
                <el-button v-auth="50400030108" type="primary" @click="handleDeleteRows">删除</el-button>
                <el-dropdown class="mrg-l" @command="toMore">
                    <el-button v-auth="50400030109" type="primary">
                        更多<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50400030127" command="copy">复制</el-dropdown-item>
                        <el-dropdown-item v-auth="50400030129" command="batch">批量修改</el-dropdown-item>
                        <!-- <el-dropdown-item v-auth="50400030130" command="confirmation">生成库位</el-dropdown-item> -->
                    </el-dropdown-menu>
                </el-dropdown>
                <el-dropdown class="mrg-l" @command="toPrint">
                    <el-button v-auth="50400030131" type="primary">
                        打印<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50400030125" command="18010080101">打印库位编码标签</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-button class="mrg-l" v-auth="50400030132" type="primary" @click="toImport">导入</el-button>
                </el-col>
                <el-col :span="3">
                <el-col :span="24" class="mrg-l">
                    <el-button v-auth="50400030102" type="primary" @click="handleMoreSearch">更多</el-button>
                    <el-button v-auth="50400030101" icon="el-icon-refresh" class="query__item" type="primary" @click="handleRefresh" plain circle></el-button>
                    <el-button
                    icon="el-icon-download"
                    v-if="false"
                    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="locCode">
              <el-input v-model="query.locCode" @keyup.enter.native="handleSearch" placeholder="快捷查询(库位编码)"></el-input>
            </el-form-item>
            <el-form-item label="库位名称" prop="def1">
              <el-input v-model="query.def1" @keyup.enter.native="handleSearch" ></el-input>
            </el-form-item>
            <el-form-item
              label="使用类型"
              prop="locUseType">
              <el-select multiple v-model="locUseType"  @change="handleStatus">
                <el-option label="箱件合并拣货库位" value="PC"></el-option>
                <el-option label="过渡车位" value="ST"></el-option>
                <el-option label="理货站" value="SS"></el-option>
                <el-option label="品质库位" value="QC"></el-option>
                <el-option label="加工台" value="KT"></el-option>
                <el-option label="越库库位" value="CD"></el-option>
                <el-option label="存储库位" value="RS"></el-option>
                <el-option label="箱拣货库位" value="CS"></el-option>
                <el-option label="件拣货库位" value="EA"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="所属库区"
              prop="zoneCode">
              <PublicQuery
              title="所属库区"
              baseUrl="/bd_web/bdweb"
              cfgKey="bdWmsZone"
              :valueField.sync="query.zoneCode" @change="handleSearch">
              </PublicQuery>
            </el-form-item>
          </el-form>
        </div>
        <more-query
        :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,
      locUseType: [],
      moreData: {}
    }
  },
  props: {
    rows: Array,
    selectIds: Array
  },
  methods: {

    // 检查是否选中行
    checkSelections () {
      if (!this.rows.length) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },

    // 新增
    handleAdd () {
      this.$emit('handleOpenForm', 'create')
    },

    handleStatus (val) {
      this.query.locUseType = val.join(',')
      this.handleSearch()
    },

    handleRefresh () {
      this.locUseType = []
      this.$refs.query.resetFields()
      this.$emit('getList')
    },
    // 搜索
    handleSearch () {
      const query = { ...this.query, locCode: `%${this.query.locCode || ''}%` }
      this.$emit('getList', query)
    },

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

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

    // 更多操作
    toMore (command) {
      if (!this.rows.length) return this.$message.warning('请选择数据')
      if (command === 'copy') {
        if (this.rows.length > 1) return this.$message.warning('请选择一条数据')
        const data = { ...this.rows[0], ...{ locCode: null } }
        this.$emit('handleOpenForm', 'copy', data)
      } else if (command === 'batch') {
        this.$emit('handleOpenForm', 'batch', this.rows)
      }
    },

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

    toImport () {
      this.$importExcel('/bd_web/bdweb', 'ZoneLocUpload')
    },

    // 删除
    handleDeleteRows () {
      const url = this.$API.warehouseBasic.libraryPlace.delList
      const param = { locs: this.rows }
      const config = {
        headers: { 'Content-Type': 'application/json' },
        transformRequest: [],
      }
      if (!this.checkSelections()) return
      this.$confirm(`您已选中${this.rows.length}条数据,是否删除？`, '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param, config).then(res => {
          if (res.errorMsg && res.errorMsg !== '操作成功') {
            this.$message.error(res.errorMsg)
          }
          this.$emit('getList')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleExcel () {
      const url = this.$API.warehouseBasic.libraryPlace.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>
