<template>
  <el-dialog v-model="props.modelValue" title="选择客户" width="900px" @closed="closeDialog" @open="openDialog">

    <el-form inline :model="queryParams">
      <el-form-item label="客户名称">
        <el-input v-model='queryParams.customerNickname' placeholder='请输入客户名称'></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="tableRef.reload()" icon="ElIconSearch">
          搜索
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 表格渲染  prop="对应的字段"-->
    <el-table v-loading="loading" :data="customersList" ref="tableRef" border highlight-current-row
      @row-click="handleRowClick">
      <el-table-column prop="customerNickname" label="客户名称" align="left" :show-overflow-tooltip="true" />
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getCustomerListFun" />

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleEditClick()">添加</el-button>
        <el-button @click="handleResetClick()">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch, nextTick } from 'vue';
import { ElMessage } from 'element-plus'
import { getCustomerWithOutBindList } from '@/api/business/ShopManager/Shops/shop.js';

// 业务参数
const total = ref(0)
const loading = ref(false)

// 基础参数
const tableRef = ref(null);
// 查询参数
const queryParams = reactive({
  customerNickname: "",
  pageNum: 1,
  pageSize: 10
});

const emits = defineEmits(['update:modelValue']);
const props = defineProps({
  modelValue: Boolean,
  data: Object,
});
const formData = ref({
  ...props.data
});

let customersList = ref([]);
// 打开窗口时运行
const openDialog = async () => {
  await getCustomerListFun()
}

const getCustomerListFun = async () => {
  loading.value = true
  await getCustomerWithOutBindList(queryParams).then((res) => {
    if (res.code == 200) {
      customersList.value = res.data.result
      total.value = res.totalNum
      loading.value = false
    }
  })
}

watch(props, (v) => {
  formData.value = v.data;
});

// -业务方法
let customerNickname = ref("");
let customerGuid = ref("");


// -基础方法

// 点击行
const handleRowClick = (row) => {
  customerNickname.value = row.customerNickname
  customerGuid.value = row.customerGuid
}

const closeDialog = () => {
  customerNickname.value = ""
  emits('update:modelValue', false);
};

// 提交 
const handleEditClick = async () => {
  if (customerNickname.value !== "") {

    formData.value.customerNickname = customerNickname.value
    formData.value.shopCustomerGuid = customerGuid.value
    if (formData.value.customerNickname != null) {
      closeDialog();
    }
  } else {
    ElMessage.error("请选择客户")
    return;
  }
};

const handleResetClick = async formEl => {

};
</script>

