<template>
  <div class="customer-wrap">
    <!-- 搜索区域 -->
    <div class="search-area" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
        <el-form-item label="客群名称" prop="groupName">
          <el-input
            v-model="queryParams.groupName"
            placeholder="请输入客群名称"
            clearable
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客群模式" prop="groupMode">
          <el-select v-model="queryParams.groupMode" placeholder="请选择" clearable style="width: 150px">
            <el-option label="静态" value="0" />
            <el-option label="动态" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建方式" prop="createMode">
          <el-select v-model="queryParams.createMode" placeholder="请选择" clearable style="width: 150px">
            <el-option label="模板导入" value="1" />
            <el-option label="绩效网格" value="2" />
            <el-option label="地理网格" value="3" />
            <el-option label="绘制网格" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="客群状态" prop="groupStatus">
          <el-select v-model="queryParams.groupStatus" placeholder="请选择" clearable style="width: 150px">
            <el-option label="正常" value="0" />
            <el-option label="已禁用" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作栏 -->
    <section class="operate-cnt">
      <div class="operate-left">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete">删除</el-button>
      </div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </section>

    <!-- 表格区域 -->
    <div class="main_table">
      <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange" style="width: 100%" max-height="625">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客群名称" prop="groupName" min-width="180" show-overflow-tooltip />
        <el-table-column label="客群模式" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.groupMode === '0'" type="info" size="small">静态</el-tag>
            <el-tag v-else-if="scope.row.groupMode === '1'" type="success" size="small">动态</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建方式" align="center" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.createMode === '1'">模板导入</span>
            <span v-else-if="scope.row.createMode === '2'">绩效网格</span>
            <span v-else-if="scope.row.createMode === '3'">地理网格</span>
            <span v-else-if="scope.row.createMode === '4'">绘制网格</span>
          </template>
        </el-table-column>
        <el-table-column label="客户数量" align="center" prop="custCount" width="100" />
        <el-table-column label="客群状态" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.groupStatus === '0'" type="success" size="small">正常</el-tag>
            <el-tag v-else-if="scope.row.groupStatus === '1'" type="danger" size="small">已禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建者" prop="nickName" width="120" show-overflow-tooltip />
        <el-table-column label="创建时间" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(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"
      />
    </div>

    <!-- 创建/编辑客群弹窗 -->
    <create-dialog
      :visible.sync="dialogVisible"
      :group-data="form"
      :is-edit="isEdit"
      @submit="handleSubmit"
    />
  </div>
</template>

<script>
import { listCustGroup, getCustGroup, deleteCustGroup } from '@/api/group/custGroup'
import CreateDialog from './components/create-dialog'

export default {
  name: 'CustGroup',
  components: { CreateDialog },
  data() {
    return {
      // 加载状态
      loading: false,
      // 显示搜索
      showSearch: true,
      // 选中ID数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 客群列表
      groupList: [],
      // 弹窗显示
      dialogVisible: false,
      // 是否编辑
      isEdit: false,
      // 表单数据
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupName: null,
        groupMode: null,
        createMode: null,
        groupStatus: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询客群列表 */
    getList() {
      this.loading = true
      listCustGroup(this.queryParams).then(response => {
        this.groupList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.isEdit = false
      this.dialogVisible = true
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids[0]
      getCustGroup(id).then(response => {
        this.form = response.data
        this.isEdit = true
        this.dialogVisible = true
      })
    },

    /** 查看按钮操作 */
    handleView(row) {
      this.$router.push({
        path: '/group/custGroup/detail',
        query: { groupId: row.id }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$modal.confirm('是否确认删除选中的客群？').then(() => {
        return deleteCustGroup(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },

    /** 提交表单 */
    handleSubmit() {
      this.dialogVisible = false
      this.getList()
    },

    /** 表单重置 */
    reset() {
      this.form = {
        id: null,
        groupName: null,
        groupMode: '0',
        createMode: null,
        groupStatus: '0',
        shareEnabled: 0,
        shareDeptIdList: [],
        remark: null,
        validTime: null
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.customer-wrap {
  background-color: #ffffff;
  overflow: hidden;
  box-shadow: 0 3px 8px 0 #00000017;
  border-radius: 16px 16px 0 0;
  padding: 24px 30px;

  .search-area {
    padding: 16px 0;
    border-bottom: 1px solid #ebebeb;
    margin-bottom: 16px;

    .el-form {
      margin-bottom: -8px;

      .el-form-item {
        margin-bottom: 8px;
      }
    }
  }

  .operate-cnt {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 24px 0 16px 0;

    .operate-left {
      display: flex;
      gap: 8px;
    }

    .el-button {
      border-radius: 4px;
    }
  }

  .main_table {
    ::v-deep .el-pagination {
      margin-top: 15px;
    }
  }
}
</style>
