<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <a-page-header :ghost="false" :backIcon="false" class="salesDetail-cont">
        <template slot="subTitle">
          <a id="salesEdit-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span style="margin: 0 15px;color: #666;font-weight: bold;">出库单号：{{ detailData&&detailData.salesBillNo }}</span>
          <span style="margin: 0 10px;color: #666;">客户名称：{{ detailData&&detailData.buyerName }}</span>
          <a-button size="small" type="link" class="button-default" @click="showDetail=!showDetail">
            <a-icon :type="showDetail ? 'eye-invisible' : 'eye'"/> {{ showDetail?'隐藏':'查看' }}信息
          </a-button>
        </template>
      </a-page-header>
    </a-card>
    <!-- 基础信息 -->
    <div ref="detailInfo" style="margin-bottom: 6px;" v-if="showDetail">
    <a-card size="small" :bordered="false" class="pages-wrap" >
      <div style="padding: 0;">
        <a-descriptions size="small" :column="3">
          <a-descriptions-item label="客户名称">{{ detailData&&detailData.buyerName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="销售单号">{{ detailData&&detailData.buyerName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="备货单号">{{ detailData&&detailData.buyerName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="收货客户名称">{{ detailData&&detailData.salesBillExtEntity&&detailData.salesBillExtEntity.consigneeName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="发货编号">{{ detailData&&detailData.salesBillExtEntity&&detailData.salesBillExtEntity.consigneeTel || '--' }}</a-descriptions-item>
          <a-descriptions-item label="收款方式">{{ detailData&&detailData.settleStyleSnDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="收货人">{{ detailData&&detailData.auditDate || '--' }}</a-descriptions-item>
          <a-descriptions-item label="收货电话">{{ detailData&&detailData.billStatusDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="收货地址">{{ shippingAddress||'--' }}</a-descriptions-item>
          <a-descriptions-item label="业务状态">{{ detailData&&detailData.financialStatusDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="备注" :span="3">{{ detailData&&detailData.remarks || '--' }}</a-descriptions-item>
        </a-descriptions>
      </div>
    </a-card>
    </div>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 搜索条件 -->
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-item label="产品编码">
                  <a-input v-model.trim="queryParam.outBizSubNo" allowClear placeholder="请输入产品编码"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="唯一码">
                  <a-input v-model.trim="queryParam.demanderName" allowClear placeholder="请输入唯一码"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="产品名称">
                  <a-input v-model.trim="queryParam.demanderName" allowClear placeholder="请输入产品名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" :disabled="disabled" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px" :disabled="disabled" @click="resetSearchForm()">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
          <div style="margin-bottom:10px;">
            <a-button
              type="primary"
              id="outboundOrder-export"
              :loading="loading"
              @click="openGuideModal = true">导入</a-button>
            <span style="margin-left:6px;color:#f30;">每次都需全部导完，请在导入列表中修改后重新全部导入。</span>
          </div>
        </div>
        <a-alert type="info" style="margin-bottom: 10px;" v-if="detailData!=null">
          <div slot="message">
            <span v-if="$route.params.type!='stockOut'">
              轮胎款数：<strong>{{ detailData&&(detailData.totalSalesQty || detailData.totalSalesQty==0) ? detailData.totalSalesQty : '--' }}</strong>；
            </span>
            轮胎总数量：<strong>{{ detailData&&(detailData.totalQty || detailData.totalQty==0) ? detailData.totalQty : '--' }}</strong>；
          </div>
        </a-alert>
        <!-- 表格数据 -->
        <s-table
          class="sTable"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :scroll="{ y: tableHeight }"
          bordered>
        </s-table>
      </a-spin>
    </a-card>
    <!-- 导入轮胎唯一码 -->
    <importGuideModal :openModal="openGuideModal" @close="openGuideModal=false" @ok="handleGuideOk" />
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { stockOutList } from '@/api/stockOut'
import importGuideModal from './importGuideModal.vue'
export default {
  name: 'UniqueCodeList',
  mixins: [commonMixin],
  components: { STable, importGuideModal },
  data () {
    return {
      disabled: false,
      spinning: false,
      handlePlData: [],
      queryParam: {
        outBizSubNo: '',
        demanderName: '',
        receiverSn: undefined,
        warehouseSn: undefined,
        outBizTypeList: ['SALES', 'ALLOCATE']
      },
      orginData: [],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return stockOutList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          this.orginData = data.list
          return data
        })
      },
      snList: [],
      openGuideModal: false,
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'stockOutNo', width: '25%', align: 'center' },
        { title: '唯一码', dataIndex: 'outBizSubNo', width: '25%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'demanderName', width: '50%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      detailData: null,
      tableHeight: 0,
      isHasId: true,
      showDetail:false
    }
  },
  watch: {
    showDetail (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  methods: {
    //  返回
    handleBack () {
      this.$router.push({ name: 'outboundOrder' })
    },
    handleGuideOk () {

    },
    custChange (val) {
      this.queryParam.receiverSn = val.key || ''
    },
    // 删除
    handleDel (row) {
      const i = this.chooseData.findIndex(item => row.stockOutSn == item.stockOutSn)
      this.chooseData.splice(i, 1)
      const oi = this.orginData.findIndex(item => row.stockOutSn == item.stockOutSn)
      if (oi >= 0) {
        this.orginData[oi].checked = false
      }
    },
    // 选择
    handleChoose (row) {
      row.checked = true
      this.chooseData.push(row)
      this.$emit('choose')
    },
    // 取消
    handleCommonCancel () {
      this.$emit('cancel')
    },
    // 重置
    resetSearchForm () {
      this.queryParam = {
        outBizSubNo: '',
        demanderName: '',
        receiverSn: undefined,
        warehouseSn: undefined,
        outBizTypeList: ['SALES', 'ALLOCATE']
      }
      this.orginData = []
      if (this.$refs.table) {
        this.$refs.table.refresh(true)
      }
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      const detailInfoH = this.showDetail? this.$refs.detailInfo.offsetHeight : 0
      this.tableHeight = window.innerHeight - tableSearchH  - detailInfoH -255
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">

</style>
