<template>
  <div class="aimodeltc-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="aimodel-dialog"
      draggable
    >
      <el-form ref="aimodeltcRef" :model="state.form" :rules="state.rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="分类名称" prop="productclassname">
              <el-input v-model="state.form.productclassname" placeholder="产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类是否可用" prop="productclasssfky">
              <el-radio-group v-model="state.form.productclasssfky">
                <el-radio :value="1" :label="1">可用</el-radio>
                <el-radio :value="0" :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类排序" prop="productclasssort">
              <el-input v-model="state.form.productclasssort" placeholder="产品排序" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import sortStoreApi from '@store/modules/sortStoreApi'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'

// 接口封装文件
const productManageStore = sortStoreApi()
// 全局变量
const {proxy} = getCurrentInstance()
// 验证form
const aimodeltcRef = ref(null)

const state = reactive({
  // 弹窗开关
  modalVisible: false,
  // 弹窗标题
  dialogTitle: '',
  // 详情
  editView: {},
  // 表单
  form: {
    productclassname: '',
    productclasssfky: '',
    productclasssort: ''
  },
  // 验证表单
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  }
})

const emits = defineEmits(['refresh'])

// 确定弹窗回调
const submitDialog = () => {
  aimodeltcRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        let params = JSON.parse(JSON.stringify(state.form))
        params['productclassid.value'] = state.editView.productclassid
        await productManageStore.up(params)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        let params = JSON.parse(JSON.stringify(state.form))
        await productManageStore.add(params)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      aimodeltcRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

// 关闭弹窗
const cancelDialog = () => {
  state.editView = {}
  aimodeltcRef.value.resetFields()
  state.modalVisible = false
}

// 打开弹窗
const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.productclassname}】` : '新增'
}

// 申请详情
const getDataView = (data) => {
  nextTick(async () => {
    const result = await productManageStore.view({'productclassid.value': data.productclassid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

defineExpose({
  openDialog
})
</script>
