<template>
    <div>
        <div class="mrg-b">
            <el-row>
                <el-col :span="21">
                <el-button v-auth="50300030101" type="primary" @click="handleOpenAddForm">新增</el-button>
                <el-button v-auth="50300030101" type="primary" @click="handleDeleteRows">删除</el-button>
                <el-dropdown class="mrg-l" @command="toMore">
                    <el-button v-auth="50300030101" type="primary">
                        更多<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50300030101" command="copy">复制</el-dropdown-item>
                        <el-dropdown-item v-auth="50300030101" command="share">共享商品</el-dropdown-item>
                        <el-dropdown-item v-auth="50300030101" command="select">选择商品</el-dropdown-item>
                        <el-dropdown-item v-auth="50300030101" command="batch">批量修改</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-dropdown class="mrg-l" @command="toPrint">
                    <el-button v-auth="50300030101" type="primary">
                        打印<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50300030101" command="18010080102">打印库位编码标签</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-button class="mrg-l" v-auth="50300030101" type="primary" @click="toImport">导入</el-button>
                </el-col>
                <el-col :span="3">
                <el-col :span="24" class="mrg-l">
                    <el-button v-auth="50300030101" type="primary" @click="handleMoreSearch">更多</el-button>
                    <el-button v-auth="50400040101" 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="ownerCode">
              <PublicQuery
              title="货主编码"
              baseUrl="/bd_web/bdweb"
              cfgKey="bdWmsCustomer"
              :valueField.sync="query.ownerCode" @change="handleSearch">
              </PublicQuery>
            </el-form-item>
            <el-form-item label="商品编码" prop="skuCode">
              <el-input v-model="query.skuCode" @keyup.enter.native="handleSearch" placeholder="快捷查询(商品编码)"></el-input>
            </el-form-item>
            <el-form-item label="商品名称" prop="skuName">
              <el-input v-model="query.skuName" @keyup.enter.native="handleSearch" ></el-input>
            </el-form-item>
          </el-form>
        </div>
        <more-query
        :moreQueryVisible="moreQueryVisible"
        @close="moreQueryVisible=false"
        @toGetList="getQueryList">
        </more-query>

        <share-form
        :shareFormVisible="shareFormVisible"
        :tableData="shareList"
        :skuIds="skuIds"
        @close="shareFormVisible=false"
        @handleShareConfirm="handleShareConfirm">
        </share-form>

        <batch-form
        :batchFormVisible="batchFormVisible"
        :rows="rows"
        @close="batchFormVisible=false"
        @handleBatchConfirm="handleBatchConfirm">
        </batch-form>
        <select-form
        :selectForm="selectForm"
        :selectFormVisible="selectFormVisible"
        @getSelectList="getSelectList"
        @selectClose="selectClose">
        </select-form>
    </div>
</template>
<script>
import MoreQuery from './more-query'
import ShareForm from './childrenForm/share-form'
import BatchForm from './childrenForm/batch-form'
import SelectForm from './childrenForm/select-form'
export default {
  components: {
    MoreQuery,
    ShareForm,
    BatchForm,
    SelectForm
  },
  data () {
    return {
      query: {},
      moreQueryVisible: false,
      shareFormVisible: false,
      batchFormVisible: false,
      selectFormVisible: false,
      shareList: [],
      skuIds: [],
      selectForm: [],
      moreData: {}
    }
  },
  props: {
    selectedIds: String,
    skuCodes: Array,
    rows: Array
  },
  methods: {

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

    // 新增窗口打开
    handleOpenAddForm () {
      this.$emit('handleOpenForm', 'create')
    },

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

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

    // 删除
    handleDeleteRows () {
      const url = this.$API.warehouseBasic.libraryGoods.remove
      const removeSkuIds = this.selectedIds.split(',')
      const param = { removeSkuIds: JSON.stringify(removeSkuIds) }

      if (!this.checkSelections()) return
      this.$confirm(`您已选中${removeSkuIds.length}条数据,是否删除？`, '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    getData (url, param) {
      return this.$https.post(url, param)
    },

    // 更多操作
    toMore (command) {
      const querySkuLoc = this.$API.warehouseBasic.libraryGoods.querySkuLoc
      const querySku = this.$API.warehouseBasic.libraryGoods.querySku
      let param = {}
      if (command === 'copy') {
        if (!this.rows.length) return this.$message.warning('请选择数据')
        if (this.rows.length > 1) {
          return this.$message.warning('请选择一条数据')
        } else {
          param = { ownerCode: this.rows[0].ownerCode, skuCode: this.rows[0].skuCode }
          this.$https.all([this.getData(querySku, param), this.getData(querySkuLoc, param)]).then(res => {
            let head = res[0].rows[0]
            const list = res[1].rows
            head = { ...head, ...{ skuCode: null, skuName: null, ownerName: res[0].rows[0].ebcuNameCn } }
            delete head.jobId // 复制时去除数据的唯一标识, 否则会提交失败
            this.$emit('handleOpenForm', 'copy', head, list)
          })
        }
      } else if (command === 'share') {
        if (!this.rows.length) return this.$message.warning('请选择数据')
        const queryWareHouse = this.$API.warehouseBasic.libraryGoods.queryWareHouse
        this.$https.post(queryWareHouse, param).then(res => {
          this.shareList = res.data
          this.skuIds = this.selectedIds.split(',')
          this.shareFormVisible = true
        })
      } else if (command === 'batch') {
        if (!this.rows.length) return this.$message.warning('请选择数据')
        this.batchFormVisible = true
      } else if (command === 'select') {
        this.selectFormVisible = true

        const queryWareHouse = this.$API.warehouseBasic.libraryGoods.queryWareSku
        this.$https.post(queryWareHouse).then(res => {
          this.selectForm = res.data
        })
      }
    },
    selectClose () {
      this.selectFormVisible = false
    },
    // 打印
    toPrint (command, vmNode) {
      if (this.checkSelections()) {
        const param = {
          title: vmNode.$el.innerHTML,
          qs: `_functionCode=${command}&skuCode=${this.skuCodes.join(';')}`
        }
        this.$print(param)
      }
    },
    toImport () {
      this.$importExcel('/bd_web/bdweb', 'SkuUpload')
    },

    handleShareConfirm (skuIds, projectIds) {
      const shareSku = this.$API.warehouseBasic.libraryGoods.shareSku
      const param = { skuIds: JSON.stringify(skuIds), projectIds: JSON.stringify(projectIds) }
      this.$https.post(shareSku, param).then(res => {
        this.shareFormVisible = false
        this.$getLotAttrs() // 重新获取全局批次属性
      })
    },
    handleBatchConfirm (form, rows) {
      const editBatch = this.$API.warehouseBasic.libraryGoods.editBatch
      const param = { editSkuModel: JSON.stringify(form), updateSkuModels: JSON.stringify(rows) }
      this.$https.post(editBatch, param).then(res => {
        this.batchFormVisible = false
        this.$emit('getList')
        this.$getLotAttrs() // 重新获取全局批次属性
      })
    },

    handleExcel () {
      const url = this.$API.warehouseBasic.libraryGoods.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>
