<template>
  <div>
    <div class="mrg-b">
      <el-row>
        <el-col :span="21">
          <el-dropdown class="mrg-l" @command="toPrint">
            <el-button v-auth="50100030105" type="primary">
              打印
              <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-auth="50100030106" command="18010050104">打印收货单</el-dropdown-item>
              <el-dropdown-item v-auth="50100030108" command="18010050105">Print Reciving List</el-dropdown-item>
              <el-dropdown-item v-auth="50100030110" command="190117001">打印仓单</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-button v-auth="50300030101" type="primary" class="mrg-l" @click="confirmRcv">收货确认</el-button>
          <el-button v-auth="50300030101" type="primary" @click="toExport">收货导出</el-button>
          <el-button 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="50100030102" type="primary" @click="handleMoreSearch">更多</el-button>
            <el-button
              v-auth="50100030101"
              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
      :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'
export default {
  components: {
    MoreQuery,
  },
  data () {
    return {
      query: {},
      moreQueryVisible: false, // 更多查询窗口显示条件
      pickerOptions2: {
        shortcuts: [today, yesterday, week, month, twoMonths, threeMonths]
      },
      date: '',
      status: [],
      moreData: {}
    }
  },
  props: {
    selectedIds: String,
    rows: Array
  },
  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)
    },
    // 更多查询数据
    getQueryList (query) {
      this.moreQueryVisible = false
      this.moreData = query
      this.$emit('getList', query)
    },

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

    // 打印
    toPrint (command, vmNode) {
      let param = {}
      if (!this.checkSelections()) return
      if (command === '190117001') {
        // if (this.rows.length > 1) return this.$message.warning('请选择一条数据')
        const asnNo = this.rows.map(item => {
          return item.asnNo
        })
        if (this.rows[0].status !== '20') return this.$message.warning('不是完全收货状态，不能操作')
        param = {
          title: vmNode.$el.innerHTML,
          qs: `_functionCode=${command}&asnNo=${asnNo.join(',')}`
        }
      } else {
        param = {
          title: vmNode.$el.innerHTML,
          qs: `_functionCode=${command}&asnNo=${this.selectedIds.split(',').join(';')}`
        }
      }
      this.$print(param)
    },
    // 收货确认
    confirmRcv () {
      if (this.checkSelections()) {
        this.$confirm(`确认要收货吗 ?`)
          .then(() => {
            const url = this.$API.stockIn.collectGoods.receiveByasnNo + '?params=' + this.selectedIds
            this.$https.get(url)
              .then(data => {
                if (data.success) {
                  if (data.messagesAsString === '操作成功') {
                    this.handleSearch()
                  } else {
                    // 有报错但success为true的情况
                    this.$message.closeAll()
                    // this.$message.error('服务出现问题, 不能操作 >.<')
                    this.$message({
                      type: 'error',
                      dangerouslyUseHTMLString: true,
                      message: `${data.messagesAsString.replace(/\n/g, '<br/><br/>')}` // 换行问题
                    })
                  }
                }
              })
          })
      }
    },
    // 收货导出
    toExport () {
      if (this.checkSelections()) {
        // todo: 判断状态(完全收货时不能导出), 现在拿的id没有状态信息, 后续优化
        window.location.href = '/inb_web/inbweb/fileExportController/exprotTask.shtml?params=' + this.selectedIds
      }
    },

    handleExcel () {
      const url = this.$API.stockIn.collectGoods.getList
      const options = { ...{ auditStatus: '99,90', holdStatus: '00', status: '00,10,20,99' }, ...this.moreData }
      this.$exportExcel(url, options)
    },
    toImport () {
      this.$importExcel('/inb_web/inbweb', 'InboundAsnUploadYG')
    }
  }
}
</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>
