<template>
  <a-card size="small" :bordered="false" class="returnSlipReportList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="returnSlipReportList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          :rules="rules"
          @keyup.enter.native="handleSearch">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="退货日期" prop="time">
                <rangeDate ref="rangeDate" :value="queryParam.time" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户名称">
                <custList id="returnSlipReportList-custList" ref="custList" @change="custChange"></custList>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户级别">
                <v-select
                  v-model="queryParam.dealerLevel"
                  ref="dealerLevel"
                  id="returnSlipReportList-dealerLevel"
                  code="DEALER_LEVEL"
                  placeholder="请选择客户级别"
                  allowClear></v-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="退货单号">
                  <a-input id="returnSlipReportList-salesReturnBillNo" v-model.trim="queryParam.salesReturnBillNo" allowClear placeholder="请输入退货单号"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="所在区域">
                  <subarea id="returnSlipReportList-subareaSn" v-model="queryParam.subareaSn"></subarea>
                </a-form-model-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-row>
                  <a-form-model-item label="地区" style="margin-bottom: 0!important;">
                    <a-col span="7">
                      <a-form-model-item prop="provinceSn" style="margin: 0;">
                        <a-select v-model="queryParam.provinceSn" allowClear @change="getCityList" placeholder="请选择省">
                          <a-select-option v-for="item in addrProvinceList" :value="item.id" :key="item.id + 'a'">{{ item.name }}</a-select-option>
                        </a-select>
                      </a-form-model-item>
                    </a-col>
                    <a-col span="7" offset="1">
                      <a-form-model-item prop="citySn" style="margin: 0;">
                        <a-select v-model="queryParam.citySn" allowClear @change="getAreaList" placeholder="请选择市">
                          <a-select-option v-for="item in addrCityList" :value="item.id" :key="item.id + 'b'">{{ item.name }}</a-select-option>
                        </a-select>
                      </a-form-model-item>
                    </a-col>
                    <a-col span="7" offset="1">
                      <a-form-model-item prop="districtSn" style="margin: 0;">
                        <a-select v-model="queryParam.districtSn" allowClear placeholder="请选择区/县">
                          <a-select-option v-for="item in addrDistrictList" :value="item.id" :key="item.id + 'c'">{{ item.name }}</a-select-option>
                        </a-select>
                      </a-form-model-item>
                    </a-col>
                  </a-form-model-item>
                </a-row>
              </a-col>
            </template>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="returnSlipReportList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="returnSlipReportList-reset">重置</a-button>
              <a-button
                style="margin-left: 10px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_returnSlip_Report')"
                id="returnSlipReportList-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 2410, y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <template slot="footer">
          <a-row>
            <a-col span="2">合计：</a-col>
            <a-col span="22">
              <a-row>
                <a-col span="4">箭牌滤清器：{{ (totalData && (totalData.jpLqqAmount || totalData.jpLqqAmount==0)) ? totalData.jpLqqAmount : '--' }}</a-col>
                <a-col span="4">箭牌变速箱滤清器：{{ (totalData && (totalData.jpBsxLqqAmount || totalData.jpBsxLqqAmount==0)) ? totalData.jpBsxLqqAmount : '--' }}</a-col>
                <a-col span="4">箭牌雨刮片：{{ (totalData && (totalData.jpYgpAmount || totalData.jpYgpAmount==0)) ? totalData.jpYgpAmount : '--' }}</a-col>
                <a-col span="4">箭牌喇叭：{{ (totalData && (totalData.jpLbAmount || totalData.jpLbAmount==0)) ? totalData.jpLbAmount : '--' }}</a-col>
                <a-col span="4">箭牌刹车片：{{ (totalData && (totalData.jpScpAmount || totalData.jpScpAmount==0)) ? totalData.jpScpAmount : '--' }}</a-col>
                <a-col span="4">箭牌感应线：{{ (totalData && (totalData.jpGyxAmount || totalData.jpGyxAmount==0)) ? totalData.jpGyxAmount : '--' }}</a-col>
                <a-col span="4">冠牌滤清器：{{ (totalData && (totalData.gpLqqAmount || totalData.gpLqqAmount==0)) ? totalData.gpLqqAmount : '--' }}</a-col>
                <a-col span="4">冠牌雨刮片：{{ (totalData && (totalData.gpYgpAmount || totalData.gpYgpAmount==0)) ? totalData.gpYgpAmount : '--' }}</a-col>
                <a-col span="4">冠牌刹车片：{{ (totalData && (totalData.gpScpAmount || totalData.gpScpAmount==0)) ? totalData.gpScpAmount : '--' }}</a-col>
                <a-col span="4">稳升点火线圈：{{ (totalData && (totalData.wsDhxqAmount || totalData.wsDhxqAmount==0)) ? totalData.wsDhxqAmount : '--' }}</a-col>
                <a-col span="4">稳升灯泡：{{ (totalData && (totalData.wsDpAmount || totalData.wsDpAmount==0)) ? totalData.wsDpAmount : '--' }}</a-col>
                <a-col span="4">汇箭：{{ (totalData && (totalData.hjAmount || totalData.hjAmount==0)) ? totalData.hjAmount : '--' }}</a-col>
                <a-col span="4">德路斯：{{ (totalData && (totalData.dlsAmount || totalData.dlsAmount==0)) ? totalData.dlsAmount : '--' }}</a-col>
                <a-col span="4">TBU刹车片：{{ (totalData && (totalData.tbuScpAmount || totalData.tbuScpAmount==0)) ? totalData.tbuScpAmount : '--' }}</a-col>
                <a-col span="4">TBU刹车油：{{ (totalData && (totalData.tbuScyAmount || totalData.tbuScyAmount==0)) ? totalData.tbuScyAmount : '--' }}</a-col>
                <a-col span="4">TBU-P：{{ (totalData && (totalData.tbupAmount || totalData.tbupAmount==0)) ? totalData.tbupAmount : '--' }}</a-col>
                <a-col span="4">布瑞斯：{{ (totalData && (totalData.brsAmount || totalData.brsAmount==0)) ? totalData.brsAmount : '--' }}</a-col>
                <a-col span="4">NGK：{{ (totalData && (totalData.ngkAmount || totalData.ngkAmount==0)) ? totalData.ngkAmount : '--' }}</a-col>
                <a-col span="4">飞利浦：{{ (totalData && (totalData.flpAmount || totalData.flpAmount==0)) ? totalData.flpAmount : '--' }}</a-col>
                <a-col span="4">车仆：{{ (totalData && (totalData.cpAmount || totalData.cpAmount==0)) ? totalData.cpAmount : '--' }}</a-col>
                <a-col span="4">瓦尔塔：{{ (totalData && (totalData.wetAmount || totalData.wetAmount==0)) ? totalData.wetAmount : '--' }}</a-col>
                <a-col span="4">雷贝斯托：{{ (totalData && (totalData.lbstAmount || totalData.lbstAmount==0)) ? totalData.lbstAmount : '--' }}</a-col>
                <a-col span="4">德尔福：{{ (totalData && (totalData.defAmount || totalData.defAmount==0)) ? totalData.defAmount : '--' }}</a-col>
                <a-col span="4">巴诺德：{{ (totalData && (totalData.bndAmount || totalData.bndAmount==0)) ? totalData.bndAmount : '--' }}</a-col>
                <a-col span="6" v-if="$hasPermissions('B_isShowPrice')">开单退货单合计金额：{{ (totalData && (totalData.totalAmount || totalData.totalAmount==0)) ? totalData.totalAmount : '--' }}</a-col>
              </a-row>
            </a-col>
          </a-row>
        </template>
      </s-table>
    </a-spin>
    <!-- 导出提示框 -->
    <reportModal :visible="showExport" @close="showExport=false"></reportModal>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import getDate from '@/libs/getDate.js'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import custList from '@/views/common/custList.vue'
import subarea from '@/views/common/subarea.js'
import reportModal from '@/views/common/reportModal.vue'
import { getArea } from '@/api/data'
import { hdExportExcel } from '@/libs/exportExcel'
import { salesReportReturnBillList, salesReportReturnBillCount, returnSlipExport } from '@/api/reportData'
export default {
  name: 'ReturnSlipReportList',
  mixins: [commonMixin],
  components: { STable, VSelect, custList, subarea, rangeDate, reportModal },
  data () {
    return {
      spinning: false,
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0,
      showExport: false,
      queryParam: { //  查询条件
        time: [
          getDate.getCurrMonthDays().starttime,
          getDate.getCurrMonthDays().endtime
        ],
        beginDate: getDate.getCurrMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime,
        dealerSn: undefined,
        provinceSn: undefined,
        citySn: undefined,
        districtSn: undefined,
        subareaSn: undefined,
        dealerLevel: undefined,
        salesReturnBillNo: ''
      },
      rules: {
        'time': [{ required: true, message: '请选择退货日期', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出loading
      addrProvinceList: [], //  省下拉
      addrCityList: [], //  市下拉
      addrDistrictList: [], //  区下拉
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 380
        }
        const params = Object.assign(parameter, this.queryParam) //  无分页
        delete params.time
        return salesReportReturnBillList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.getCount(params)
            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
        })
      },
      totalData: null
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '区域', dataIndex: 'dealer.subareaNameSet', width: 120, align: 'center', customRender: function (text) { return (text && text.length > 0) ? text.join('/') : '--' } },
        { title: '退货单号', dataIndex: 'salesReturnBillNo', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '省份', dataIndex: 'dealer.provinceName', width: 90, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户名称', dataIndex: 'dealer.dealerName',width: 120,align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '客户级别', dataIndex: 'dealer.dealerLevelDictValue', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌',
          align: 'center',
          children: [
            {
              title: '箭牌',
              align: 'center',
              children: [
                {
                  title: '滤清器',
                  dataIndex: 'jpLqqAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '变速箱滤清器',
                  dataIndex: 'jpBsxLqqAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '雨刮片',
                  dataIndex: 'jpYgpAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '喇叭',
                  dataIndex: 'jpLbAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '刹车片',
                  dataIndex: 'jpScpAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '感应线',
                  dataIndex: 'jpGyxAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                }
              ]
            },
            {
              title: '冠牌',
              align: 'center',
              children: [
                {
                  title: '滤清器',
                  dataIndex: 'gpLqqAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '雨刮片',
                  dataIndex: 'gpYgpAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '刹车片',
                  dataIndex: 'gpScpAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                }
              ]
            },
            {
              title: '稳升',
              align: 'center',
              children: [
                {
                  title: '点火线圈',
                  dataIndex: 'wsDhxqAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '灯泡',
                  dataIndex: 'wsDpAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                }
              ]
            },
            {
              title: '汇箭',
              dataIndex: 'hjAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '德路斯',
              dataIndex: 'dlsAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: 'TBU',
              align: 'center',
              children: [
                {
                  title: '刹车片',
                  dataIndex: 'tbuScpAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                },
                {
                  title: '刹车油',
                  dataIndex: 'tbuScyAmount',
                  align: 'center',
                  width: 80,
                  customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
                }
              ]
            },
            {
              title: 'TBU-P',
              dataIndex: 'tbupAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '布瑞斯',
              dataIndex: 'brsAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            }
          ]
        },
        { title: '代理品牌',
          align: 'center',
          children: [
            {
              title: 'NGK',
              dataIndex: 'ngkAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '飞利浦',
              dataIndex: 'flpAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '车仆',
              dataIndex: 'cpAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '瓦尔塔',
              dataIndex: 'wetAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '雷贝斯托',
              dataIndex: 'lbstAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '德尔福',
              dataIndex: 'defAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            },
            {
              title: '巴诺德',
              dataIndex: 'bndAmount',
              align: 'center',
              width: 80,
              customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' }
            }
          ]
        }
        // { title: '开单退货单合计金额', width: 90, dataIndex: 'totalAmount', align: 'center', customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' } }
      ]
      if (this.$hasPermissions('B_isShowPrice')) { //  售价权限
        arr.splice(arr.length, 0, { title: '开单退货单合计金额', width: 90, dataIndex: 'totalAmount', align: 'center', customRender: function (text) { return (text || text == 0) ? ('￥' + text) : '--' } })
      }
      return arr
    }
  },
  methods: {
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.showExport = true
          _this.exportLoading = true
          _this.spinning = true
          hdExportExcel(returnSlipExport, params, '开单退货单报表', function () {
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    },
    // 总计
    getCount (params) {
      salesReportReturnBillCount(params).then(res => {
        if (res.status == 200 && res.data) {
          this.totalData = res.data
        } else {
          this.totalData = null
        }
      })
    },
    handleSearch () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.$refs.table.refresh(true)
        } else {
          return false
        }
      })
    },
    //  创建时间  change
    dateChange (date) {
      if (date[0] && date[1]) {
        this.queryParam.time = date
      } else {
        this.queryParam.time = []
      }
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    custChange (val) {
      this.queryParam.dealerSn = val.key
    },
    //  重置
    resetSearchForm () {
      this.queryParam.time = [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ]
      this.$refs.rangeDate.resetDate(this.queryParam.time)
      this.queryParam.beginDate = getDate.getCurrMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.dealerSn = undefined
      this.queryParam.provinceSn = undefined
      this.queryParam.citySn = undefined
      this.queryParam.districtSn = undefined
      this.queryParam.subareaSn = undefined
      this.queryParam.dealerLevel = undefined
      this.queryParam.salesReturnBillNo = ''
      this.$refs.custList.resetForm()
      this.totalData = null
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    // 获取城市列表
    getCityList (val) {
      this.addrCityList = []
      this.addrDistrictList = []
      this.queryParam.citySn = undefined
      this.queryParam.districtSn = undefined
      if (val) {
        this.getArea('city', val)
      }
    },
    // 获取区县列表
    getAreaList (val) {
      this.addrDistrictList = []
      this.queryParam.districtSn = undefined
      if (val) {
        this.getArea('district', val)
      }
    },
    //  省/市/区
    getArea (leve, sn) {
      let params
      if (leve == 'province') {
        params = { type: '2' }
      } else {
        params = { parentId: sn, type: leve == 'city' ? '3' : '4' }
      }
      getArea(params).then(res => {
        if (res.status == 200) {
          if (leve == 'province') {
            this.addrProvinceList = res.data || []
          } else if (leve == 'city') {
            this.addrCityList = res.data || []
          } else if (leve == 'district') {
            this.addrDistrictList = res.data || []
          }
        } else {
          if (leve == 'province') {
            this.addrProvinceList = []
          } else if (leve == 'city') {
            this.addrCityList = []
          } else if (leve == 'district') {
            this.addrDistrictList = []
          }
        }
      })
    },
    pageInit () {
      this.getArea('province')
    }
  },
  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>
