<template>
  <a-card size="small" :bordered="false" class="productPricingList-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="productPricingList-code" v-model.trim="queryParam.code" allowClear placeholder="请输入产品编码"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品名称">
              <a-input id="productPricingList-name" v-model.trim="queryParam.name" allowClear placeholder="请输入产品名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="产品品牌">
              <ProductBrand id="productPricingList-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="productPricingList-productType" @change="changeProductType" v-model="productType"></ProductType>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="原厂编码">
                <a-input id="productPricingList-origCode" v-model.trim="queryParam.origCode" allowClear placeholder="请输入原厂编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="产品状态">
                <v-select
                  code="ONLINE_FLAG2"
                  id="productPricingList-onlineFalg"
                  v-model="queryParam.onlineFalg"
                  allowClear
                  placeholder="请选择产品状态"
                ></v-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="24">
            <a-checkbox v-model="queryParam.putStockFlag"><span style="display: inline-block;">包含未入库产品</span></a-checkbox>
            <a-button style="margin-left: 10px" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productPricingList-refresh">查询</a-button>
            <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productPricingList-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>
    <!-- 价格 -->
    <div style="padding: 10px 0;text-align: right;">
      <a-checkbox v-model="isCostPrice" v-if="$hasPermissions('M_ShowAllCost')"><span style="display: inline-block;">参考成本价</span></a-checkbox>
      <!-- 特约加盟商不可见市级价 -->
      <a-checkbox v-model="isCityPrice" v-if="dealerData && dealerData.dealerLevel != 'SPECIAL'"><span style="display: inline-block;">市级价</span></a-checkbox>
      <!-- 是否展示特约价 -->
      <a-checkbox v-model="isSpecialPrice" v-if="dealerData && dealerData.dealerLevel == 'SPECIAL'"><span style="display: inline-block;">特约价</span></a-checkbox>
      <a-checkbox v-model="isTerminalPrice"><span style="display: inline-block;">终端价/自定义终端价</span></a-checkbox>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable fixPagination"
      ref="table"
      :style="{ height: tableHeight+74.5+'px' }"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ y: tableHeight }"
      :defaultLoadData="false"
      bordered>
      <!-- 车主价 -->
      <template slot="carOwners" slot-scope="text, record">
        {{ (record.carOwnersPrice ||record.carOwnersPrice==0)?toThousands(record.carOwnersPrice):'--' }}/{{ record.dealerProductPrice &&(record.dealerProductPrice.carOwnersPrice ||record.dealerProductPrice.carOwnersPrice==0)? toThousands(record.dealerProductPrice.carOwnersPrice):'--' }}
      </template>
      <!-- 终端价 -->
      <template slot="terminal" slot-scope="text, record">
        {{ (record.terminalPrice ||record.terminalPrice==0)?toThousands(record.terminalPrice):'--' }}/{{ record.dealerProductPrice &&(record.dealerProductPrice.terminalPrice ||record.dealerProductPrice.terminalPrice==0)? toThousands(record.dealerProductPrice.terminalPrice):'--' }}
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button
          size="small"
          type="link"
          class="button-primary"
          @click="handleDetail(record)"
        >销售记录</a-button>
      </template>
    </s-table>
    <!-- 销售记录 -->
    <product-salesRecord-modal ref="salseRecord" :openModal="openModal" @close="openModal = false" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import ProductType from '../../common/productType.js'
import ProductBrand from '../../common/productBrand.js'
import { getCurrentDealer } from '@/api/product'
import { dealerProductPriceList } from '@/api/dealerProduct'
import productSalesRecordModal from './productSalesRecordModal.vue'
export default {
  name: 'DealerProductPriceList',
  components: { STable, VSelect, ProductType, ProductBrand, productSalesRecordModal },
  mixins: [commonMixin],
  data () {
    return {
      tableHeight: 0,
      openModal: false,
      advanced: true, // 高级搜索 展开/关闭
      queryParam: { //  查询条件
        name: '',
        code: '',
        origCode: '',
        productBrandSn: undefined,
        productTypeSn1: '',
        productTypeSn2: '',
        productTypeSn3: '',
        onlineFalg: undefined,
        putStockFlag: true
      },
      productType: [],
      disabled: false, //  查询、重置按钮是否可操作
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        params.putStockFlag = params.putStockFlag ? 1 : 0
        return dealerProductPriceList(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
        })
      },
      isCostPrice: false,
      isCityPrice: false,
      isSpecialPrice: false,
      isTerminalPrice: false,
      dealerData: null
    }
  },
  computed: {
    columns () {
      const _this = this
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '产品编码', dataIndex: 'code', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, sorter: true },
        { title: '产品名称', dataIndex: 'name', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '原厂编码', dataIndex: 'origCode', width: '11%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌', dataIndex: 'productBrandName', width: '11%', align: 'center', customRender: function (text) { return text || '--' }, sorter: true },
        { title: '库存数量', dataIndex: 'stockQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '车主价/自定义车主价', scopedSlots: { customRender: 'carOwners' }, width: '12%', align: 'right' },
        { title: '产品状态', dataIndex: 'onlineFalgDictValue', width: '6%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '6%', align: 'center' }
      ]
      if (this.isCostPrice) {
        arr.splice(6, 0, { title: '参考成本价', dataIndex: 'offerCost', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.isCityPrice) {
        const ind = this.isCostPrice ? 7 : 6
        arr.splice(ind, 0, { title: '市级价', dataIndex: 'cityPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.isSpecialPrice) {
        let ind = 0
        if (this.isCostPrice && this.isCityPrice) {
          ind = 8
        } else if ((!this.isCostPrice && this.isCityPrice) || (this.isCostPrice && !this.isCityPrice)) {
          ind = 7
        } else if (!this.isCostPrice && !this.isCityPrice) {
          ind = 6
        }
        arr.splice(ind, 0, { title: '特约价', dataIndex: 'specialPrice', width: '6%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } })
      }
      if (this.isTerminalPrice) {
        arr.splice(arr.length - 2, 0, { title: '终端价/自定义终端价', scopedSlots: { customRender: 'terminal' }, width: '12%', align: 'right' })
      }
      return arr
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.name = ''
      this.queryParam.code = ''
      this.queryParam.origCode = ''
      this.queryParam.productBrandSn = undefined
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.queryParam.onlineFalg = undefined
      this.queryParam.putStockFlag = true
      this.productType = []
      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] : ''
    },
    // 销售记录
    handleDetail (row) {
      this.openModal = true
      this.$refs.salseRecord.getDetail(row)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      // 获取当前登录用户经销商等级
      getCurrentDealer().then(res => {
        if (res.status == 200) {
          this.dealerData = res.data
        } else {
          this.dealerData = null
        }
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 230
    }
  },
  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>
