<template>
  <a-card size="small" :bordered="false" class="merchantInfoManagementList-wrap">
    <!-- 搜索条件 -->
    <div 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="区域">
              <subarea id="merchantInfoManagementList-subarea" v-model="queryParam.subareaSn"></subarea>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="商户名称/别名">
              <a-input id="merchantInfoManagementList-nameLike" v-model.trim="queryParam.nameLike" allowClear placeholder="请输入商户名称/别名"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="商户类型">
              <v-select code="DEALER_TYPE" id="merchantInfoManagementList-dealerType" v-model="queryParam.dealerType" allowClear placeholder="请选择商户类型"></v-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-item label="审核状态">
                <v-select code="AUDIT_STATE" id="merchantInfoManagementList-auditState" v-model="queryParam.auditState" allowClear placeholder="请选择审核状态"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="合作状态">
                <v-select code="DEALER_COOPERATE_FLAG" id="merchantInfoManagementList-cooperateFlag" v-model="queryParam.cooperateFlag" allowClear placeholder="请选择合作状态"></v-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="24">
            <a-button style="margin-bottom: 18px;" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="merchantInfoManagementList-refresh">查询</a-button>
            <a-button style="margin: 0 0 18px 5px" @click="resetSearchForm" :disabled="disabled" id="merchantInfoManagementList-reset">重置</a-button>
            <a-button
              style="margin-left: 5px"
              type="primary"
              class="button-warning"
              @click="handleExport"
              :disabled="disabled"
              :loading="exportLoading"
              id="merchantInfoManagementList-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>
    </div>
    <!-- 操作按钮 -->
    <div class="table-operator">
      <a-button id="merchantInfoManagementList-add" type="primary" class="button-error" @click="handleAdd">新增</a-button>
    </div>
    <!-- 总计 -->
    <a-alert type="info" showIcon style="margin-bottom:15px">
      <div slot="message">合计：<strong>{{ total }}</strong>条</div>
    </a-alert>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 2240, y: tableHeight }"
      bordered>
      <!-- 经销商 -->
      <template slot="dealerName" slot-scope="text, record">
        <span style="color: #ed1c24;cursor: pointer;" @click="handleDetail(record)">{{ record.dealerName }}</span>
      </template>
      <!-- 区域 -->
      <template slot="area" slot-scope="text, record">
        <div v-if="record.subareaNameSet&&record.subareaNameSet.length!=0">
          <span v-for="(item, index) in record.subareaNameSet" :key="index">{{ item }}{{ index+1 == record.subareaNameSet.length ? '' : '，' }}</span>
        </div>
        <span v-else>--</span>
      </template>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" class="button-warning" v-if="record.auditState == 'WAIT'" @click="handleAudit(record)" >审核</a-button>
        <a-button size="small" type="link" class="button-success" @click="handleCooperation(record)" >合作设置</a-button>
        <a-button size="small" type="link" class="button-error" v-if="record.auditState != 'WAIT'" @click="handleDistribution(record)">经销权</a-button>
        <a-button size="small" type="link" class="button-primary" @click="handleLevel(record)">级别设置</a-button>
        <a-button size="small" type="link" class="button-info" @click="handleEdit(record)">编辑</a-button>
      </template>
    </s-table>
    <!-- 产品详情 -->
    <merchant-info-management-detail-modal :openModal="openModal" :isAudit="isAudit" :itemId="itemId" @ok="resetForm" @close="closeModal" />
    <!-- 合作设置 -->
    <UpdateCooperate ref="updateCooperate" :openUcModal="openUcModal" @ok="resetForm" @close="openUcModal=false"></UpdateCooperate>
    <!-- 级别设置 -->
    <UpdateDealerLeve ref="updateDealerLevel" :openUdModal="openUdModal" @ok="resetForm" @close="openUdModal=false"></UpdateDealerLeve>
  </a-card>
</template>

<script>
import moment from 'moment'
import { dealerQueryList, dealerExport } from '@/api/dealer'
import { STable, VSelect } from '@/components'
import subarea from '@/views/common/subarea.js'
import merchantInfoManagementDetailModal from './detailModal.vue'
import UpdateCooperate from './updateCooperate.vue'
import UpdateDealerLeve from './updateDealerLevel.vue'
export default {
  components: { STable, VSelect, merchantInfoManagementDetailModal, subarea, UpdateCooperate, UpdateDealerLeve },
  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        nameLike: '', //  商户名称、别名
        dealerType: undefined, //  商户类型
        subareaSn: undefined, //  区域
        auditState: undefined, // 审核状态
        cooperateFlag: undefined //  合作状态
      },
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false, // 导出loading
      dateFormat: 'YYYY-MM-DD',
      total: 0,
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center' },
        { title: '区域', scopedSlots: { customRender: 'area' }, width: 200, align: 'center' },
        { title: '商户名称', scopedSlots: { customRender: 'dealerName' }, align: 'center', ellipsis: true },
        { title: '商户别名', dataIndex: 'dealerAlias', width: 200, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '商户类型', dataIndex: 'dealerTypeDictValue', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '商户级别', dataIndex: 'dealerLevelDictValue', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '联系人', dataIndex: 'contact', width: 200, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '联系电话', dataIndex: 'dealerTelephone', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '绑定手机号码', dataIndex: 'managerMobile', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '审核状态', dataIndex: 'auditStateDictValue', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '合作状态', dataIndex: 'cooperateFlagDictValue', width: 120, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '生效时间', dataIndex: 'cooperateEffectiveTime', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 240, align: 'center', fixed: 'right' }
      ],
      loading: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 440
        }
        return dealerQueryList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          this.total = data.count
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          return data
        })
      },
      openModal: false, //  查看详情  弹框
      openUcModal: false, // 合作设置 弹框
      openUdModal: false, // 级别设置 弹框
      itemId: '', //  当前商户sn
      isAudit: false
    }
  },
  methods: {
    //  重置列表
    resetSearchForm () {
      this.queryParam = {
        nameLike: '', //  商户名称、别名
        dealerType: undefined, //  商户类型
        subareaSn: undefined, //  区域
        auditState: undefined, // 审核状态
        cooperateFlag: undefined //  合作状态
      }
      this.$refs.table.refresh(true)
    },
    // 刷新当前列表
    resetForm () {
      this.$refs.table.refresh()
      this.openModal = false
      this.isAudit = false
      this.openUcModal = false
      this.openUdModal = false
    },
    // 新增
    handleAdd () {
      this.$router.push({ name: `merchantInfoManagementAdd` })
    },
    // 编辑
    handleEdit (row) {
      this.$router.push({ name: `merchantInfoManagementEdit`, params: { id: row.dealerSn } })
    },
    // 合作设置
    handleCooperation (row) {
      this.openUcModal = true
      this.$refs.updateCooperate.setData(row)
    },
    // 价格级别
    handleLevel (row) {
      this.openUdModal = true
      this.$refs.updateDealerLevel.setData(row)
    },
    // 经销权
    handleDistribution (row) {
      this.$router.push({ name: `merchantInfoManagementSet`, params: { sn: row.dealerSn } })
    },
    // 审核
    handleAudit (row) {
      this.itemId = row.dealerSn
      this.openModal = true
      this.isAudit = true
    },
    //  详情
    handleDetail (row) {
      this.itemId = row.dealerSn
      this.openModal = true
      this.isAudit = false
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    //  导出
    handleExport () {
      const _this = this
      const params = this.queryParam
      this.exportLoading = true
      dealerExport(params).then(res => {
        this.exportLoading = false
        if (res.type == 'application/json') {
          var reader = new FileReader()
          reader.addEventListener('loadend', function () {
            const obj = JSON.parse(reader.result)
            _this.$notification.error({
              message: '提示',
              description: obj.message
            })
          })
          reader.readAsText(res)
        } else {
          this.download(res)
        }
      })
    },
    download (data) {
      if (!data) { return }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const a = moment().format('YYYYMMDDHHmmss')
      const fname = '经销商资料' + a
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
    })
  }
}
</script>

<style lang="less">
  .merchantInfoManagementList-wrap{
    .productPic{
      cursor: pointer;
    }
  }
</style>
