<template>
<div>
<el-row class="query">
  <div class="query__operations">
    <el-button v-auth="50300010105" type="primary" @click="toAdd">新增</el-button>
    <el-button v-auth="50300010106" type="primary" @click="e => operate(e, 'delete')" needconfirm>删除</el-button>
    <el-button v-auth="50300010107" type="primary" @click="e => operate(e, 'createTask')">生成普通盘点任务</el-button>
    <el-dropdown class="query__item" @command="toPrint">
      <el-button v-auth="50300010115" type="primary">
        打印<i class="el-icon-arrow-down el-icon--right"></i>
      </el-button>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item v-auth="50300010117" command="18010070101">打印盘点任务单</el-dropdown-item>
        <el-dropdown-item v-auth="50300010122" command="18010070102">Print Count Task List</el-dropdown-item>
        <el-dropdown-item v-auth="50300010116" command="18010070103">打印盘点差异单</el-dropdown-item>
        <el-dropdown-item v-auth="50300010121" command="18010070104">Print Count Diff List</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <el-dropdown class="query__item">
      <el-button v-auth="50300010108" type="primary">
        更多<i class="el-icon-arrow-down el-icon--right"></i>
      </el-button>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item v-auth="50300010109" command="cancelTask" needconfirm @click.native="operate">取消盘点任务</el-dropdown-item>
        <el-dropdown-item v-auth="50300010110" command="reCreateTask" @click.native="operate">生成复盘任务</el-dropdown-item>
        <el-dropdown-item v-auth="50300010111" command="createAdjustment" @click.native="operate">生成调整单</el-dropdown-item>
        <!--<el-dropdown-item v-auth="50300010112" command="createSerialCount" @click.native="operate">生成序列号盘点</el-dropdown-item>-->
        <el-dropdown-item v-auth="50300010113" command="closeCount" needconfirm @click.native="operate">关闭盘点</el-dropdown-item>
        <el-dropdown-item v-auth="50300010114" command="cancelCount" needconfirm @click.native="operate">取消盘点</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
  <div class="query__retrieve">
    <!-- <el-input
      v-model="query.countNo"
      placeholder="快捷查询（盘点单号）"
      @keyup.enter.native="toGetList(query)"
    ></el-input> -->
    <el-button v-auth="50300010102" class="query__item" type="primary" @click="moreQuery">更多</el-button>
    <el-button v-auth="50300010101" icon="el-icon-refresh" class="query__item" type="primary" @click="handelGetList(query)" plain circle></el-button>
    <el-button v-auth="50100030101" icon="el-icon-download" v-if="false" type="primary" @click="handleExcel" plain circle></el-button>
    <MoreQuery
      :queryProp="query"
      :showMore="showMore"
      @close="showMore = false"
      @toGetList="toGetList"
    ></MoreQuery>
  </div>
</el-row>
<div class="inputBox">
    <el-form inline :model="query" ref="query" label-position="left">
      <el-form-item label="盘点单号" prop="countNo">
        <el-input v-model="query.countNo" @keyup.enter.native="toGetList(query)"></el-input>
      </el-form-item>
      <el-form-item
        label="状态"
        prop="status">
        <el-select multiple v-model="status" @change="val => handleStatus(val, 'status')">
          <el-option
            v-for="item in $keys.inventory.count.status"
            :key="item.id"
            :label="item.name"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="盘点单类型"
        prop="countType">
        <el-select multiple v-model="countType" @change="val => handleStatus(val, 'countType')">
          <el-option
            v-for="item in $keys.inventory.count.countType"
            :key="item.id"
            :label="item.name"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import MoreQuery from './MoreQuery'
export default {
  components: { MoreQuery },
  props: ['selectedIds'],
  created () {
  },
  mounted () {
  },

  data () {
    return {
      query: {
        countNo: '',
      }, // 查询条件
      showMore: false, // 显示更多查询
      fileList: [],
      file: [],
      countType: [],
      status: [],
      moreData: {}
    }
  },
  methods: {
    handelGetList () {
      this.$refs.query.resetFields()
      this.status = []
      this.countType = []
      this.$emit('getList')
    },
    handleStatus (val, type) {
      if (type === 'countType') {
        this.query.countType = val.join(',')
      } else {
        this.query.status = val.join(',')
      }
      this.$emit('getList', this.query)
    },
    // 检查是否有勾选
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },
    // 新增按钮
    toAdd () {
      this.$router.push({
        name: 'countCreator',
        params: {
          mode: 'create',
        }
      })
    },
    // 对盘点任务进行操作, 更多按钮也在这里
    operateConfirm (type) {
      const url = this.$API.inventory.count[type]
      const param = {
        countNos: this.selectedIds
      }
      this.$https.post(url, param)
        .then(data => {
          console.log('操作成功', data)
          this.toGetList()
        })
    },
    // 打印
    toPrint (command, vmNode) {
      if (this.checkSelections()) {
        const param = {
          title: vmNode.$el.innerHTML,
          qs: `_functionCode=${command}&countNo=${this.selectedIds.split(',').join(';')}`
        }
        this.$print(param)
      }
    },
    // 查询
    toGetList (query) {
      this.moreData = query
      this.$emit('getList', query)
    },
    // 点击更多查询
    moreQuery () {
      this.showMore = true
    },
    handleExcel () {
      const url = this.$API.inventory.count.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;
      }
  }
  .query {
    &__operations {
      float: left;
    }
    &__retrieve {
      float: right;
      .el-input {
        max-width: 180px;
      }
    }
    &__item {
      margin-left: 10px;
    }
  }
</style>
