<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="wxmenuname">
              <el-input v-model="state.form.wxmenuname" placeholder="微信菜单名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信菜单类型" prop="wxmenuntype">
              <el-radio-group v-model="state.form.wxmenuntype">
                <el-radio :value="1" :label="1">1</el-radio>
                <el-radio :value="0" :label="0">2</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信菜单KEY" prop="wxmenukey">
              <el-input v-model="state.form.wxmenukey" placeholder="菜单KEY" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信菜单url链接" prop="wxmenuurl">
              <el-input v-model="state.form.wxmenuurl" placeholder="微信菜单url链接" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信菜单media_id" prop="wxmenumediaid">
              <el-input v-model="state.form.wxmenumediaid" placeholder="微信菜单media_id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小程序appid" prop="wxmenuappid">
              <el-input v-model="state.form.wxmenuappid" placeholder="微信菜单小程序appid" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小程序页面路径" prop="wxmenupagepath">
              <el-input v-model="state.form.wxmenupagepath" placeholder="微信菜单小程序页面路径" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布article_id" prop="wxmenuarticleid">
              <el-input v-model="state.form.wxmenuarticleid" placeholder="微信菜单发布后获得的合法article_id" />
            </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 wxmenuStoreApi from '@store/modules/wxmenuStoreApi'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'

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

const state = reactive({
  // 弹窗开关
  modalVisible: false,
  // 弹窗标题
  dialogTitle: '',
  // 详情
  editView: {},
  // 表单
  form: {
    wxmenuname: '',
    wxmenuntype: '',
    wxmenuurl: '',
    wxmenumediaid: '',
    wxmenuappid: '',
    wxmenupagepath: '',
    wxmenuarticleid: '',
    wxmenukey: ''
  },
  // 验证表单
  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.productname}】` : '新增'
}

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

defineExpose({
  openDialog
})
</script>
