<template>
  <a-card size="small" :bordered="false" class="chainSalesDetailsCountReportList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form-model
          id="chainSalesDetailsCountReportList-form"
          ref="ruleForm"
          class="form-model-con"
          layout="inline"
          :model="queryParam"
          :rules="rules"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          @keyup.enter.native="handleSearch" >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="审核日期" prop="time">
                <rangeDate id="chainSalesDetailsCountReportList-time" ref="rangeDate" @change="dateChange" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="连锁店" prop="tenantId">
                <a-select
                  placeholder="请选择连锁店"
                  id="chainSalesDetailsCountReportList-tenantId"
                  allowClear
                  v-model="queryParam.tenantId"
                  :showSearch="true"
                  option-filter-prop="children"
                  :filter-option="filterOption">
                  <a-select-option v-for="item in linkageGroupData" :id="item.dealerSn" :key="item.dealerSn" :value="item.dealerSn">{{ item.dealerName }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="2" :sm="8" style="margin-top: 6px;">
              <a-checkbox @change="onChange" :default-checked="queryParam.oosFlag" id="chainSalesDetailsCountReportList-oosFlag">
                包含急件
              </a-checkbox>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="handleSearch" :disabled="disabled" id="chainSalesDetailsCountReportList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="chainSalesDetailsCountReportList-reset">重置</a-button>
              <a-button
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                v-if="$hasPermissions('B_chainSalesDetailsCountReport_export')"
                id="chainSalesDetailsCountReportList-export">导出</a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.targetSn"
        :scroll="{ x: 1500 }"
        rowKeyName="targetSn"
        :columns="columns"
        :data="loadData"
        :defaultLoadData="false"
        :showPagination="false"
        bordered>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { downloadExcel } from '@/libs/JGPrint.js'
import { reportSalesBillDetailTotalList, reportSalesBillDetailTotalExport, linkageGroupList } from '@/api/reportData'
export default {
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate },
  data () {
    const _this = this
    return {
      spinning: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      tableHeight: 0,
      queryParam: { //  查询条件
        tenantId: undefined, // 连锁店id
        time: [],
        beginDate: '', // 开始时间
        endDate: '', // 结束时间
        oosFlag: true // 包含急件
      },
      rules: {
        'time': [{ required: true, message: '请选择审核日期', trigger: 'change' }]
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      columns: [
        { title: '序号', dataIndex: 'no', width: 50, align: 'center', fixed: 'left' },
        { title: '门店', dataIndex: 'salesTargetName', width: 150, align: 'left', fixed: 'left', customRender: function (text) { return text || '--' } },
        {
          title: '所有品牌',
          children: [
            { title: '滤清器', dataIndex: 'lqqAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
            { title: '雨刮片', dataIndex: 'ygpAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
            { title: '刹车制动系统', dataIndex: 'scpAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
            { title: '蓄电池', dataIndex: 'xdcAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
            { title: 'LED灯', dataIndex: 'ledAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
          ]
        },
        {
          title: '稳升',
          children: [
            { title: '点火线圈', dataIndex: 'dhxqAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
          ]
        },
        {
          title: '德路斯、TBU',
          children: [
            { title: '润滑油', dataIndex: 'rhyAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
            { title: '变速箱油', dataIndex: 'bsxyAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
            { title: '刹车油', dataIndex: 'scyAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
          ]
        },
        {
          title: '火花塞',
          children: [
            { title: 'NGK', dataIndex: 'hhsNgkAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
            { title: '中冠', dataIndex: 'hhsZgAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
          ]
        },
        {
          title: '轮胎',
          children: [
            { title: '箭冠', dataIndex: 'ltAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
          ]
        },
        {
          title: '防冻液',
          children: [
            { title: '酷兰德', dataIndex: 'fdyAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
          ]
        },
        { title: '皮带', dataIndex: 'pdAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '轮子', dataIndex: 'lzAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '其他', dataIndex: 'otherAmount', width: 90, align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } },
        { title: '合计', dataIndex: 'totalAmount', width: 100, align: 'right', fixed: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text) : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        const params = Object.assign(parameter, this.queryParam)
        this.spinning = true
        delete params.time
        return reportSalesBillDetailTotalList(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      linkageGroupData: [], // 连锁店列表
      totalData: null // 统计数据
    }
  },
  methods: {
    // 是否包含急件
    onChange (e) {
      this.queryParam.oosFlag = e.target.checked
    },
    //  创建时间  change
    dateChange (date) {
      this.queryParam.time = date
      this.queryParam.beginDate = date[0] || ''
      this.queryParam.endDate = date[1] || ''
    },
    //  查询
    handleSearch () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$refs.table.refresh(true)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.tenantId = undefined
      this.$refs.rangeDate.resetDate()
      this.queryParam.time = []
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.totalData = null
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearTable()
    },
    //  导出
    handleExport () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.queryParam
          _this.exportLoading = true
          _this.spinning = true
          reportSalesBillDetailTotalExport(params).then(res => {
            downloadExcel(res, '连锁门店销售明细合计报表')
            _this.exportLoading = false
            _this.spinning = false
          })
        } else {
          return false
        }
      })
    },
    // 连锁店
    getLinkageGroup () {
      linkageGroupList({}).then(res => {
        if (res.status == 200) {
          const data = res.data || []
          if (data.length > 0) {
            this.linkageGroupData = data
          } else {
            this.linkageGroupData = []
          }
        } else {
          this.linkageGroupData = []
        }
      })
    },
    // 连锁店筛选
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    pageInit () {
      this.getLinkageGroup()
    }
  },
  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>
