<template>
  <div class="registeredUser-baseInfo">
    <a-page-header :ghost="false" title="注册用户" :backIcon="false" class="registeredUser-back">
      <template slot="subTitle">
        <a href="javascript:;" @click="handleBack" id="registeredUser-handleBack">
          <a-icon type="left"></a-icon> 返回上页
        </a>
      </template>
    </a-page-header>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <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="5" :sm="24">
                <a-form-item label="注册时间">
                  <rangeDate ref="rangeDate" v-model="time" @change="dateChange" />
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="手机号码">
                  <a-input id="registeredUser-mobile" v-model.trim="queryParam.mobile" allowClear placeholder="请输入手机号码"/>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="用户类型">
                  <v-select id="registeredUser-sysUserFlag" code="USER_TYPE" v-model="queryParam.sysUserFlag" allowClear placeholder="请选择用户类型"></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="汽车修理厂">
                  <storeList ref="storeList" @change="storeChange" id="registeredUser-storeChange"></storeList>
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="4" :sm="24">
                  <a-form-item label="职位">
                    <v-select id="registeredUser-storeManagerFlag" code="USER_JOB" v-model="queryParam.storeManagerFlag" allowClear placeholder="请选择职位"></v-select>
                  </a-form-item>
                </a-col>
                <a-col :md="5" :sm="24">
                  <a-form-item label="配件经销商">
                    <dealerList ref="dealerList" @change="dealerChange" id="registeredUser-dealerChange"></dealerList>
                  </a-form-item>
                </a-col>
              </template>
              <a-col :md="4" :sm="24" style="margin-bottom: 10px;">
                <a-button type="primary" @click="$refs.table.refresh(true)" id="registeredUser-refresh">查询</a-button>
                <a-button type="" @click="reset" style="margin-left: 10px;" id="registeredUser-reset">重置</a-button>
                <a @click="advanced=!advanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div class="table-operator" v-if="countData">
          <a-alert type="info">
            <div slot="message" class="total-bar">
              <div>
                用户数量：<strong>{{ countData.userTotal||0 }}</strong>；
                累计vin扫描次数合计：<strong>{{ countData.vinCount||0 }}</strong>；
              </div>
            </div>
          </a-alert>
        </div>
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+84.5+'px', wordBreak: 'break-all' }"
          size="small"
          rowKey="id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <span slot="customTitle">
            <a-popover>
              <template slot="content">
                表示以用户身份的vin扫描次数，<br>而不是用户所在门店的vin扫描次数
              </template>
              <a-icon type="question-circle" theme="twoTone" />
            </a-popover>
            累计vin扫描次数
          </span>
        </s-table>
      </a-spin>
    </a-card>
  </div>
</template>

<script>
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import { registerUserQueryPage, queryStatistics } from '@/api/report.js'
import dealerList from '@/views/common/dealerList.vue'
import storeList from '@/views/common/storeList.vue'
import { toThousands } from '@/libs/tools.js'
export default {
  name: 'UserList',
  components: { STable, VSelect, rangeDate, dealerList, storeList },
  data () {
    return {
      spinning: false,
      advanced: false,
      tableHeight: 0,
      time: [],
      // 查询参数
      queryParam: {
        beginDate: '',
        endDate: '',
        mobile: '',
        storeManagerFlag: undefined,
        sysUserFlag: undefined,
        dealerSn: undefined,
        storeSn: undefined
      },
      showModal: false,
      itemData: null, // 编辑行数据
      openStatusModal: false, // 货架状态弹窗初始状态
      countData: null,
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '注册时间', dataIndex: 'registerDate', width: '15%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '手机号码', dataIndex: 'mobile', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '用户类型', dataIndex: 'sysUserFlagDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '汽车修理厂', dataIndex: 'storeName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '职位', dataIndex: 'storeManagerFlagDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '配件经销商', dataIndex: 'dealerName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { slots: { title: 'customTitle' }, dataIndex: 'vinCount', width: '10%', align: 'center', customRender: function (text) { return toThousands(text, 2) || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return registerUserQueryPage(params).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (let i = 0; i < data.list.length; i++) {
              const _item = data.list[i]
              _item.no = no + i + 1
            }
          }
          this.getCount(params)
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    handleBack () {
      this.$router.go(-1)
    },
    getCount (params) {
      queryStatistics(params).then(res => {
        this.countData = res.data || null
      })
    },
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 经销商 change
    dealerChange (obj) {
      this.queryParam.dealerSn = obj.key || undefined
    },
    // 汽车修理厂 change
    storeChange (obj) {
      this.queryParam.storeSn = obj.key || undefined
    },
    // 重置
    reset () {
      this.queryParam = {
        beginDate: '',
        endDate: '',
        mobile: '',
        storeManagerFlag: undefined,
        sysUserFlag: undefined,
        dealerSn: undefined,
        storeSn: undefined
      }
      this.time = []
      this.$refs.rangeDate.resetDate('')
      this.$refs.table.refresh(true)
      if (this.advanced) {
        this.$refs.dealerList.resetForm()
      }
      this.$refs.storeList.resetForm()
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
        _this.reset()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 255
    }
  },
  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()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
  }
}
</script>
<style lang="less">
  .registeredUser-baseInfo{
    .registeredUser-back{
      margin-bottom: 6px;
    }
  }
</style>
