<template>
  <a-card size="small" :bordered="false" class="approveStore-wrap">
    <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="6" :sm="24">
              <a-form-item label="经销商名称">
                <a-input allowClear placeholder="请输入经销商名称" v-model.tirm="queryParam.dealerName"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="商城状态">
                <v-select
                  v-model="queryParam.openFlag"
                  ref="openFlag"
                  id="approveStore-openFlag"
                  code="OPEN_FLAG"
                  placeholder="请选择商城状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="支付状态">
                <v-select
                  v-model="queryParam.payOpenFlag"
                  ref="payOpenFlag"
                  id="approveStore-payOpenFlag"
                  :list="payStatusList"
                  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.wait_audit"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 商城状态-->
        <template slot="statusVal" slot-scope="text, record">
          <a-switch checked-children="已开启" un-checked-children="已关闭" v-model="record.statusFlag" @change="e=>handleStatus(e,record)"/>
        </template>
        <!-- 支付状态-->
        <template slot="payVal" slot-scope="text, record">
          <span v-if="record.payOpenFlag=='wait_audit'">{{ payStatus(record.payOpenFlag) }}</span>
          <a-button size="small" type="link" v-if="record.payOpenFlag=='wait_open'" @click="e=>handlePayStatus(e,record)">申请开通</a-button>
          <div v-if="record.payOpenFlag=='pay_opened'||record.payOpenFlag=='pay_close'">
            <a-switch checked-children="已开启" un-checked-children="已关闭" v-model="record.payFlag" @change="e=>handlePayStatus(e,record)"/>
          </div>
        </template>
      </s-table>
    </a-spin>
  </a-card>
</template>

<script>
// 组件
import { STable, VSelect } from '@/components'
// 接口
import { findMallParamList, updateMallParam, updatePayOpenParam } from '@/api/dealerBizParam'
export default {
  components: { STable, VSelect },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      // 查询条件
      queryParam: {
        dealerName: undefined, // 经销商名称
        delearSn: undefined,
        openFlag: undefined, // 商城状态
        payOpenFlag: undefined// 支付状态
      },
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '经销商名称', dataIndex: 'dealerName', width: '30%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商城状态时间', dataIndex: 'updateDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商城状态', dataIndex: 'paramValue', width: '15%', align: 'center', scopedSlots: { customRender: 'statusVal' } },
        { title: '支付状态时间', dataIndex: 'payOpenActDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '支付状态', dataIndex: 'payOpenFlag', width: '15%', align: 'center', scopedSlots: { customRender: 'payVal' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return findMallParamList(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
              data.list[i].statusFlag = data.list[i].paramValue == '1'
              data.list[i].payFlag = data.list[i].payOpenFlag == 'pay_opened'
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      payStatusList: [
        { code: 'pay_opened', dispName: '已开通' },
        { code: 'wait_open', dispName: '待开通' },
        { code: 'wait_audit', dispName: '审核中' },
        { code: 'pay_close', dispName: '已关闭' }
      ]
    }
  },
  methods: {
    payStatus (val) {
      const row = this.payStatusList.find(item => item.code == val)
      return row ? row.dispName : '--'
    },
    custChange (val) {
      if (val) {
        this.queryParam.delearSn = val.key
        this.queryParam.dealerName = val.label.match(/[\u4e00-\u9fff]+/g).join('')
      } else {
        this.queryParam.delearSn = undefined
        this.queryParam.dealerName = undefined
      }
    },
    // 修改商城状态
    handleStatus (val, row) {
      const _this = this
      _this.spinning = true
      updateMallParam({
        tenantId: row.tenantId,
        paramValue: row.paramValue == '0' ? '1' : '0'
      }).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$refs.table.refresh()
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    },
    // 修改支付开通状态
    handlePayStatus (val, row) {
      const _this = this
      _this.spinning = true
      updatePayOpenParam({
        tenantId: row.tenantId,
        paramValue: row.payOpenFlag == 'wait_open' ? 'wait_audit' : (row.payOpenFlag == 'pay_close' ? 'pay_opened' : 'pay_close')
      }).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.dealerName = undefined
      this.queryParam.delearSn = undefined
      this.queryParam.openFlag = undefined
      this.queryParam.payOpenFlag = undefined
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 420
    },
    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>
