<template>
  <div class="app-container organization-manage">
    <!-- 搜索 -->
    <el-form :model="state.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="客户企业码" prop="customerqym">
        <el-input v-model="state.queryParams['customerqym.value']" placeholder="客户企业码" clearable
          @keyup.enter="handleQuery({ initPageIndex: true })" />
      </el-form-item>

      <!-- 按钮组件 -->
      <all-button :buttonType="['', '', 'query', 'refresh']" @addButton="accountEditRef.openDialog()"
        @queryButton="handleQuery({ initPageIndex: true })" @refreshButton="handleQuery({ initPageIndex: true })" />
    </el-form>
    <!-- 表格 -->
    <el-table v-loading="state.tableLoading" :data="state.tableData" row-key="id" style="flex: 1" stripe border
      table-layout="fixed" @cell-dblclick="tableCelldbl">
      <el-table-column align="center" prop="customerid" label="客户id" min-width="150" sortable></el-table-column>
      <el-table-column align="center" prop="customertel" label="客户手机" min-width="150"></el-table-column>
      <el-table-column align="center" prop="customerwxopenid" label="微信openid" min-width="150"></el-table-column>

      <el-table-column align="center" prop="customername" label="客户名称" min-width="150"></el-table-column>
      <el-table-column align="center" prop="customertype" label="客户类型" min-width="150">
        <template #default="scope">
          <span>{{ scope.row[scope.column.property] }} {{ scope.row.customertype1 ? `-${scope.row.customertype1}` : ''
            }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" prop="customersfky" label="客户是否可用" min-width="150"></el-table-column> -->
      <el-table-column align="center" prop="customersfrz" label="客户是否认证" min-width="150">
        <template #default="scope">
          <el-tag type="danger" v-if="scope.row[scope.column.property] == 1">已认证</el-tag>
          <el-tag v-if="scope.row[scope.column.property] == 0">未认证</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="customerissubscribe" label="客户是否关注" min-width="150">
        <template #default="scope">
          <el-tag v-if="scope.row[scope.column.property] == 1">已关注</el-tag>
          <el-tag v-if="scope.row[scope.column.property] == 0">没关注</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column align="center" prop="customerzjlx" label="客户证件类型" min-width="150"></el-table-column> -->
      <!-- <el-table-column align="center" prop="customerzjhm" label="客户证件号码" min-width="210"></el-table-column> -->
      <!-- <el-table-column align="center" prop="customerzjyxq" label="客户证件有效期" min-width="150"></el-table-column> -->
      <!-- <el-table-column align="center" prop="customerheadimgurl" label="客户微信头像" min-width="150"></el-table-column> -->
      <el-table-column align="center" prop="customerqym" label="客户企业码" min-width="150"></el-table-column>
      <el-table-column align="center" prop="customercreatedate" label="客户创建时间" min-width="150"></el-table-column>
      <el-table-column align="center" prop="customerupdate" label="客户最后更新时间" min-width="150"></el-table-column>

      <el-table-column align="center" :width="fitWidth" label="操作" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <div class="operation-box">
            <el-text class="operate-btn-item" type="primary" @click="handleDictInfo(scope.row)">客户资料</el-text>
            <el-text class="operate-btn-item" type="primary" @click="handleDictOrer(scope.row)">客户订单</el-text>
            <el-text class="operate-btn-item" type="primary" @click="handleDictUser(scope.row)">宽带客户</el-text>
            <!-- <el-text class="operate-btn-item" type="danger" @click="handleDictDel(scope.row)">删除客户</el-text> -->
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-box">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="paginaionConfig.query.pageindex" :page-sizes="paginaionConfig.sizeOptions"
        :page-size="paginaionConfig.query.rows" layout="total, sizes, prev, pager, next, jumper"
        :total="paginaionConfig.total">
      </el-pagination>
    </div>

    <paymentEdit width="50%" ref="accountEditRef" @refresh="handleQuery" />
  </div>
</template>

<script setup name="Account">
import prowlerStoreApi from '@store/modules/prowlerStoreApi'
import useSystemManageStore from '@store/modules/systemManage'
import paymentEdit from '../components/paymentEdit/index.vue'
import _ from 'lodash'
import usePagination from '@/hooks/usePagination'
import useFitColumn from '@/hooks/useFitColumn'
import tableCelldbl from '@/hooks/tableCelldbl'
const router = useRouter()

const { proxy } = getCurrentInstance()
const systemManageStore = useSystemManageStore()
const { paginaionConfig, handleSizeChange, handleCurrentChange } = usePagination(handleQuery)
const { fitWidth } = useFitColumn()
// 接口封装文件
const productManageStore = prowlerStoreApi()

const state = reactive({
  tableLoading: false,
  tableData: [],
  queryParams: {
    'customerqym.value': ''
  }
})

// components组件
const accountEditRef = ref(null)

// 角色组信息
const roleList = ref([])
provide('roleList', readonly(roleList))

// 重置搜索项
const resetButton = () => {
  state.queryParams = {}
  handleQuery({ initPageIndex: true })
}

// 默认信息
const getRoleList = async () => {
  const { Total, Data } = await systemManageStore.sysusergroupLoad({
    pageindex: 1,
    rows: 1000
  })
  roleList.value = Data
}

function handleQuery(config) {
  if (config?.initPageIndex) paginaionConfig.query.pageindex = 1
  getList(Object.assign(_.cloneDeep(paginaionConfig.query), state.queryParams))
}

/** 查询菜单列表 */
const getList = async (query) => {
  state.tableLoading = true
  const { Total, Data } = await productManageStore.list(query)
  paginaionConfig.total = Total
  state.tableData = Data
  state.tableLoading = false
}

// 查看详情
const handleDictInfo = async (query) => {
  router.push({ path: '/peoplesmanage/prowlerinfo', query: { customerid: query.customerid } })
}

const handleDictOrer = async (query) => {
  router.push({ path: '/orderesmanage/orderesmanage', query: { customerid: query.customerid } })
}

const handleDictUser = async (query) => {
  router.push({ path: '/kuandaiManage/kuandaiManage', query: { customerid: query.customerid } })
}

/** 删除 */
const handleDictDel = async (query) => {
  try {
    let params = JSON.parse(JSON.stringify(query))
    params['customerid.value'] = params.customerid
    params['customerid'] = params.customerid
    await proxy.$modal.confirm('请问确认删除吗？')
    await productManageStore.del({ 'customerid.value': params.customerid })
    proxy.$modal.msgSuccess('删除成功！')
    handleQuery()
  } catch (error) {
    console.log(error, 'error')
  }
}

onMounted(() => {
  handleQuery({ initPageIndex: true })
  // 其他信息
  // getRoleList()
})
</script>
