<template>
  <Dialog title="采购审核" v-model="dialogVisible" width="800px">
    <Descriptions :data="form" :schema="schema" :columns="3" />
    <el-form v-if="canEdit" :model="form" ref="auditForm" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-form-item label="审核" prop="isPass">
            <el-radio-group v-model="form.isPass">
              <el-radio :label="true">通过</el-radio>
              <el-radio :label="false">驳回</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-form-item label="备注" prop="auditRemark">
            <Editor v-model:modelValue="form.auditRemark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :disabled="formLoading" type="primary" @click="handleSave">保 存</el-button>
      </span>
    </template>
  </Dialog>
</template>

<script setup name="DialogAuditPurchase">
import { auditPurchase } from '@/api/mall/purchase'
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示

const form = ref({})
const canEdit = ref(true)

const schema = ref([])

const open = (val, flag) => {
  dialogVisible.value = true
  form.value = { ...val }
  canEdit.value = flag
  resetSchema()
  if (!canEdit.value) {
    const arr = [
      {
        field: 'auditStatusName',
        label: '采购状态'
      },
      {
        field: 'checkUserName',
        label: '审核人'
      },
      {
        field: 'checkTime',
        label: '审核时间'
      },
      {
        field: 'auditRemark',
        label: '审核备注',
        isEditor: true,
        span: 3
      }
    ]
    schema.value = [...schema.value, ...arr]
  } else {
    form.value.isPass = true
    form.value.auditRemark = ''
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const resetSchema = () => {
  schema.value = [
    {
      field: 'productName',
      label: '产品名称'
    },
    {
      field: 'specsName',
      label: '规格名称'
    },
    {
      field: 'supplier',
      label: '供应商'
    },
    {
      field: 'num',
      label: '采购数量'
    },
    {
      field: 'unitPrice',
      label: '采购单价'
    },
    {
      field: 'totalPrice',
      label: '总金额'
    },
    {
      field: 'warehouseName',
      label: '仓库'
    },
    {
      field: 'applyUserName',
      label: '申请人'
    },
    {
      field: 'applyTime',
      label: '申请时间',
      dateFormat: 'YYYY-MM-DD'
    },
    {
      field: 'remark',
      label: '备注',
      isEditor: true,
      span: 3
    }
  ]
}

const formLoading = ref(false)
const emit = defineEmits(['getList'])
async function handleSave() {
  try {
    formLoading.value = true
    await auditPurchase(form.value)
    message.success('审核完成')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('getList')
  } finally {
    formLoading.value = false
  }
}
</script>

<style scoped></style>
