<template>
  <a-card size="small" :bordered="false" class="easyPassShopping-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button
          type="primary"
          class="button-info"
          v-if="shoppingMallStatus=='0'"
          icon="unlock"
          id="easyPassShopping-open-btn"
          :loading="iconLoading"
          @click="editShoppingStatus('1')">
          打开商城
        </a-button>
        <a-button
          type="primary"
          v-else
          icon="poweroff"
          id="easyPassShopping-close-btn"
          :loading="iconLoading"
          @click="editShoppingStatus('0')">
          关闭商城
        </a-button>
        <div style="margin:10px 0;color:gray;">开启商城后，易码通会显示商城</div>
      </div>
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="easyPassShopping-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="easyPassShopping-name" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称" /></a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品编码/原厂编码"><a-input id="easyPassShopping-code" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码/原厂编码" /></a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品品牌">
                <productBrand id="easyPassShopping-productBrandSn" placeholder="请选择产品品牌" v-model="queryParam.productBrandSn"></productBrand>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品分类">
                <productType id="easyPassShopping-productType" placeholder="请选择产品分类" :isDealer="true" @change="changeProductType" v-model="productType"></productType>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="商城类目">
                <a-cascader
                  id="easyPassShopping-category"
                  expandTrigger="hover"
                  :changeOnSelect="true"
                  :fieldNames="fieldNames"
                  :options="categoryOptions"
                  placeholder="请选择商城类目"
                  @change="changeCategory"
                  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="easyPassShopping-status" v-model="queryParam.status" allowClear placeholder="请选择产品商城状态"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="热销产品">
                <v-select code="FLAG" id="easyPassShopping-hotFlag" v-model="queryParam.hotFlag" allowClear placeholder="请选择热销产品"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="easyPassShopping-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="easyPassShopping-reset">重置</a-button>
            </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="productCode" slot-scope="text, record">
          <span>{{ record.productCode || '--' }}</span>
          <a-badge count="热" v-if="record.hotFlag==1" :number-style="{ zoom:'80%',marginLeft:'5px' }"></a-badge>
        </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="categoryName" slot-scope="text, record">
          <span v-if="record.categoryName1 || record.categoryName2">{{ record.categoryName1 }} {{ record.categoryName2 ? '>' : '' }} {{ record.categoryName2 }}</span>
          <span v-else>--</span>
        </template>
        <!-- 售价类型 -->
        <template slot="priceType" slot-scope="text, record">
          {{ record.priceType&&record.priceType==='OCS_PRICE'?'商城价':'自定义终端价' }}
        </template>
      </s-table>
    </a-spin>
  </a-card>
</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 { shopProductList, findMallParam, updateMallParam, queryExtList } from '@/api/shopProduct'
export default {
  name: 'ShelfProductTemplList',
  components: { STable, VSelect, productType, productBrand },
  mixins: [commonMixin],
  data () {
    const _this = this
    return {
      spinning: false,
      iconLoading: false, // 打开关闭商城按钮状态
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      //  查询条件
      queryParam: {
        productName: '', //  产品名称
        productCode: '', //  产品编码/原厂编码
        productBrandSn: undefined, //  产品品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '', //  产品三级分类
        status: undefined, //  产品商城状态
        categorySn1: undefined, // 产品一级类目
        categorySn2: undefined, // 产品二级类目
        orderBy: 'createDate', // 按照创建时间排序
        hotFlag: undefined, // 热销产品
        categoryStatus: '1'// 状态 默认值1
      },
      productType: [], // 产品分类
      categoryOptions: [], // 产品类目列表
      category: [],
      fieldNames: { label: 'categoryName', value: 'categorySn', children: 'shopCategoryList' },
      shoppingMallStatus: '0', // 商城状态1：开启   0 关闭
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'productCode', scopedSlots: { customRender: 'productCode' }, width: '12%', align: 'center' },
        { title: '产品名称', dataIndex: 'productName', width: '13%', align: 'left', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'origCode', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品品牌', dataIndex: 'productBrandName', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品分类', scopedSlots: { customRender: 'productType' }, width: '9%', align: 'center' },
        { title: '商城类目', scopedSlots: { customRender: 'categoryName' }, width: '9%', align: 'center' },
        { title: '自定义终端价', dataIndex: 'terminalPrice', width: '10%', align: 'right', customRender: function (text) { return text || text == 0 ? _this.toThousands(text, 2) : '--' } },
        { title: '商城价', dataIndex: 'ocsPrice', width: '6%', align: 'right', customRender: function (text) { return text || text == 0 ? _this.toThousands(text, 2) : '--' } },
        { title: '车主价', dataIndex: 'carOwnersPrice', width: '6%', align: 'right', customRender: function (text) { return text || text == 0 ? _this.toThousands(text, 2) : '--' } },
        { title: '售价', dataIndex: 'price', width: '6%', align: 'right', customRender: function (text) { return text || text == 0 ? _this.toThousands(text, 2) : '--' } },
        { title: '售价类型', scopedSlots: { customRender: 'priceType' }, width: '7%', align: 'center' },
        { title: '产品商城状态', dataIndex: 'statusDictValue', width: '7%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shopProductList(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
        })
      }
    }
  },
  methods: {
    //  产品分类  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] : ''
    },
    // 商城类目
    changeCategory (val, opt) {
      this.queryParam.categorySn1 = val[0] ? val[0] : ''
      this.queryParam.categorySn2 = val[1] ? val[1] : ''
    },
    // 获取产品类目列表
    getCategoryList () {
      const _this = this
      _this.spinning = true
      queryExtList({ status: 1 }).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 = undefined
      this.queryParam.productTypeSn2 = undefined
      this.queryParam.productTypeSn3 = undefined
      this.queryParam.status = undefined
      this.queryParam.categorySn1 = undefined
      this.queryParam.categorySn2 = undefined
      this.queryParam.hotFlag = undefined
      this.queryParam.orderBy = 'createDate'
      this.queryParam.categoryStatus = '1'
      this.productType = []
      this.category = []
      this.$refs.table.refresh(true)
    },
    // 获取初始商城打开、关闭状态
    getShoppingStatus () {
      const _this = this
      _this.spinning = true
      findMallParam({}).then(res => {
        if (res.status == 200) {
          this.shoppingMallStatus = res.data.paramValue || '0'
        }
        _this.spinning = false
      })
    },
    // 修改商城状态
    editShoppingStatus (typeVal) {
      const _this = this
      this.$confirm({
        title: (typeVal == '0' ? '关闭' : '开启') + '商城',
        content: '确认' + (typeVal == '0' ? '关闭' : '开启') + '吗，' + (typeVal == '0' ? '关闭' : '开启') + '后修理厂' + (typeVal == '0' ? '不可' : '可') + '在易码通查看商城',
        centered: true,
        okText: (typeVal == '0' ? '关闭商城' : '开启商城'),
        onOk () {
          _this.iconLoading = true
          updateMallParam({ paramValue: typeVal }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.shoppingMallStatus = typeVal
              _this.iconLoading = false
            } else {
              _this.iconLoading = false
            }
          })
        }
      })
    },
    // 表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 280
    },
    // 初始化页面
    pageInit () {
      const _this = this
      this.getShoppingStatus()
      this.getCategoryList()
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
