<template>
  <a-card size="small" :bordered="false" class="productChangeRecordList-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="变更时间">
              <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="productChangeRecordList-productName" 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="productChangeRecordList-productCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="品牌">
                <a-select
                  placeholder="请选择"
                  id="productChangeRecordList-productBrandSn"
                  allowClear
                  v-model="queryParam.productBrandSn"
                  :showSearch="true"
                  option-filter-prop="children"
                  :filter-option="filterOption">
                  <a-select-option v-for="item in productBrandList" :key="item.brandSn" :value="item.brandSn">{{ item.brandName }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品分类">
                <a-cascader
                  @change="changeProductType"
                  expand-trigger="hover"
                  change-on-select
                  :options="productTypeList"
                  :fieldNames="{ label: 'productTypeName', value: 'id', children: 'children' }"
                  id="productInfoList-productType"
                  placeholder="请选择产品分类"
                  allowClear
                  v-model="productType" />
              </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="productChangeRecordList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productChangeRecordList-reset">重置</a-button>
            <!-- <a-button
              style="margin: 0 0 18px 8px"
              type="primary"
              class="button-warning"
              @click="handleExport"
              :loading="exportLoading"
              id="productChangeRecordList-export">导出</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+122.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.productEntity.productTypeName2 || record.productEntity.productTypeName3">{{ record.productEntity.productTypeName2 }} {{ record.productEntity.productTypeName3 ? '>' : '' }} {{ record.productEntity.productTypeName3 }}</span>
        <span v-else>--</span>
      </template>
      <!-- 省级价变更后 -->
      <template slot="afterProvincePrice" slot-scope="text, record">
        <span v-if="record.afterProvincePrice || record.afterProvincePrice==0" :class="record.beforeProvincePrice != record.afterProvincePrice ? 'red' : ''">{{ record.afterProvincePrice }}</span>
        <span v-else>--</span>
      </template>
      <!-- 市级价变更后 -->
      <template slot="afterCityPrice" slot-scope="text, record">
        <span v-if="record.afterCityPrice || record.afterCityPrice==0" :class="record.beforeCityPrice != record.afterCityPrice ? 'red' : ''">{{ record.afterCityPrice }}</span>
        <span v-else>--</span>
      </template>
      <!-- 特约价变更后 -->
      <template slot="afterSpecialPrice" slot-scope="text, record">
        <span v-if="record.afterSpecialPrice || record.afterSpecialPrice==0" :class="record.beforeSpecialPrice != record.afterSpecialPrice ? 'red' : ''">{{ record.afterSpecialPrice }}</span>
        <span v-else>--</span>
      </template>
      <!-- 终端价变更后 -->
      <template slot="afterTerminalPrice" slot-scope="text, record">
        <span v-if="record.afterTerminalPrice || record.afterTerminalPrice==0" :class="record.beforeTerminalPrice != record.afterTerminalPrice ? 'red' : ''">{{ record.afterTerminalPrice }}</span>
        <span v-else>--</span>
      </template>
      <!-- 车主价变更后 -->
      <template slot="afterCarOwnersPrice" slot-scope="text, record">
        <span v-if="record.afterCarOwnersPrice || record.afterCarOwnersPrice==0" :class="record.beforeCarOwnersPrice != record.afterCarOwnersPrice ? 'red' : ''">{{ record.afterCarOwnersPrice }}</span>
        <span v-else>--</span>
      </template>
    </s-table>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import getDate from '@/libs/getDate.js'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { productBrandQuery } from '@/api/productBrand'
import { productTypeQuery } from '@/api/productType'
import { productPriceChangeList, getCurrentDealer } from '@/api/product'
// import { downloadExcel } from '@/libs/JGPrint.js'
export default {
  name: 'ProductPriceChangeList',
  components: { STable, VSelect, rangeDate },
  mixins: [commonMixin],
  data () {
    return {
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        beginDate: getDate.getThreeMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        productName: '', //  产品名称
        productCode: '', //  产品编码
        productBrandSn: undefined, //  品牌
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      },
      productType: [],
      disabled: false, //  查询、重置按钮是否可操作
      time: [
        moment(getDate.getThreeMonthDays().starttime, 'YYYY-MM-DD'),
        moment(getDate.getCurrMonthDays().endtime, 'YYYY-MM-DD')
      ], //  变更时间
      exportLoading: false, // 导出loading
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return productPriceChangeList(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.total = data.count || 0
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      columns: [],
      total: 0,
      productBrandList: [], //  品牌下拉数据
      productTypeList: [] //  分类下拉数据
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 设置表头
    getColumns () {
      this.columns = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '变更时间', dataIndex: 'updateDate', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productEntity.name', width: '11%', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productEntity.code', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌', dataIndex: 'productEntity.productBrandName', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品分类', scopedSlots: { customRender: 'productType' }, width: '8%', align: 'center' },
        { title: '单位', dataIndex: 'productEntity.unit', width: '4%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      const cArr = [
        {
          title: '终端价',
          dataIndex: 'hterminaldsdPrice',
          align: 'center',
          children: [
            { title: '变更前', dataIndex: 'beforeTerminalPrice', width: '4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
            { title: '变更后', scopedSlots: { customRender: 'afterTerminalPrice' }, width: '4%', align: 'center' }
          ]
        },
        {
          title: '车主价',
          dataIndex: 'sterminaldsdPrice',
          align: 'center',
          children: [
            { title: '变更前', dataIndex: 'beforeCarOwnersPrice', width: '4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
            { title: '变更后', scopedSlots: { customRender: 'afterCarOwnersPrice' }, width: '4%', align: 'center' }
          ]
        },
        { title: '变更原因', dataIndex: 'changeReason', width: '7%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ]
      getCurrentDealer().then(res => {
        if (res.status == 200) {
          if (res.data.dealerLevel && res.data.dealerLevel == 'PROVINCE') { //  省级
            this.columns.push({
              title: '省级价',
              dataIndex: 'sdtermindfaldsdPrice',
              align: 'center',
              children: [
                { title: '变更前', dataIndex: 'beforeProvincePrice', width: '4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
                { title: '变更后', scopedSlots: { customRender: 'afterProvincePrice' }, width: '4%', align: 'center' }
              ]
            },
            {
              title: '市级价',
              dataIndex: 'sdterminaldsdPrice',
              align: 'center',
              children: [
                { title: '变更前', dataIndex: 'beforeCityPrice', width: '4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
                { title: '变更后', scopedSlots: { customRender: 'afterCityPrice' }, width: '4%', align: 'center' }
              ]
            })
          } else if (res.data.dealerLevel && res.data.dealerLevel == 'CITY') { //  市级
            this.columns.push({
              title: '市级价',
              dataIndex: 'sdterminaldsdPrice',
              align: 'center',
              children: [
                { title: '变更前', dataIndex: 'beforeCityPrice', width: '4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
                { title: '变更后', scopedSlots: { customRender: 'afterCityPrice' }, width: '4%', align: 'center' }
              ]
            })
          }
          if (res.data.isShowSpecialPrice && res.data.isShowSpecialPrice == '1') { //  是否展示特约价
            this.columns.push({
              title: '特约价',
              dataIndex: 'gterminaldsdPrice',
              align: 'center',
              children: [
                { title: '变更前', dataIndex: 'beforeSpecialPrice', width: '4%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
                { title: '变更后', scopedSlots: { customRender: 'afterSpecialPrice' }, width: '4%', align: 'center' }
              ]
            })
          }
          this.columns = [...this.columns, ...cArr]
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = getDate.getThreeMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.productCode = ''
      this.queryParam.productName = ''
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.productType = []
      this.$refs.table.refresh(true)
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    //  产品分类  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] : ''
    },
    //  导出
    handleExport () {
      const params = this.queryParam
      this.exportLoading = true
      // customerBundleExportDelay(params).then(res => {
      //   this.exportLoading = false
      // downloadExcel(res, '价格变更记录')
      // })
    },
    //  产品品牌  列表
    getProductBrand () {
      productBrandQuery({}).then(res => {
        if (res.status == 200) {
          this.productBrandList = res.data
        } else {
          this.productBrandList = []
        }
      })
    },
    //  产品分类  列表
    getProductType () {
      productTypeQuery({}).then(res => {
        if (res.status == 200) {
          this.productTypeList = res.data
        } else {
          this.productTypeList = []
        }
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.getColumns()
      this.getProductBrand()
      this.getProductType()
    },
    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()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less" scoped>
  .productChangeRecordList-wrap{
    .red{
      color: #ed1c24;
    }
  }
</style>
