<template>
  <a-card size="small" :bordered="false" class="approveStore-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 提示 -->
      <a-alert type="info" style="margin-bottom: 10px">
        <div slot="message">我的邀请码：{{ dealerInviteCode }} <a-button type="link" v-copyText="dealerInviteCode">复制</a-button></div>
      </a-alert>
      <!-- 搜索条件 -->
      <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="4" :sm="24">
              <a-form-item label="联系人手机">
                <a-input allowClear placeholder="请输入联系人手机" v-model.tirm="queryParam.contactPhone"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="联系人姓名">
                <a-input allowClear placeholder="请输入联系人姓名" v-model.tirm="queryParam.contactName"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="门店名称">
                <a-input allowClear placeholder="请输入门店名称" v-model.tirm="queryParam.storeName"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="审核状态">
                <v-select
                  v-model="queryParam.auditStatus"
                  ref="auditStatus"
                  id="approveStore-auditStatus"
                  code="XPRH_STORE_APPLY_STATUS"
                  placeholder="请选择审核状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <div>
                <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="approveStore-refresh">查询</a-button>
                <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="approveStore-reset">重置</a-button>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 图片 -->
        <template slot="imageUrl" slot-scope="text, record">
          <div v-if="text&&text.length">
            <img :src="text" width="30" height="30" class="imageUrl" @click="inited([text])" />
          </div>
          <span v-else>--</span>
        </template>
        <!-- 地址 -->
        <template slot="addr" slot-scope="text, record">
          <div style="word-break:break-all;" v-if="record.addrProvinceName||record.addrDetail">
            <div v-if="record.addrProvinceName">{{ record.addrProvinceName +'-'+ record.addrCityName +'-'+ record.addrDistrictName }}</div>
            <div v-if="record.addrDetail">{{ record.addrDetail }}</div>
          </div>
          <div v-else>--</div>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <div v-if="record.auditStatus == 'WAIT'">
            <a-button size="small" type="link" class="button-primary" @click="handleAudit(record,1)">通过</a-button>
            <a-button size="small" type="link" class="button-primary" @click="handleAudit(record,0)">不通过</a-button>
          </div>
          <span v-else>--</span>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
import { mapState } from 'vuex'
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { xprhStoreApplyQueryPage, xprhStoreApplyAudit } from '@/api/approveStore'
export default {
  components: { STable, VSelect },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: {
        contactName: '',
        contactPhone: '',
        storeName: '',
        auditStatus: undefined
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '提交时间', dataIndex: 'applyTime', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '联系人手机', dataIndex: 'contactPhone', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '联系人姓名', dataIndex: 'contactName', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '门店名称', dataIndex: 'storeName', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '门店地址', dataIndex: 'addrDetail', width: '22%', align: 'center', scopedSlots: { customRender: 'addr' } },
        { title: '门头照片', dataIndex: 'storeImage', width: '6%', align: 'center', scopedSlots: { customRender: 'imageUrl' } },
        { title: '营业执照', dataIndex: 'licenseImage', width: '6%', align: 'center', scopedSlots: { customRender: 'imageUrl' } },
        { title: '审核状态', dataIndex: 'auditStatusDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '价格权限设置', dataIndex: 'priceShowName', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return xprhStoreApplyQueryPage(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.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false,
      nowData: null
    }
  },
  computed: {
    ...mapState({
      dealerInviteCode: state => state.user.info.extInfo.dealer.dealerInviteCode
    })
  },
  methods: {
    inited (viewer) {
      if (viewer.length) {
        this.$viewerApi({
          images: viewer
        })
      }
    },
    // 审核
    handleAudit (row, type) {
      // 审核通过
      if (type == 1) {
        this.$router.push({ name: 'approveStoreAuthPass', params: { sn: row.applySn } })
      } else {
        const _this = this
        this.$confirm({
          title: '提示',
          content: '确认审核不通过吗？',
          centered: true,
          closable: true,
          onOk () {
            _this.spinning = true
            xprhStoreApplyAudit({
              applySn: row.applySn,
              auditStatus: 'REFUSE'
            }).then(res => {
              if (res.status == 200) {
                _this.$message.success(res.message)
                _this.$refs.table.refresh()
                _this.spinning = false
              } else {
                _this.spinning = false
              }
            })
          }
        })
      }
    },
    // 重置
    resetSearchForm () {
      this.queryParam.contactName = ''
      this.queryParam.contactPhone = ''
      this.queryParam.storeName = ''
      this.queryParam.auditStatus = undefined
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 245
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
        _this.resetSearchForm()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.setTableH()
      this.$refs.table.refresh()
    }
  }
}
</script>
