<template>
  <div>
    <Breadcrumb :items="['项目中心', '合同管理']" :title="'合同管理'">
    </Breadcrumb>
    <div class="container">
      <a-space direction="vertical" :size="16" fill>
        <div class="space-unit">
          <DataOverview />
        </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="filterContract()"
                          @clear="filterContract()"
                          @keyup.enter="filterContract()"
                        />
                      </a-form-item>
                    </a-col>
                    <a-col :xxl="6" :xl="8" :lg="12" :md="12" :sm="24">
                      <a-form-item
                        field="customerName"
                        label="客户名称"
                        row-class="mb0"
                        ><a-input-search
                          v-model="filterForm.customerName"
                          placeholder="根据客户名称模糊匹配"
                          allow-clear
                          @search="filterContract()"
                          @clear="filterContract()"
                          @keyup.enter="filterContract()"
                        />
                      </a-form-item>
                    </a-col>
                    <a-col
                      v-if="expandForm"
                      :xxl="6"
                      :xl="8"
                      :lg="12"
                      :md="12"
                      :sm="24"
                    >
                      <a-form-item
                        field="prjName"
                        label="项目名称"
                        row-class="mb0"
                        ><a-input-search
                          v-model="filterForm.prjName"
                          placeholder="根据项目名称模糊匹配"
                          allow-clear
                          @search="filterContract()"
                          @clear="filterContract()"
                          @keyup.enter="filterContract()"
                        />
                      </a-form-item>
                    </a-col>
                    <a-col
                      v-if="expandForm"
                      :xxl="6"
                      :xl="8"
                      :lg="12"
                      :md="12"
                      :sm="24"
                    >
                      <a-form-item
                        field="handlerId"
                        label="签订人"
                        row-class="mb0"
                      >
                        <a-select
                          v-model="filterForm.handlerId"
                          placeholder="全部"
                          :allow-search="true"
                          :allow-clear="true"
                          @change="filterContract()"
                        >
                          <a-option
                            v-for="{ id, name } in accountList"
                            :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="handlerId"
                        label="签订时间"
                        row-class="mb0"
                      >
                        <a-range-picker
                          v-model="filterForm.date"
                          style="width: 100%"
                          format="YYYY-MM-DD"
                          @change="filterContract()"
                        />
                      </a-form-item>
                    </a-col>

                    <a-col :xxl="12" :xl="24" :lg="12">
                      <a-form-item hide-label>
                        <a-space>
                          <a-button type="outline" @click="showContractModal()">
                            <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="contractListRef"
                :is-auth-customer="isAuthCustomer"
                :filter-form="filterForm"
                :account-list="accountList"
                :customer-list="customerList"
              ></List>
            </a-card>
          </a-col>
        </a-row>
      </a-space>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted, computed } from 'vue'
import { FormInstance } from '@arco-design/web-vue/es/form'
import { Customer as CustomerApi } from '@/enums/api'
import { getCustomerList } from '@/api/biz/customer'
import { getStaffList } from '@/api/base/staff'
import { useUserStore } from '@/store'
import DataOverview from './components/statistics.vue'
import List from './components/List.vue'

/**
 * 条件筛选表单
 */
const filterForm = reactive<{
  name?: string
  customerName?: string
  prjName?: string
  handlerId?: string
  date?: string[]
}>({
  name: undefined,
  customerName: undefined,
  prjName: undefined,
  handlerId: undefined,
  date: []
})

const expandForm = ref(false)
const contractListRef = ref<any>(null)
const accountList = ref<{ id: string; name: string }[]>([]) // 分组树
const customerList = ref<{ id: string; name: string }[]>([]) // 分组树
const filterFormRef = ref<FormInstance>()

const userStore = useUserStore()
const { permissionsList } = userStore

const filterContract = () => {
  contractListRef.value?.filterContract()
}

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

// 是否授权客户信息
const isAuthCustomer = computed(() => {
  return (
    permissionsList?.findIndex(
      item => `api/${item}` === CustomerApi.BIZ_CUSTOMER_LIST
    ) !== -1
  )
})

/**
 * 显示添加modal
 */
const showContractModal = async () => {
  contractListRef.value?.showContractModal()
}

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

/**
 * 获取客户列表
 */
const getCustomers = async () => {
  if (isAuthCustomer.value) {
    try {
      const { list } = await getCustomerList({ page_no: 1, page_size: 9999 })
      customerList.value = list
    } catch (e) {
      console.error(e)
    }
  }
}

onMounted(() => {
  filterContract()
  getAccounts().then(() => console.log('获取相关负责人账号列表'))
  getCustomers().then(() => console.log('获取客户列表'))
})
</script>

<script lang="ts">
export default {
  name: 'Contract'
}
</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>
