<!--
 * @Descripttion: (小程序客户/tb_customer)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-06-05)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-06-05)
-->
<template>
  <div class="app-container">
    <el-row :gutter="24">
      <!-- 搜索框 queryParams.需要搜索的字段 -->
      <el-form :model="queryParams" label-position="left" style="margin:15px;"  inline ref="queryForm" v-show="showSearch"
        @submit.prevent>
      <el-form-item label="小程序openid" prop="customerXcxOpenid">
        <el-input v-model="queryParams.customerXcxOpenid" placeholder="请输入小程序openid" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="昵称" prop="customerNickname">
        <el-input v-model="queryParams.customerNickname" placeholder="请输入昵称" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号" prop="customerMobilePhoneNumber">
        <el-input v-model="queryParams.customerMobilePhoneNumber" placeholder="请输入手机号" clearable @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="最后登录时间">
        <el-date-picker 
          v-model="dateRange" 
          style="width: 240px" 
          type="daterange" 
          @change="handleQuery"
          range-separator="-"
          start-placeholder="开始日期" 
          end-placeholder="结束日期" 
          placeholder="请选择最后登录时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          :shortcuts="dateOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
      </el-form>
    </el-row>

    <!-- 工具按钮 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:customer:addOrUpdate']" plain icon="plus" @click="AddDialogVisible = true">
          {{ $t('btn.add') }}
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:customer:delete']" plain icon="delete" @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['business:customer:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格渲染  prop="对应的字段"-->
    <el-table v-loading="loading" :data="dataList" ref="tableRef"  highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
	  
      <el-table-column prop="customerType" label="客户类型" align="center">
        <template #default="scope">
          <dict-tag :options=" customer_type " :value="scope.row.customerType" />
        </template>
      </el-table-column>
      <el-table-column prop="customerAvatar" label="头像" align="center">
        <template #default="scope">
          <el-image preview-teleported :hide-on-click-modal="true" lazy class="table-td-thumb" fit="contain" :src="scope.row.customerAvatar.split(',')[0]" :preview-src-list="scope.row.customerAvatar.split(',')">
            <div><el-icon :size="15"><document /></el-icon></div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="customerXcxOpenid" label="小程序openid" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="customerNickname" label="昵称" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="customerMobilePhoneNumber" label="手机号" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="customerGender" label="性别" align="center">
        <template #default="scope">
          <dict-tag :options=" sys_user_sex " :value="scope.row.customerGender" />
        </template>
      </el-table-column>
      <el-table-column prop="customerAvailableBalance" label="可用余额" align="center" sortable />
      <el-table-column prop="customerAvailablePoints" label="可用积分" align="center" sortable />
      <el-table-column prop="customerTotalPaymentAmount" label="总支付金额" align="center" sortable />
      <el-table-column prop="customerActualConsumptionAmount" label="实际消费金额" align="center" sortable />
      <el-table-column prop="customerLastLoginTime" width="200" label="最后登录时间" align="center" :show-overflow-tooltip="true" sortable />

      <el-table-column label="操作" width="250" fixed="right">
        <template #default="scope">
          <el-button type="primary" size="small" icon="edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:customer:addOrUpdate']">编辑</el-button>
          <!-- <el-button type="danger" size="small" icon="delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:customer:delete']">删除</el-button> -->
          <el-button size="small" icon="view" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
  
  <!-- 添加 -->
  <!-- <AddDialog v-model="AddDialogVisible" :done="() => resetQuery()"></AddDialog> -->
  <!-- 编辑 -->
  <EditDialog v-model="EditDialogVisible" :data="EditDialogRow" :done="() => resetQuery()"></EditDialog>
  <!-- 详情 -->
  <DetailDialog v-model="DetailDialogVisible" :data="DetailDialogRow" :done="() => resetQuery()"></DetailDialog>
</template>
<script setup name="customer">
import { ElMessageBox } from 'element-plus'
import modal from '@/plugins/modal.js'
import {  exportCustomer,    customerList , delCustomer } from '@/api/business/Custom/Customers/customer.js'
import AddDialog from "./components/AddDialog.vue";
import EditDialog from "./components/EditDialog.vue";
import DetailDialog from "./components/DetailDialog.vue";

const AddDialogVisible = ref(false);
const EditDialogVisible = ref(false);
const EditDialogRow = ref({});
const DetailDialogVisible = ref(false);
const DetailDialogRow = ref({});

const { proxy } = getCurrentInstance()

// 选中categoryId数组数组
const ids = ref([])
// 非单选禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 数据列表
const dataList = ref([])
// 总记录数
const total = ref(0)
// 是否加载
const loading = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
})
const { queryParams } = toRefs(data)
const dateRange = ref([])


// 业务参数


// 业务方法

// 字典获取
const sys_user_sex = ref([]);
async function getsys_user_sex() {
  await proxy.getDicts('sys_user_sex').then((res) => {
    sys_user_sex.value = res.data
  })
}
getsys_user_sex()

// 字典获取
const customer_type = ref([]);
async function getcustomer_type() {
  await proxy.getDicts('customer_type').then((res) => {
    customer_type.value = res.data
  })
}
getcustomer_type()



//基础方法

// 查询数据
function getList() {
  loading.value = true
  customerList(proxy.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    if (res.code == 200) {
      loading.value = false
      dataList.value = res.data.result
      total.value = res.data.totalNum
    }
  })
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.customerId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置查询操作 */
function resetQuery() {
  proxy.resetForm('queryForm')
  handleQuery()
}
/** 搜索按钮操作 */
function handleQuery() {
  getList()
}
/** 删除按钮操作 */
function handleDelete(row) {
  const Ids = row.customerId || ids.value

    ElMessageBox.confirm("是否确认删除？", "系统提示", {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: "warning",
    })
    .then(function () {
      return delCustomer(Ids)
    })
    .then(() => {
      handleQuery()
      modal.msgSuccess("删除成功")
    })
    .catch(() => { })
}

/** 导出按钮操作 */
function handleExport(row) {
  const Ids = row.customerId || ids.value
  const name = ref("所有")

  if (Ids.length != 0) {
    const ids = Object.values(Ids).map(value => String(value));
    const str = ids.join(',');
    queryParams.value.ids = str
    name.value = "选中"
  }

  ElMessageBox.confirm('是否确认导出' + name.value + '数据?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(function () {
      return exportCustomer(queryParams.value)
    })
    .then((response) => {
      proxy.download(response.data.path)
    })
}


// 修改
function handleUpdate(row) {
  EditDialogVisible.value = true
  EditDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailDialogVisible.value = true
  DetailDialogRow.value = row
}

handleQuery()
</script>
