<template>
  <div>
    <Breadcrumb :items="['营销中心', '客户管理']" :title="'客户管理'">
    </Breadcrumb>
    <div class="container">
      <a-space direction="vertical" :size="16" fill>
        <div class="space-unit">
          <Statistics />
        </div>
        <a-row :gutter="20" align="stretch">
          <a-col :span="24">
            <a-card class="general-card" style="min-height: 70vh">
              <template #title>
                <a-form
                  ref="filterFormRef"
                  :model="filterForm"
                  :label-col-props="{ span: 6 }"
                  :wrapper-col-props="{ span: 18 }"
                  label-align="right"
                  auto-label-width
                >
                  <a-row :gutter="[16, 16]">
                    <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24">
                      <a-form-item field="name" label="客户名称" row-class="mb0"
                        ><a-input-search
                          v-model="filterForm.name"
                          placeholder="根据客户名称模糊匹配"
                          allow-clear
                          @search="filterCustomer()"
                          @clear="filterCustomer()"
                          @keyup.enter="filterCustomer()"
                        />
                      </a-form-item>
                    </a-col>

                    <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24">
                      <a-form-item
                        field="levelId"
                        label="客户级别"
                        row-class="mb0"
                      >
                        <a-select
                          v-model="filterForm.levelId"
                          placeholder="全部"
                          :allow-clear="true"
                          @change="filterCustomer()"
                        >
                          <a-option
                            v-for="item in customerLevelList"
                            :key="item.id"
                            :value="item.id"
                            :label="item.name"
                          ></a-option>
                        </a-select>
                      </a-form-item>
                    </a-col>
                    <a-col
                      v-if="expandForm"
                      :xxl="6"
                      :xl="8"
                      :lg="12"
                      :md="12"
                      :sm="24"
                    >
                      <a-form-item
                        field="industryId"
                        label="所属行业"
                        row-class="mb0"
                      >
                        <a-select
                          v-model="filterForm.industryId"
                          placeholder="全部"
                          :allow-search="true"
                          :allow-clear="true"
                          @change="filterCustomer()"
                        >
                          <a-option
                            v-for="{ id, name } in industryList"
                            :key="id"
                            :value="id"
                            :label="name"
                          ></a-option>
                        </a-select>
                      </a-form-item>
                    </a-col>
                    <a-col
                      v-if="expandForm"
                      :xxl="6"
                      :xl="8"
                      :lg="12"
                      :md="12"
                      :sm="24"
                    >
                      <a-form-item
                        field="regionId"
                        label="所在地区"
                        row-class="mb0"
                      >
                        <a-cascader
                          v-model="filterForm.regionId"
                          style="width: 100%"
                          :options="regionTreeData"
                          :field-names="{
                            value: 'id',
                            label: 'name'
                          }"
                          :allow-search="false"
                          :allow-clear="true"
                          placeholder="全部"
                          check-strictly
                          @change="filterCustomer()"
                        />
                      </a-form-item>
                    </a-col>

                    <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24">
                      <a-form-item hide-label>
                        <a-space>
                          <a-button type="outline" @click="showCustomerModal()">
                            <template #icon>
                              <icon-plus />
                            </template>
                            新建客户
                          </a-button>
                          <a-button type="primary" @click="resetFilter()">
                            重置
                          </a-button>
                          <a-typography-text
                            type="primary"
                            style="cursor: pointer; font-size: 14px"
                            @click="expandForm = !expandForm"
                          >
                            {{ expandForm ? '收起' : '更多' }}
                            <icon-up v-if="expandForm" />
                            <icon-down v-else />
                          </a-typography-text>
                        </a-space>
                      </a-form-item>
                    </a-col>
                  </a-row>
                </a-form>
              </template>
              <a-divider style="margin-top: 0" />
              <List
                ref="customerListRef"
                :filter-form="filterForm"
                :region-tree-data="regionTreeData"
                :industry-list="industryList"
                :level-list="customerLevelList"
                :account-list="accountList"
              ></List>
            </a-card>
          </a-col>
        </a-row>
      </a-space>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { getCustomerLevelList, getIndustryTypeList } from '@/api/base/base'
import { DomainTree } from '@/types/domain'
import { FormInstance } from '@arco-design/web-vue/es/form'
import { getStaffList } from '@/api/base/staff'
import { getRegionTree } from '@/api/base/region'
import Statistics from './components/statistics.vue'
import List from './components/List.vue'

/**
 * 条件筛选表单
 */
const filterForm = reactive<{
  name?: string
  levelId?: string
  regionId?: number
  industryId?: string
}>({
  name: undefined,
  levelId: undefined,
  regionId: undefined,
  industryId: undefined
})

const expandForm = ref(false)
const filterFormRef = ref<FormInstance>()
const customerListRef = ref<any>(null)
const industryList = ref<{ id: string; name: string }[]>([])
const customerLevelList = ref<{ id: string; name: string }[]>([])
const regionTreeData = ref<DomainTree[]>([]) // 区域树
const accountList = ref<{ id: string; name: string }[]>([]) // 分组树

/**
 * 显示添加客户modal
 */
const showCustomerModal = async () => {
  customerListRef.value?.showCustomerModal()
}

const getIndustryType = async () => {
  const { list } = await getIndustryTypeList()
  industryList.value = list
}

const getLevels = async () => {
  const { list } = await getCustomerLevelList()
  customerLevelList.value = list
}

/**
 * 递归获取ng-tree需要的结构体
 * @param domains 接口返回的树状数组
 */
const getNodesTree = (domains: DomainTree): DomainTree => {
  domains.key = domains.id.toString()
  domains.title = domains.name
  if (domains.children && domains.children.length > 0) {
    domains.children.map(res => getNodesTree(res))
  }
  return domains
}

/**
 * 获取区域树
 */
const getTree = async () => {
  const { tree } = await getRegionTree()
  regionTreeData.value = tree.map(domains => {
    return getNodesTree(domains)
  })
}

const filterCustomer = () => {
  customerListRef.value?.filterCustomer()
}

const resetFilter = (): void => {
  filterFormRef.value?.resetFields()
  filterCustomer()
}

/**
 * 获取用户列表
 */
const getAccountList = async () => {
  try {
    const { list } = await getStaffList({ page_no: 1, page_size: 9999 })
    accountList.value = list
  } catch (e) {
    console.error(e)
  }
}

onMounted(async () => {
  try {
    getTree()
    getLevels()
    getIndustryType()
    getAccountList()
    filterCustomer()
  } catch (e) {
    console.log(e)
  }
})
</script>

<script lang="ts">
export default {
  name: 'Customer'
}
</script>

<style scoped lang="less">
.container {
  padding: 20px 20px 20px 20px;
  .general-card {
    :deep(.arco-card-header) {
      padding: 20px 20px 0 20px;
    }
  }
}
.dtree {
  padding: 0 10px;
  // max-height: 350px;
  :deep(.arco-tree-node-selected) {
    background-color: rgb(var(--gray-2));
  }
  :deep(.arco-tree-node:hover) {
    background-color: rgb(var(--gray-2));
  }
  :deep(.arco-tree-node-title:hover) {
    background-color: transparent;
  }
}

:deep(.arco-table-th) {
  &:last-child {
    .arco-table-th-item-title {
      margin-left: 16px;
    }
  }
}
</style>
