<template>
    <div>
        <div class="mrg-b">
            <el-row>
                <el-col :span="21">
                <el-button v-auth="50100020105" type="primary" @click="handleOpenAddForm">新增</el-button>
                <el-dropdown class="mrg-l" @command="toImport">
                    <el-button v-auth="50100020106" type="primary">
                        导入<i class="el-icon-arrow-down el-icon&#45;&#45;right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50100020107" command="Asn">导入ASN</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020108" command="Serial">导入序列号</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-button v-auth="50100020109" type="primary" class="mrg-l" @click="handleDeleteRows" >删除</el-button>
                <el-button v-auth="50100020110" type="primary" needConfirm @click="e => operate(e, 'audit')">审核</el-button>
                <!-- <el-button v-auth="50100020122" type="primary">附件管理</el-button> -->
                <el-dropdown class="mrg-l">
                    <el-button v-auth="50100020111" type="primary">
                        更多<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item v-auth="50100020112" command="duplicate" @click.native="operate">复制</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020113" needConfirm command="cancelAudit" @click.native="operate">取消审核</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020114" command="createQcByAsnNo" @click.native="operate">生成质检单</el-dropdown-item>
                        <!--质检报告上传按钮-->
                        <el-dropdown-item v-auth="50100020114" @click.native="toUploadQCReport">质检报告</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020115" needConfirm command="holdAsn" @click.native="operate">冻结订单</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020116" needConfirm command="cancelHoldAsn" @click.native="operate">取消冻结</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020117" command="createVoucherNoByAsnNo" @click.native="operate">生成凭证号</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020118" needConfirm command="cancelVoucherNoByAsnNo" @click.native="operate">取消凭证号</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020119" needConfirm command="close" @click.native="operate">关闭订单</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020120" needConfirm command="cancel" @click.native="operate">取消订单</el-dropdown-item>
                        <el-dropdown-item v-auth="50100020121" needConfirm command="generalChargeBoundAgain" @click.native="operate">重新计费</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <el-dropdown class="mrg-l" @command="toPrint">
                    <el-button v-auth="50100020124" 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="18010050101">打印入库计划单</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                </el-col>
                <el-col :span="3">
                <el-col :span="24" class="mrg-l">
                    <el-button v-auth="50100020102" type="primary" @click="handleMoreSearch">更多</el-button>
                    <el-button v-auth="50100020101" icon="el-icon-refresh" class="query__item" type="primary" @click="handleRefresh" 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="asnNo">
              <el-input v-model="query.asnNo" @keyup.enter.native="handleSearch" placeholder="快捷查询(入库单号)"></el-input>
            </el-form-item>
            <el-form-item
              label="入库状态"
              prop="status">
              <el-select multiple v-model="status" @change="handleStatus">
                <el-option label="创建" value="00"></el-option>
                <el-option label="部分收货" value="10"></el-option>
                <el-option label="完全收货" value="20"></el-option>
                <el-option label="取消订单" value="90"></el-option>
                <el-option label="关闭订单" value="99"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="订单时间">
              <el-date-picker
                v-model="date"
                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="getTime">
              </el-date-picker>
            </el-form-item>
            <el-form-item
              label="货主"
              prop="ownerCode">
              <PublicQuery
              title="货主"
              baseUrl="/inb_web/inbweb"
              cfgKey="inbWmsCustomer"
              :valueField.sync="query.ownerCode"
               @change="handleSearch">
              </PublicQuery>
            </el-form-item>
          </el-form>
        </div>
        <more-query
          ref="queryForm"
          :moreQueryVisible="moreQueryVisible"
          @close="moreQueryVisible=false"
          @toGetList="getQueryList">
        </more-query>
    </div>
</template>
<script>
import { today, yesterday, week, month, twoMonths, threeMonths } from '@/config/shortcuts'
import MoreQuery from './more-query'
import AddForm from './add-form'
export default {
  components: {
    MoreQuery,
    AddForm,
  },
  props: {
    selectedIds: String
  },
  data () {
    return {
      // 搜索查询条件
      query: {},
      moreQueryVisible: false, // 更多查询窗口显示条件
      pickerOptions2: {
        shortcuts: [today, yesterday, week, month, twoMonths, threeMonths]
      },
      date: '',
      status: [],
      moreData: {}
    }
  },
  methods: {

    // 检查是否选中行
    checkSelections () {
      if (!this.selectedIds) {
        this.$message.error('请选择记录')
        return false
      } else {
        return true
      }
    },
    getTime (val) {
      this.query.orderTimeFm = val ? val[0] : ''
      this.query.orderTimeTo = val ? val[1] : ''
      this.handleSearch()
    },
    handleStatus (val) {
      this.query.status = val.join(',')
      this.handleSearch()
    },

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

    // 删除选中行数据
    handleDeleteRows () {
      const url = this.$API.stockIn.preReceiving.delList
      const param = {
        asnNos: 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: '已取消删除'
        })
      })
    },

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

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

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

    // 导入
    toImport (command) {
      if (command === 'Asn') {
        this.$importExcel('/inb_web/inbweb', 'InboundAsnUpload')
      } else if (command === 'Serial') {
        this.$importExcel('/inb_web/inbweb', 'InboundAsnSerialUpload')
      }
    },

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

    // 对预收货单进行操作, 更多按钮也在这里
    operateConfirm (type) {
      const url = this.$API.stockIn.preReceiving[type]
      const param = {
        asnNos: this.selectedIds
      }
      this.$https.post(url, param)
        .then(data => {
          if (data.success) {
            if (data.messagesAsString === '操作成功') {
              this.handleSearch()
            } else {
              // 有报错但success为true的情况
              this.$message.closeAll()
              this.$message.error(data.messagesAsString || '服务出现问题, 不能操作 >.<')
            }
          }
        })
    },
    // 质检报告按钮
    toUploadQCReport (e) {
      if (this.checkSelections()) {
        const config = {
          orderNo: this.selectedIds,
          businessType: 'QC',
          accept: 'image/*, application/pdf',
        }
        this.$upload(config)
      }
    },
    handleExcel () {
      const url = this.$API.stockIn.preReceiving.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>
