<template>
  <a-modal
    centered
    class="unique-code-modal"
    :footer="null"
    :maskClosable="false"
    v-model="isShow"
    :title="modalTit"
    @cancel="isShow=false"
    width="70%">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="common-main">
        <!-- 搜索条件 -->
        <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>
        <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"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: 400 }"
          :pageSize="10"
          bordered>
        </s-table>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { stockOutList } from '@/api/stockOut'
export default {
  name: 'UniqueCodeModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    modalTit: { // 弹框标题
      type: String,
      default: null
    },
    chooseData: { // 已选数据
      type: Array,
      default: function () {
        return []
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      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
              const index = this.chooseData.findIndex(item => data.list[i].stockOutSn == item.stockOutSn)
              data.list[i].checked = index >= 0
            }
            this.disabled = false
          }
          this.spinning = false
          this.orginData = data.list
          return data
        })
      },
      snList: [],
      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
    }
  },
  methods: {
    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 = []
      this.$nextTick(() => {
        this.$refs.dealerSubareaScopeList.resetForm()
      })
      if (this.$refs.table) {
        this.$refs.table.refresh(true)
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.handleCommonCancel()
      } else {
        this.resetSearchForm()
      }
    }
  }
}
</script>

<style lang="less">

</style>
