<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="small" label-width="100px">
        <el-form-item label="租户名称" prop="companyName">
          <el-input
            v-model="queryParams.companyName"
            placeholder="请输入租户名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户名称" prop="externalContactName">
          <el-input
            v-model="queryParams.externalContactName"
            placeholder="请输入客户名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入客户手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择客户状态" clearable size="small">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <inline-tenant-selector @change="handleQuery" />
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['admin:qwContact:list']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border size="small" style="width:100%" v-loading="loading" :data="externalContactList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户名称" align="center" prop="companyName" />
      <el-table-column label="客户名称" align="center" prop="externalContactName" />
      <el-table-column label="客户手机号" align="center" prop="phone" />
      <el-table-column label="客户备注" align="center" prop="remark" />
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="客户状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">正常</el-tag>
          <el-tag v-else type="error">已删除</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            plain
            @click="handleView(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情弹窗 -->
    <el-dialog title="企微用户详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-form :model="viewForm" label-width="100px">
        <el-form-item label="租户名称">
          <span>{{ viewForm.companyName }}</span>
        </el-form-item>
        <el-form-item label="客户名称">
          <span>{{ viewForm.externalContactName }}</span>
        </el-form-item>
        <el-form-item label="客户手机号">
          <span>{{ viewForm.phone }}</span>
        </el-form-item>
        <el-form-item label="客户备注">
          <span>{{ viewForm.remark }}</span>
        </el-form-item>
        <el-form-item label="添加时间">
          <span>{{ viewForm.createTime }}</span>
        </el-form-item>
        <el-form-item label="客户状态">
          <el-tag v-if="viewForm.status == 0" type="success">正常</el-tag>
          <el-tag v-else type="error">已删除</el-tag>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listAllExternalContact, getExternalContact, exportAllExternalContact } from '@/api/admin/qwExternalContact'

import InlineTenantSelector from "@/components/InlineTenantSelector"

export default {
  name: 'QwExternalContactAdmin',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企微用户表格数据
      externalContactList: [],
      // 状态选项
      statusOptions: [
        { value: 0, label: '正常' },
        { value: 1, label: '已删除' }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        externalContactName: null,
        phone: null,
        status: null
      },
      // 详情弹窗
      viewOpen: false,
      viewForm: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listAllExternalContact(this.queryParams).then(response => {
        this.externalContactList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 查看详情 */
    handleView(row) {
      getExternalContact(row.id).then(response => {
        this.viewForm = response.data
        this.viewOpen = true
      })
    },
    /** 导出按钒操作 */
    handleExport() {
      this.exportLoading = true
      exportAllExternalContact(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>