<template>
  <el-dialog title="添加客户" v-model="visible" width="800px" height="400px" @close="close" append-to-body draggable>
    <el-form ref="dialogForm" class="master-container" :model="queryParams" :inline="true" style="align-items: center">
      <div style="display: flex">
        <el-form-item label="客户名称：" prop="companyName">
          <el-input v-model.trim="queryParams.companyName" type="text" @keydown.enter.prevent style="width: 120px"
            placeholder="请输入关键字" :clearable="true" @keyup.enter="handleSearch" />
        </el-form-item>
        <el-form-item label="助记码：" prop="mnemonicCode">
          <el-input v-model.trim="queryParams.mnemonicCode" type="text" @keydown.enter.prevent style="width: 120px"
            placeholder="请输入关键字" :clearable="true" @keyup.enter="handleSearch" />
        </el-form-item>
        <el-form-item>
          <el-button type="info" icon="Search" @click="handleSearch">搜索</el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-table ref="dialogTable" :data="dataList" size="small" v-loading="loading" border height="370px">
      <el-table-column type="index" label="行号" width="50" align="center" />
      <el-table-column label="公司编码" align="center" prop="companyCode" width="80" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="公司别名" align="center" prop="companyAlias" />
      <el-table-column label="助记码" align="center" prop="mnemonicCode" />
      <el-table-column label="操作" width="64" align="center">
        <template #default="scope">
          <el-button type="success" icon="finished" circle @click="handleSingleSelected(scope.row)" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </el-dialog>
</template>
<script setup>
import { listCompany } from "@/api/business/company";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { process_status } = proxy.useDict("process_status");
/** 工序变量 */
const total = ref(0);
const props = defineProps({
  singleSelected: {
    type: Function,
    default: null,
  },
});

const { singleSelected } = toRefs(props);
const dataList = ref([]);
const visible = ref(false);
const loading = ref(false);
const queryParams = ref({
  name: "",
  pageNum: 1,
  pageSize: 10,
});
const selections = ref([]);

/**
 * 对话框打开 事件
 */
function open() {
  visible.value = true;
  getList();
}

/**
 * 对话框关闭 事件
 */
function close() {
  proxy.$refs.dialogForm.resetFields();
  proxy.$refs.dialogTable.clearSelection();
  queryParams.value.pageNum = 1;
  visible.value = false;
}

/**
 * 加载数据
 */
function getList() {
  loading.value = true;
  queryParams.value.supplierId = props.supplierId;
  listCompany(queryParams.value).then((res) => {
    dataList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

/**
 * 列表checkbox列选择 事件
 */
function handleSelectionChange(selection) {
  selections.value = selection;
}

/**  搜索 事件 */
function handleSearch() {
  getList();
}

/** 单选事件 */
function handleSingleSelected(row) {
  if (singleSelected.value) {
    singleSelected.value(row);
  }
  close();
}

defineExpose({
  open,
});
</script>
