<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card shadow="hover" class="search-card">
      <el-form :model="queryParams" ref="queryRef" inline label-width="90px">
        <el-form-item label="客户名称" prop="customerName" class="custom-form-item">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable />
        </el-form-item>
        <el-form-item label="联系人类型" prop="type" class="custom-form-item">
          <el-select v-model="queryParams.type" placeholder="请选择" clearable>
            <el-option
              v-for="dict in contactTypeOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="gender" class="custom-form-item">
          <el-select v-model="queryParams.gender" placeholder="请选择" clearable>
            <el-option label="男" value="0" />
            <el-option label="女" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机" prop="phone" class="custom-form-item">
          <el-input v-model="queryParams.phone" placeholder="请输入手机" clearable />
        </el-form-item>
      </el-form>

      <div class="search-btns-area">
        <el-button icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </div>
    </el-card>

    <!-- 列表区域 -->
    <el-card shadow="hover" class="table-card">
      <template #header>
        <div class="card-header">
          <span class="list-title">联系人信息列表</span>
        </div>
      </template>
      <el-table v-loading="loading" :data="contactPersonList" border class="custom-table" :header-cell-style="{ background: '#f8fafc', color: '#333' }">
        <el-table-column label="联系人" align="center" prop="contactName">
          <template #default="scope">
            <span class="contact-name-text">{{ scope.row.contactName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="部门" align="center" prop="deptName" />
        <el-table-column label="客户名称" align="center" prop="customerName" show-overflow-tooltip />
        <el-table-column label="职位" align="center" prop="position" />
        <el-table-column label="手机号码" align="center" prop="phone" />
        <el-table-column label="办公电话" align="center" prop="officePhone" />
        <el-table-column label="操作" align="center" width="160" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleDetail(scope.row)">详情</el-button>
            <el-button link type="info" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

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

    <!-- 联系人详情抽屉 -->
    <contact-detail ref="detailRef" @success="getList" />
    <!-- 新增联系人抽屉 -->
    <contact-add ref="addRef" @success="getList" />

    <router-view />
  </div>
</template>

<script setup name="ContactPerson">
import { ref, reactive, toRefs, getCurrentInstance, onMounted, onActivated } from 'vue';
import { useRouter } from 'vue-router';
import { listContact, delContact } from "@/api/customer/crmContact";
import ContactDetail from './detail.vue';
import ContactAdd from './add.vue';

const proxy = getCurrentInstance().proxy;
const contactTypeOptions = ref([
  { label: '公司职员', value: '1' },
  { label: '关系资源人', value: '2' }
]);
const router = useRouter();

const contactPersonList = ref([]);
const loading = ref(true);
const total = ref(0);
const detailRef = ref(null);
const addRef = ref(null);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    customerName: null,
    type: null,
    gender: null,
    phone: null
  }
});

const { queryParams } = toRefs(data);

/** 查询联系人列表 */
function getList() {
  loading.value = true;
  listContact(queryParams.value).then(response => {
    contactPersonList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  addRef.value.open();
}

/** 详情按钮操作 */
function handleDetail(row) {
  detailRef.value.open(row.id);
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _id = row.id;
  proxy.$modal.confirm('是否确认删除联系人编号为"' + _id + '"的数据项？').then(function() {
    return delContact(_id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);
}

.search-card {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding: 15px 20px;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

:deep(.el-form--inline) {
  display: flex;
  align-items: center;
}

.custom-form-item {
  margin-bottom: 0 !important;
  margin-right: 20px;
  :deep(.el-form-item__label) {
    padding-right: 12px;
    color: #606266;
  }
  :deep(.el-input), :deep(.el-select), :deep(.el-date-editor) {
    width: 220px;
  }
}

.search-btns-area {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: 90px;
  margin-top: 20px;
}

.table-card {
  margin-bottom: 20px;
  :deep(.el-card__header) {
    padding: 12px 20px;
    border-bottom: 1px solid #f1f5f9;
  }
  :deep(.el-card__body) { padding: 10px 20px 20px; }
}
:deep(*) {
  font-weight: 400 !important;
}

.list-title {
  font-size: 14px;
}
</style>
