<template>
  <div>
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :scroll="{ y: 400 }"
        :data="loadData"
        :defaultLoadData="false"
        :showPagination="false"
        bordered>
      </s-table>
      <div style="padding-top: 15px;">
        <a-button id="newProduct-modal-btn" @click="viewMore" block type="primary" ghost>查看更多{{ onlineFalg==1?'新品':'下线产品' }}</a-button>
      </div>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
// 接口
import { queryNewProductPage } from '@/api/product'
export default {
  name: 'NewProductListModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    onlineFalg: { // 上下线标识  1为上线，0为下线
      type: [ String, Number ],
      default: '1'
    }
  },
  data () {
    return {
      spinning: false,
      queryParam: {}, // 查询参数
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        // 获取表格数据 无分页
        return queryNewProductPage(Object.assign(parameter, this.queryParam, { onlineFalg: this.onlineFalg })).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.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: 50, align: 'center' },
        { title: '产品品牌', dataIndex: 'productBrandName', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'code', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'name', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '单位', dataIndex: 'unit', width: 60, align: 'center', customRender: function (text) { return text || '--' } }
      ]
      if (this.onlineFalg == '1') {
        arr.push({ title: '上线时间', dataIndex: 'auditTime', width: 140, align: 'center', customRender: function (text) { return text || '--' } })
      } else if (this.onlineFalg == '0') {
        arr.push({ title: '下线时间', dataIndex: 'auditTime', width: 140, align: 'center', customRender: function (text) { return text || '--' } })
      }
      return arr
    }
  },
  methods: {
    // 查看更多
    viewMore () {
      this.$emit('seeMore')
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.$refs.table.refresh(true)
    })
  },
  watch: {
    onlineFalg (newValue, oldValue) {
      const _this = this
      setTimeout(() => {
        _this.$refs.table && _this.$refs.table.refresh(true)
      }, 400)
    }
  }
}
</script>
