<template>
  <a-card size="small" :bordered="false" class="productChangeRecordList-wrap jg-page-wrap">
    <!-- 搜索条件 -->
    <div ref="tableSearch" class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="产品编码">
              <a-input id="productUniversalList-productCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="productUniversalList-productName" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品状态">
              <v-select code="ONLINE_FLAG" id="productUniversalList-onlineFalg" v-model="queryParam.onlineFalg" allowClear placeholder="请选择产品状态"></v-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="通用产品编码">
                <a-input id="productUniversalList-productCommonCode" v-model.trim="queryParam.productCommonCode" allowClear placeholder="请输入通用产品编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="通用产品名称">
                <a-input id="productUniversalList-productCommonName" v-model.trim="queryParam.productCommonName" allowClear placeholder="请输入通用产品名称"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productUniversalList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productUniversalList-reset">重置</a-button>
            <a @click="advanced=!advanced" style="margin-left: 5px">
              {{ advanced ? '收起' : '展开' }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable fixPagination"
      ref="table"
      :style="{ height: tableHeight+84.5+'px' }"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ y: tableHeight }"
      :defaultLoadData="false"
      bordered>
      <!-- 产品分类 -->
      <template slot="productType" slot-scope="text, record">
        <span v-if="record.productCommon.productTypeName2 || record.productCommon.productTypeName3">{{ record.productCommon.productTypeName2 }} {{ record.productCommon.productTypeName3 ? '>' : '' }} {{ record.productCommon.productTypeName3 }}</span>
        <span v-else>--</span>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { productUniversalCodeList } from '@/api/productUniversalCode'
export default {
  name: 'ProductUniversalList',
  mixins: [commonMixin],
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        productCode: '',
        productName: '',
        onlineFalg: undefined,
        productCommonName: '',
        productCommonCode: ''
      },
      productType: [],
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出loading
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', align: 'center', width: '20%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品状态', dataIndex: 'product.onlineFalgDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '通用产品编码', dataIndex: 'productCommonCode', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '通用产品名称', dataIndex: 'productCommon.name', width: '12%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '通用产品分类', scopedSlots: { customRender: 'productType' }, width: '12%', align: 'center' },
        { title: '通用产品状态', dataIndex: 'productCommon.onlineFalgDictValue', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return productUniversalCodeList(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
          return data
        })
      },
      openModal: false, //  编辑  弹框
      itemId: '' //  当前id
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.queryParam.productName = ''
      this.queryParam.onlineFalg = undefined
      this.queryParam.productCommonName = ''
      this.queryParam.productCommonCode = ''
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  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" scoped>
  .productChangeRecordList-wrap{
    .red{
      color: #ed1c24;
    }
  }
</style>
