<template>
  <div class="rebateBinding-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" @back="handleBack" class="rebateBinding-cont">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="rebateBinding-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
          <span style="margin-left: 20px;color: #666;" v-if="detail">经销商：{{ detail.dealerName }}</span>
        </template>
      </a-page-header>
      <a-card size="small" :bordered="false" class="rebateBinding-main">
        <!-- 搜索条件 -->
        <div ref="tableSearch" class="table-page-search-wrapper">
          <a-form-model
            id="rebateBinding-form"
            v-show="!isTeyue"
            ref="ruleForm"
            class="form-model-con"
            layout="inline"
            :model="queryParam"
            @keyup.enter.native="handleSearch">
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="品牌">
                  <ProductBrand id="rebateBinding-productBrandSn" v-model="queryParam.productBrandSn"></ProductBrand>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="产品分类">
                  <ProductType id="rebateBinding-productType" placeholder="请选择产品分类" @change="changeProductType" v-model="productType"></ProductType>
                </a-form-model-item>
              </a-col>
              <!-- <a-col :md="6" :sm="24" v-if="!isTeyue">
                <a-form-model-item label="价格级别">
                  <v-select code="PRICE_LEVEL" :notIn="['SPECIAL']" v-model="queryParam.priceLevel" allowClear placeholder="请选择价格级别"></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24" v-if="!isTeyue">
                <a-form-model-item label="经销商">
                  <a-input v-model.trim="queryParam.dealerName" allowClear placeholder="请输入经销商名称"/>
                </a-form-model-item>
              </a-col> -->
              <a-col :md="6" :sm="24" v-show="false">
                <a-form-model-item label="差价归属方">
                  <v-select
                    id="rebateBinding-rebateParentType"
                    code="REBATE_PARENT_TYPE"
                    ref="rpType"
                    v-model="queryParam.rebateParentType"
                    allowClear
                    placeholder="请选择差价归属方"></v-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
                <a-button type="primary" @click="handleSearch" :disabled="disabled" id="rebateBinding-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="rebateBinding-reset">重置</a-button>
              </a-col>
            </a-row>
          </a-form-model>
        </div>
        <!-- 操作按钮 -->
        <div class="table-operator" style="display: flex;align-items: center;justify-content: space-between;" v-if="showBtns">
          <a-button id="rebateBinding-brand" type="primary" class="button-error" @click="openGlModal">差价设置</a-button>
          <div v-if="!isTeyue && hasData">
            <a-button id="rebateBinding-isTeyue" type="default" @click="expandAllRow">{{ expandAll?'收起全部':'展开全部' }}</a-button>
          </div>
        </div>
        <!-- 列表 -->
        <categoryList ref="categoryList" @loaded="loadList" @del="delOk" @edit="handleEdit" :maxHeight="tableHeight"></categoryList>
      </a-card>
    </a-spin>
    <!-- 差价设置 -->
    <priceDiffModal ref="priceDiffModal" :openModal="openModal" @ok="resetGetData" @close="openModal=false"></pricediffmodal>
  </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 priceDiffModal from './priceDiffModal.vue'
import categoryList from './categoryList'
// 接口
import { dealerDetailBySn } from '@/api/dealer'
export default {
  name: 'RebateBinding',
  mixins: [commonMixin],
  components: { STable, VSelect, ProductType, ProductBrand, priceDiffModal, categoryList },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      loading: false, //  表格加载中
      openModal: false, // 差价设置弹窗
      detail: null, // 详情数据
      productType: [], // 产品分类查询数据
      //  查询条件
      queryParam: {
        rebateParentType: undefined, // 差价归属方
        priceLevel: undefined, // 价格级别
        productBrandSn: undefined, // 品牌
        productTypeSn1: undefined, // 产品一级分类
        productTypeSn2: undefined, // 产品二级分类
        productTypeSn3: undefined, // 产品三级分类
        dealerName: ''// 经销商名称
      },
      showBtns: true, // 是否显示操作按钮
      expandAll: false, // 显示展开全部  收起全部按钮
      hasData: false // 列表数据是否为空
    }
  },
  computed: {
    // 判断是否显示特约经销商内容
    isTeyue () {
      return this.detail && this.detail.dealerLevel == 'SPECIAL'
    }
  },
  methods: {
    // 返回
    handleBack () {
      this.$router.push({ name: 'merchantInfoManagementList', query: { closeLastOldTab: true } })
    },
    // 获取列表数据
    loadList (list) {
      this.showBtns = this.isTeyue ? list.length == 0 : true
      this.hasData = list.length > 0
      this.$nextTick(() => {
        if (!this.expandAll) {
          this.expandAllRow()
        }
      })
    },
    // 查询
    handleSearch () {
      this.queryParam.dealerSn = this.$route.params.sn
      const params = {}
      for (const k in this.queryParam) {
        if (this.queryParam[k]) {
          params[k] = this.queryParam[k]
        }
      }
      this.$refs.categoryList.pageInit(params, this.isTeyue)
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productTypeSn1 = val[0] ? val[0] : undefined
      this.queryParam.productTypeSn2 = val[1] ? val[1] : undefined
      this.queryParam.productTypeSn3 = val[2] ? val[2] : undefined
    },
    // 收起全部 展开全部 事件
    expandAllRow () {
      if (this.expandAll) {
        this.$refs.categoryList.foldAll()
      } else {
        this.$refs.categoryList.expandAll()
      }
      this.expandAll = !this.expandAll
    },
    // 差价设置成功,关闭弹窗,初始化弹窗所需数据
    resetGetData () {
      this.expandAll = false
      this.$refs.categoryList.dataSource = []
      this.resetSearchForm()
    },
    // 重置表单
    resetSearchForm () {
      this.queryParam.dealerName = ''
      this.queryParam.rebateParentType = undefined
      this.queryParam.priceLevel = undefined
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = undefined
      this.queryParam.productTypeSn2 = undefined
      this.queryParam.productTypeSn3 = undefined
      this.productType = []
      this.handleSearch()
      this.openModal = false
    },
    // 打开关联弹框
    openGlModal () {
      this.openModal = true
      const checkedDealerSn = this.$refs.categoryList.checkedDealerSn
      this.$refs.priceDiffModal.setData(this.$refs.rpType.getOptionDatas(), this.$route.params.sn, this.detail, checkedDealerSn)
    },
    // 编辑
    handleEdit (row) {
      this.openModal = true
      this.$refs.priceDiffModal.editData(this.$refs.rpType.getOptionDatas(), this.$route.params.sn, this.detail, row)
    },
    // 删除成功
    delOk () {
      this.handleSearch()
    },
    // 详情
    getDetail () {
      this.spinning = true
      dealerDetailBySn({ sn: this.$route.params.sn }).then(res => {
        if (res.status == 200) {
          this.detail = res.data
          this.handleSearch()
        }
        this.spinning = false
      })
    },
    // 初始化
    pageInit () {
      const _this = this
      this.getDetail()
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 获取表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - (!this.showBtns ? 50 : 205)
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less" scoped>
  .rebateBinding-wrap{
    .rebateBinding-cont{
      margin-bottom: 6px;
    }
  }
</style>
