<template>
    <div>
        <div class="mrg-b">
            <el-row>
                <el-col :span="21">
                <el-button v-auth="50200110105" type="primary" @click="handleOpenAddForm">新增</el-button>
                <el-button v-auth="50200110106" type="primary" @click="handleDeleteRows">删除</el-button>
                <el-button v-auth="50200110107" type="primary" @click="handleLoading">发货确认</el-button>
                <el-button v-auth="50200110108" type="primary" @click="handleDelivery">装车交接</el-button>
                <el-dropdown class="mrg-l" @command="toMore">
                    <el-button v-auth="50200110109" type="primary">
                        更多<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50200110110" command="getCancelShipmentByLdNo">取消发货</el-dropdown-item>
                        <el-dropdown-item v-auth="50200110111" command="getCancelDeliveryByLdNo">取消交接</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-dropdown class="mrg-l" @command="toPrint">
                    <el-button v-auth="50200110115" type="primary">
                        打印<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50200110116" command="18010060131">打印装车清单</el-dropdown-item>
                        <el-dropdown-item v-auth="50200110117" command="18010060132">Print LoadOrder</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <!-- <el-button class="mrg-l" v-auth="50200110118" type="primary">附件管理</el-button> -->
                </el-col>
                <el-col :span="3">
                <!-- <el-col :span="15">
                    <el-input v-model="query.ldNo" @keyup.enter.native="handleSearch" placeholder="快捷查询(装车单号)"></el-input>
                </el-col> -->
                <el-col class="mrg-l">
                    <el-button v-auth="50200110102" type="primary" @click="handleMoreSearch">更多</el-button>
                    <el-button v-auth="50200110101" 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="ldNo">
              <el-input v-model="query.ldNo" @keyup.enter.native="handleSearchs"></el-input>
            </el-form-item>
            <el-form-item
              label="出库单号"
              prop="soNo">
              <el-input v-model="query.soNo" @keyup.enter.native="handleSearchs"></el-input>
            </el-form-item>
            <el-form-item
              label="车牌号"
              prop="vehicleNo">
              <el-input v-model="query.vehicleNo" @keyup.enter.native="handleSearchs"></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>
        </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: {},
      status: [],
      moreQueryVisible: false, // 更多查询窗口显示条件
      moreData: {}
    }
  },
  props: {
    selectedIds: String
  },
  methods: {
    handleGetList () {
      this.$refs.query.resetFields()
      this.status = []
      this.$emit('getList')
    },
    getParam () {
      const query = { ...this.query, ldNo: `%${this.query.ldNo || ''}%` }
      return query
    },
    handleStatus (val) {
      this.query.status = val.join(',')
      this.$emit('getList', this.getParam())
    },
    // 检查是否选中行
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },

    handleOpenAddForm () {
      this.$emit('handleOpenAddForm', 'create')
    },

    // 删除多行
    handleDeleteRows () {
      const url = this.$API.stockOut.loadingList.delList
      const param = {
        ldNos: this.selectedIds
      }
      if (!this.checkSelections()) return
      this.$confirm(`您已选中${this.selectedIds.split(',').length}条数据,是否删除？`, '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$https.post(url, param).then(res => {
          this.$emit('getList')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 搜索查询
    handleSearchs () {
      this.$emit('getList', this.getParam())
    },
    // 搜索查询
    handleSearch () {
      this.$emit('getList', this.getParam())
    },

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

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

    getData (urlkey, text) {
      const url = this.$API.stockOut.loadingList[urlkey]
      const param = { ldNos: this.selectedIds }
      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}`
        })
      })
    },
    // 打印
    toPrint (command, vmNode) {
      if (this.checkSelections()) {
        const param = {
          title: vmNode.$el.innerHTML,
          qs: `_functionCode=${command}&ldNo=${this.selectedIds.split(',').join(';')}`
        }
        this.$print(param)
      }
    },

    // 更多操作
    toMore (command, vmNode) {
      const urlkey = command
      const text = vmNode.$el.innerText
      this.getData(urlkey, text)
    },

    // 发货确认
    handleLoading () {
      const urlkey = 'getOutboundShipmentByLdNo'
      this.getData(urlkey, '发货确认')
    },

    // 装车交接
    handleDelivery () {
      const urlkey = 'getDeliveryByLdNo'
      this.getData(urlkey, '装车交接')
    },
    handleExcel () {
      const url = this.$API.stockOut.loadingList.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>
