<template>
  <div>
    <a-card size="small" :bordered="false" class="promoProducts-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="promoProducts-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码/原厂编码">
                <a-input id="promoProducts-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="promoProducts-name" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="品牌">
                <productBrand id="promoProducts-productBrandSn" v-model="queryParam.productBrandSn"></productBrand>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="产品分类">
                  <productType id="promoProducts-productType" placeholder="请选择产品分类" @change="changeProductType" v-model="productType"></productType>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="促销名称">
                  <a-input id="promoProducts-promoName" v-model.trim="queryParam.promoName" allowClear placeholder="请输入促销名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="促销类型">
                  <v-select
                    v-model="queryParam.promoType"
                    ref="promotionType"
                    id="promoProducts-promoType"
                    code="SHOP_PROMO_PROMO_TYPE"
                    placeholder="请选择促销类型"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="促销状态">
                  <v-select
                    v-model="queryParam.promoState"
                    ref="promoState"
                    id="promoProducts-promoState"
                    :notIn="['HAVE_DISCARD','IS_OVER']"
                    code="SHOP_PROMO_PROMO_STATE"
                    placeholder="请选择促销状态"
                    allowClear></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="商城类目">
                  <a-cascader
                    id="promoProducts-category"
                    expandTrigger="hover"
                    :changeOnSelect="true"
                    :fieldNames="fieldNames"
                    :options="categoryOptions"
                    placeholder="请选择商城类目"
                    @change="changeProductsCategory"
                    allowClear
                    v-model="category"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="商城状态">
                  <v-select code="SHOP_PRODUCT_STATUS" id="promoProducts-status" v-model="queryParam.shopProductStatus" allowClear placeholder="请选择商城状态"></v-select>
                </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="promoProducts-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="promoProducts-reset">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px" id="merchantInfoManagementList-advanced">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :pageSize="20"
          :scroll="{ y: tableHeight - 20 }"
          :defaultLoadData="false"
          bordered>
          <!-- 产品编码 -->
          <template slot="productCode" slot-scope="text, record">
            {{ record.productCode }}
          </template>
          <!-- 产品分类 -->
          <template slot="productType" slot-scope="text, record">
            <span v-if="record.productTypeName2 || record.productTypeName3">{{ record.productTypeName2 }} {{ record.productTypeName3 ? '>' : '' }} {{ record.productTypeName3 }}</span>
            <span v-else>--</span>
          </template>
          <!-- 促销时间 -->
          <template slot="promoTime" slot-scope="text, record">
            <span v-if="record.promoStartDate">{{ record.promoStartDate }} 至 {{ record.promoEndDate }}</span>
            <span v-else>--</span>
          </template>
          <!-- 活动规则 -->
          <template slot="promoRules" slot-scope="text, record">
            <span v-if="record.promoType==='BUY_PROD_GIVE_PROD'&&record.conditionValue&&record.resultValue">买{{ record.conditionValue }}赠{{ record.resultValue }}</span>
            <div v-else-if="record.promoType==='PROMO_PROD'&&(record.shopProductPrice||record.shopProductPrice ==0)&&(record.conditionValue||record.conditionValue==0)">
              <span v-if="record.shopProductPrice - record.conditionValue>0">优惠{{ (record.shopProductPrice - record.conditionValue).toFixed(2) }}元</span>
              <span v-else style="color:#ED1C24;">优惠{{ (record.shopProductPrice - record.conditionValue).toFixed(2) }}元</span>
            </div>
            <span v-else-if="record.promoType==='BUY_PROD_GIVE_VALID'&&record.resultValue">返{{ record.resultValue ? record.resultValue.toFixed(2):'--' }}元</span>
            <span v-else>--</span>
          </template>
          <!-- 促销售价 -->
          <template slot="promoPrice" slot-scope="text, record">
            <span v-if="record.promoType==='BUY_PROD_GIVE_PROD'&&record.shopProductPrice">{{ record.shopProductPrice ?record.shopProductPrice.toFixed(2):'--' }}</span>
            <span v-else-if="record.promoType==='PROMO_PROD'&&record.conditionValue">{{ record.conditionValue?record.conditionValue.toFixed(2):'--' }}</span>
            <span v-else-if="record.promoType==='BUY_PROD_GIVE_VALID'&&record.shopProductPrice">{{ record.shopProductPrice?record.shopProductPrice.toFixed(2):'--' }}</span>
            <span v-else>--</span>
          </template>
          <!-- 商城类目 -->
          <template slot="categoryName" slot-scope="text, record">
            <span v-if="record.indName || record.dirName">{{ record.indName }} {{ (record.indName&&record.dirName) ? '>' : '' }} {{ record.dirName }}</span>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import productType from '@/views/common/productType.js'
import productBrand from '@/views/common/productBrand.js'
// 接口
import { shopPromoProductList } from '@/api/shopPromoProduct'
import { queryExtList } from '@/api/shopProduct'
export default {
  name: 'PromoProductsList',
  mixins: [commonMixin],
  components: { STable, VSelect, productType, productBrand },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0, // 表格高度
      // form表单label布局
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      productType: [], // 产品分类
      // 查询条件
      queryParam: {
        productName: '', // 产品名称
        productCode: '', // 产品编码/原厂编码
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '', //  产品三级分类
        promoName: '', // 促销名称
        promoType: undefined, // 促销状态
        promoState: undefined, // 促销状态
        categorySn: undefined, // 商城类目
        shopProductStatus: undefined, // 商城状态
        shopProductDelFlag: 0, // 促销产品标志
        promoStateList: ['NOT_RELEASE', 'HAVE_RELEASE']// 促销列表只查询这两种状态
      },
      categoryOptions: [], // 产品类目列表
      fieldNames: { label: 'categoryName', value: 'categorySn', children: 'shopCategoryList' },
      category: [], // 类目val
      columns: [
        { title: '序号', dataIndex: 'no', align: 'center', width: '50px' },
        { title: '产品编码', dataIndex: 'productCode', width: '10%', align: 'center', scopedSlots: { customRender: 'productCode' } },
        { title: '产品名称', dataIndex: 'productName', width: '22%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'productOrigCode', align: 'center', width: '10%', customRender: function (text) { return text || '--' } },
        { title: '品牌', dataIndex: 'productBrandName', align: 'center', width: '10%', customRender: function (text) { return text || '--' } },
        { title: '产品分类', align: 'center', width: '15%', scopedSlots: { customRender: 'productType' } },
        { title: '促销名称', dataIndex: 'promoName', width: '15%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '促销时间', dataIndex: 'promoStartDate', align: 'center', width: '16%', scopedSlots: { customRender: 'promoTime' } },
        { title: '促销类型', dataIndex: 'promoTypeDictValue', align: 'center', width: '10%', customRender: function (text) { return text || '--' } },
        { title: '活动规则', align: 'center', width: '10%', scopedSlots: { customRender: 'promoRules' } },
        { title: '促销状态', dataIndex: 'promoStateDictValue', align: 'center', width: '8%', customRender: function (text) { return text || '--' } },
        { title: '促销售价', width: '8%', align: 'right', scopedSlots: { customRender: 'promoPrice' } },
        { title: '商城类目', scopedSlots: { customRender: 'categoryName' }, width: '15%', align: 'center' },
        { title: '商城状态', dataIndex: 'shopProductStatusDictValue', align: 'center', width: '8%', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return shopPromoProductList(params).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
        })
      }
    }
  },
  methods: {
    // 获取产品类目列表
    getCategoryList () {
      const _this = this
      _this.spinning = true
      queryExtList({}).then(res => {
        if (res.status == 200) {
          this.categoryOptions = res.data || []
        }
        _this.spinning = false
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.productName = ''
      this.queryParam.productCode = ''
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.queryParam.promoName = ''
      this.queryParam.promoType = undefined
      this.queryParam.promoState = undefined
      this.queryParam.shopProductStatus = undefined
      this.queryParam.categorySn = undefined
      this.queryParam.shopProductDelFlag = 0
      this.queryParam.promoStateList = ['NOT_RELEASE', 'HAVE_RELEASE']
      this.productType = []
      this.category = []
      this.$refs.table.refresh(true)
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.productTypeSn3 = val[2] ? val[2] : ''
    },
    // 商城类目
    changeProductsCategory (val) {
      this.queryParam.categorySn = (val[0] && val[1]) ? val[1] : (val[0] && !val[1]) ? val[0] : undefined
    },
    // 初始化
    pageInit () {
      const _this = this
      _this.getCategoryList()
      _this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 200
    }
  },
  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(true)
    }
  }
}
</script>
