<template>
  <div class="mrg-b">
    <el-row>
      <el-col :span="21">
        <el-button v-auth="50200020105" type="primary" @click="handleOpenAddForm" v-show="tabIndex==='0'">新增</el-button>
        <!-- <el-button v-auth="50200020106" type="primary" v-show="tabIndex===0">导入</el-button> -->
        <el-dropdown class="mrg-l" @command="toImport" v-show="tabIndex==='0'">
          <el-button v-auth="50200070112" 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="OutboundSoUpload">导入SO</el-dropdown-item>
            <el-dropdown-item v-auth="50300030101" command="OutboundSoUploadHS">导入SO--合顺</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button class="mrg-l" v-auth="50200020109" type="primary" @click="handleDeleteRows" v-show="tabIndex==='0'">删除</el-button>
        <el-button v-auth="50200020110" type="primary" @click="handleAudit" v-show="tabIndex==='0'">审核</el-button>
        <el-button v-auth="50200020111" type="primary" @click="handleAllot" v-show="tabIndex==='1'||tabIndex==='3'">分配</el-button>
        <el-button v-auth="50200020111" type="primary" @click="handleCopy" v-show="tabIndex==='1'">复制</el-button>
        <el-button v-auth="50200020112" type="primary" @click="handleAuditCancel" v-show="tabIndex==='1'">取消审核</el-button>
        <el-button v-auth="50200020120" type="primary" @click="handleAllotCancel" v-show="tabIndex==='1'||tabIndex==='2'||tabIndex==='3'">取消分配</el-button>
        <el-button v-auth="50200020112" type="primary" @click="handleOrderCancel" v-show="tabIndex==='0'">取消订单</el-button>
        <el-button v-auth="50200020120" type="primary" @click="handleIntercept" v-show="tabIndex==='0'||tabIndex==='1'||tabIndex==='2'||tabIndex==='3'">拦截订单</el-button>
        <el-button v-auth="50200020120" type="primary" @click="handlePickingFinish" v-show="tabIndex==='3'">完成拣货</el-button>
        <el-button v-auth="50200020120" type="primary" @click="handleOrderClose" v-show="tabIndex==='4'">关闭订单</el-button>
        <el-button v-auth="50200020120" type="primary" @click="handleGeneralCharge" v-show="tabIndex==='5'">重新计费</el-button>
        <!-- <el-button v-auth="50200020120" type="primary" v-show="tabIndex===4||tabIndex===5">附件管理</el-button> -->
        <el-button v-auth="50200020120" type="primary" @click="handleSendEdiAgain" v-show="tabIndex==='5'">重新发送EDI</el-button>
        <el-dropdown class="mrg-l" @command="toPrint" v-show="tabIndex==='2'||tabIndex==='4'||tabIndex==='5'">
          <el-button v-auth="50200070112" type="primary">
            打印<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <!--<el-dropdown-item v-auth="50200070113" v-show="tabIndex===2" command="18010060133">打印送货单</el-dropdown-item>-->
            <!--<el-dropdown-item v-auth="50200070114" v-show="tabIndex===2" command="18010060101">打印送货通知单(冷链)</el-dropdown-item>-->
            <el-dropdown-item v-auth="50300030101" v-show="tabIndex==='2'" command="18010060102">打印拣货单(拣货顺序)</el-dropdown-item>
            <el-dropdown-item v-auth="50300030101" v-show="tabIndex==='2'" command="18010060103">打印拣货单(商品顺序)</el-dropdown-item>
            <el-dropdown-item v-auth="50300030101" v-show="tabIndex==='4'||tabIndex==='5'" command="18010060124">打印发货确认单</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="3">
        <el-button v-auth="50200020102" type="primary" @click="handleMoreSearch">更多</el-button>
        <el-button v-auth="50200020101" icon="el-icon-refresh" class="query__item" type="primary" @click="handleGetList" plain circle></el-button>
        <el-button
        v-show="false"
          icon="el-icon-download"
          type="primary"
          @click="handleExcel"
          plain
          circle
        ></el-button>
      </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="订单时间">
              <el-date-picker
                v-model="date1"
                type="datetimerange"
                format="yyyy-MM-dd HH:mm"
                value-format="yyyy-MM-dd HH:mm:ss"
                :default-time="['00:00:00', '23:59:59']"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions2"
                @change="getTime1">
              </el-date-picker>
            </el-form-item>
            <el-form-item
              label="货主编码"
              prop="ownerName">
              <PublicQuery
              title="货主编码"
              baseUrl="/oub_web/oubweb"
              cfgKey="oubWmsCustomer"
              :valueField.sync="query.ownerCode"
              :textField.sync="query.ownerName"
              @change="getGoodsDate">
              </PublicQuery>
            </el-form-item>
             <el-form-item
              label="物流单号"
              prop="logisticNo">
              <el-input v-model="query.logisticNo" @keyup.enter.native="handleLogisticNo"></el-input>
            </el-form-item>
          </el-form>
        </div>
    <more-query :moreQueryVisible="moreQueryVisible" @close="moreQueryVisible=false" @toGetList="getQueryList">
    </more-query>

  </div>
</template>
<script>
import AddForm from './add-form'
import MoreQuery from './more-query'
import { time } from 'apec-fe-utils'
import { today, yesterday, week, month, twoMonths, threeMonths } from '@/config/shortcuts'
export default {
  components: {
    AddForm,
    MoreQuery
  },
  data () {
    return {
      logisticNo: '',
      soNo: '',
      date1: '',
      query: {},
      moreQueryVisible: false, // 更多查询窗口显示条件
      addFormVisible: false,
      pickerOptions2: {
        shortcuts: [today, yesterday, week, month, twoMonths, threeMonths]
      },
      moreData: {}
    }
  },
  props: {
    selectedIds: String,
    type: String,
    tabIndex: String,
    cacheQuery: Object,
    rows: Array
  },
  mounted () {
  },
  methods: {
    handleGetList () {
      this.date1 = ''
      this.$refs.query.resetFields()
      this.$emit('getList', { ...this.cacheQuery }, 'quick')
    },
    getParam () {
      const query = { ...this.query, ...this.cacheQuery, soNo: `%${this.query.soNo || ''}%` }
      return query
    },
    getTime1 (val) {
      this.query.orderTimeFm = val ? val[0] : ''
      this.query.orderTimeTo = val ? val[1] : ''
      this.$emit('getList', this.getParam(), 'quick')
    },
    handleLogisticNo () {
      this.$emit('getList', this.getParam(), 'quick')
    },
    getGoodsDate () {
      this.$emit('getList', this.getParam(), 'quick')
    },
    // 检查是否选中行
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },

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

    // 搜索查询
    handleSearch () {
      let query = {}
      // if (!this.query.quickCondition) {
      //   query = this.cacheQuery
      // } else {
      //   query = { ...{ quickCondition: `%${this.query.quickCondition || ''}%` }, ...this.cacheQuery, ...this.query }
      // }
      this.$emit('getList', this.getParam(), 'quick')
    },

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

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

    // 删除选中的行
    handleDeleteRows () {
      const url = this.$API.stockOut.sendOrder.delList
      const param = {
        soNos: 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: '已取消删除'
        })
      })
    },

    // 请求
    getData (urlkey, text) {
      let param = {}
      const url = this.$API.stockOut.sendOrder[urlkey]
      if (urlkey === 'getGeneralCharge') {
        param = { soHeaders: JSON.stringify(this.rows) }
      } else {
        param = { soNos: 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}&SONO=${this.selectedIds.split(',').join(';')}`
        }
        this.$print(param)
      }
    },

    // 分配
    handleAllot () {
      const urlkey = 'getAllot'
      const text = '分配'
      this.getData(urlkey, text)
    },

    // 取消分配
    handleAllotCancel () {
      const urlkey = 'getAllotCancel'
      const text = '取消分配'
      this.getData(urlkey, text)
    },

    // 审核
    handleAudit () {
      const urlkey = 'getAuditing'
      const text = '审核'
      this.getData(urlkey, text)
    },

    // 取消审核
    handleAuditCancel () {
      const urlkey = 'getAuditCancel'
      const text = '取消审核'
      this.getData(urlkey, text)
    },

    // 取消订单
    handleOrderCancel () {
      const urlkey = 'getOrderCancel'
      const text = '取消订单'
      this.getData(urlkey, text)
    },

    // 拦截订单
    handleIntercept () {
      const urlkey = 'getIntercept'
      const text = '拦截订单'
      this.getData(urlkey, text)
    },

    // 关闭订单
    handleOrderClose () {
      const urlkey = 'getOrderClose'
      const text = '关闭订单'
      this.getData(urlkey, text)
    },

    // 复制
    handleCopy () {
      const urlkey = 'getCopy'
      const text = '复制'
      this.getData(urlkey, text)
    },

    // 完成拣货
    handlePickingFinish () {
      const urlkey = 'getPickingFinish'
      const text = '完成拣货'
      this.getData(urlkey, text)
    },

    // 重新计费
    handleGeneralCharge () {
      const urlkey = 'getGeneralCharge'
      const text = '重新计费'
      this.getData(urlkey, text)
    },

    // 重新发送EDI
    handleSendEdiAgain () {
      const urlkey = 'getSendEdiAgain'
      const text = '重新发送EDI'
      this.getData(urlkey, text)
    },
    toImport (command) {
      this.$importExcel('/oub_web/oubweb', command)
    },
    handleExcel () {
      const url = this.$API.stockOut.sendOrder.getList
      const options = this.moreData
      this.$exportExcel(url, options)
    },
  }
}
</script>
<style lang="scss" scoped>
    .inputBox {
      width: 100%;
      border: 1px solid #d1d5db;
      margin-top: 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>
