<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container" v-if="!!~[2].indexOf(userType)">
      <div class="top clearfix">
        <div class="title fl">条件筛选</div>
      </div>
      <el-form ref="screenForm" :model="screenForm" label-width="70px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="商户账号" prop="account">
              <el-input v-model="screenForm.account" placeholder="请输入商户账号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="商户昵称" prop="nickname">
              <el-input v-model="screenForm.nickname" placeholder="请输入商户昵称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="screenForm.phone" placeholder="请输入联系电话"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="电子邮箱" prop="email">
              <el-input v-model="screenForm.email" placeholder="请输入电子邮箱"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="状态" prop="state">
              <el-select v-model="screenForm.status" placeholder="全部">
                <el-option label="全部" value=""></el-option>
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="(item, index) in select_status"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="18" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix" v-if="!!~[2].indexOf(userType)">
        <div class="fl">
          <el-button size="small" type="primary" @click="toCreate()">开通账号</el-button>
        </div>
        <div class="fr">
          <el-button size="small" type="primary" icon="el-icon-download" @click="handleExport">导出数据</el-button>
        </div>
      </div>
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column align="center" label="操作" width="230" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="toDetail(scope.row.adminUserId, 1)">详情</el-button>
              <el-button type="text" @click="toDetail(scope.row.adminUserId, 2)">商城配置</el-button>
              <template v-if="!!~[2].indexOf(userType)">
                <el-button type="text" @click="toDetail(scope.row.adminUserId, 3)">微信配置</el-button>
                <el-popconfirm
                  v-if="scope.row.status"
                  style="margin-left: 10px"
                  title="确定冻结吗？"
                  @confirm="changeStatus(scope.row.adminUserId, 0)"
                >
                  <el-button slot="reference" type="text">冻结</el-button>
                </el-popconfirm>
                <el-popconfirm
                  v-else
                  style="margin-left: 10px"
                  title="确定恢复吗？"
                  @confirm="changeStatus(scope.row.adminUserId, 1)"
                >
                  <el-button slot="reference" type="text">恢复</el-button>
                </el-popconfirm>
              </template>
            </template>
          </el-table-column>
          <el-table-column align="center" label="商户账号" prop="userName" min-width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" @click="toAccountList(scope.row)">查看</el-button>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="商户昵称"
            prop="companyName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="负责人"
            prop="linkName"
            min-width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="角色"
            prop="roleName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="联系电话"
            prop="linkPhone"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="到期时间"
            prop="expireTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="电子邮箱"
            prop="email"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="地址"
            prop="address"
            min-width="200"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="center" label="状态" class-name="status-col">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '正常' : '冻结' }}</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="pagination clearfix">
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { COMMON_SELECT } from '@/utils/select_data'
import { getList, changeAccountStatus } from '@/api/merchant'
import { downloadFiles } from '@/utils/util'
export default {
  data() {
    return {
      isOpen: false, // 是否展开条件筛选
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: null, // 列表数据
      listLoading: false, // 列表加载loading
      screenForm: {
        // 筛选表单数据
        account: '',
        nickname: '',
        phone: '',
        email: '',
        status: ''
      },
      select_status: [
        // 筛选字段 - 状态
        { label: '正常', value: true },
        { label: '冻结', value: false }
      ]
    }
  },
  computed: {
    userType() {
      return JSON.parse(localStorage.getItem('greemall_user')).type
    }
  },
  created() {
    this.getListfun()
  },
  methods: {
    // 查询列表
    getListfun() {
      this.listLoading = true
      getList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.screenForm
      }).then(res => {
        this.dataList = res.data.records
        this.listTotal = res.data.total
        this.listLoading = false
      })
    },
    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1
      this.getListfun()
    },
    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields()
      this.currentPage = 1
      this.getListfun()
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getListfun()
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getListfun()
    },

    // 去开通账号
    toCreate() {
      this.$router.push({
        name: 'settledManagement',
        query: {}
      })
    },

    toAccountList(data) {
      this.$router.push({
        name: 'account',
        query: {
          companyWechatId: data.companyWechatId
        }
      })
    },

    toDetail(id, type) {
      this.$router.push({
        name: 'tenantDetails',
        query: {
          id,
          type
        }
      })
    },

    // 操作 - 更改状态(type: 禁用0，启用1)
    changeStatus(id, type) {
      type = type ? true : false
      changeAccountStatus({ adminUserId: id, status: type }).then(res => {
        this.getListfun()
        this.$successMsg()
      })
    },

    // 导出
    handleExport() {
      let screenData = {
        userName: this.screenForm.account,
        nickName: this.screenForm.nickName,
        linkPhone: this.screenForm.phone,
        email: this.screenForm.email,
        status: this.screenForm.status
      }
      downloadFiles('admin/user/mch/export', screenData)
    }
  }
}
</script>

<style scoped></style>
