<template>
  <el-dialog :title="title" v-model="dialogVisible" width="1200px" append-to-body>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px">
      <el-form-item label="供应商编码" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入供应商编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="Plus" @click="handleAdd" v-hasPermi="['ware:wmsSupplier:add']">新增供应商</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商编码" align="center" prop="supplierCode" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="状态" align="center" prop="status" >
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />

    <!-- 新增供应商对话框 -->
    <el-dialog :title="addTitle" v-model="addOpen" width="500px" append-to-body>
      <el-form ref="supplierRef" :model="addForm" :rules="addRules" label-width="100px">
        <el-form-item label="供应商编码" prop="supplierCode">
          <el-input v-model="addForm.supplierCode" placeholder="请输入供应商编码" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="addForm.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="addForm.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="addForm.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="addForm.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addForm.remark" type="textarea" placeholder="请输入备注内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitAddForm">确 定</el-button>
          <el-button @click="handleAddCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="SupplierSelectDialog">
import { ref, reactive, watch } from 'vue'
import { listWmsSupplier, addWmsSupplier } from "@/api/ware/wmsSupplier"
const { proxy } = getCurrentInstance()

const { sys_normal_disable } = proxy.useDict('sys_normal_disable')
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '选择供应商'
  }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel'])

// 本地对话框可见性状态
const dialogVisible = ref(props.visible)

// 监听props变化，同步本地状态
watch(() => props.visible, (newVal) => {
  dialogVisible.value = newVal
})

// 监听本地状态变化，通知父组件
watch(dialogVisible, (newVal) => {
  emit('update:visible', newVal)
})

// 供应商列表
const supplierList = ref([])
// 加载状态
const loading = ref(false)
// 总条数
const total = ref(0)
// 选中的供应商
const selectedSuppliers = ref([])

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  supplierCode: null,
  supplierName: null,
  contactPerson: null,
  contactPhone: null
})

// 加载供应商列表
const handleQuery = () => {
  loading.value = true
  listWmsSupplier(queryParams).then(response => {
    supplierList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 重置查询参数
const resetQuery = () => {
  queryParams.supplierCode = null
  queryParams.supplierName = null
  queryParams.contactPerson = null
  queryParams.contactPhone = null
  queryParams.pageNum = 1
  handleQuery()
}

// 供应商选择变化
const handleSelectionChange = (selection) => {
  selectedSuppliers.value = selection
}

// 确认选择
const handleConfirm = () => {
  if (selectedSuppliers.value.length === 0) {
    return
  }
  emit('confirm', selectedSuppliers.value)
  dialogVisible.value = false
}

// 取消选择
const handleCancel = () => {
  emit('cancel')
  dialogVisible.value = false
}

// 新增供应商相关状态
const addOpen = ref(false)
const addTitle = ref("")

const addForm = reactive({
  supplierId: null,
  supplierCode: null,
  supplierName: null,
  contactPerson: null,
  contactPhone: null,
  address: null,
  status: null,
  remark: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null
})

// 新增供应商表单验证规则
const addRules = reactive({
  supplierCode: [
    { required: true, message: "供应商编码不能为空", trigger: "blur" }
  ],
  supplierName: [
    { required: true, message: "供应商名称不能为空", trigger: "blur" }
  ],
})

// 打开新增供应商对话框
function handleAdd() {
  resetAddForm()
  addOpen.value = true
  addTitle.value = "添加供应商"
}

// 关闭新增供应商对话框
function handleAddCancel() {
  addOpen.value = false
  resetAddForm()
}

// 重置新增供应商表单
function resetAddForm() {
  Object.assign(addForm, {
    supplierId: null,
    supplierCode: null,
    supplierName: null,
    contactPerson: null,
    contactPhone: null,
    address: null,
    status: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  })
  proxy.resetForm("supplierRef")
}

// 提交新增供应商表单
function submitAddForm() {
  proxy.$refs["supplierRef"].validate(valid => {
    if (valid) {
      addWmsSupplier(addForm).then(response => {
        proxy.$modal.msgSuccess("新增成功")
        addOpen.value = false
        handleQuery() // 刷新供应商列表
      })
    }
  })
}

// 暴露方法
defineExpose({
  handleQuery
})
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>