<template>
  <div class="main animate__animated animate__backInRight" v-loading="loading">
    <custom-search-bar :fields="fields.filter((f) => f.isSearch)" v-model="searchForm" @search="search" @reset="toReset"></custom-search-bar>
    <custom-button-bar :fields="buttonFields" @add="toAdd"></custom-button-bar>
    <custom-table :data="data" :fields="fields" @query="search" :total="total" :opera="opera" @dict="toDict" @edit="toEdit" @delete="toDelete"></custom-table>
    <el-dialog v-model="dialog.show" :title="dialog.title" :destroy-on-close="false" @close="toClose" :top="dialog.top">
      <el-row>
        <el-col :span="24" v-if="dialog.type == '1'">
          <custom-form v-model="form" :fields="formFields" :rules="rules" @save="toSave">
            <template #is_use>
              <el-radio v-for="i in isUseList" :key="i._id" :label="i.value">{{ i.label }}</el-radio>
            </template>
          </custom-form>
        </el-col>
        <el-col :span="24" v-if="dialog.type == '2'">
          <dictData></dictData>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup>
import dictData from '@/views/system/dictData/index.vue'
const $checkRes = inject('$checkRes')
import { cloneDeep, get } from 'lodash-es'
const { t } = useI18n()
// 接口
import { DictTypeStore } from '@/store/api/system/dictType'
import { DictDataStore } from '@/store/api/system/dictData'
const store = DictTypeStore()
const dictDataStore = DictDataStore()
const data = ref([])
const searchForm = ref({})
const fields = [
  { label: t('pages.dict.title'), model: 'title', isSearch: true },
  { label: t('pages.dict.code'), model: 'code', isSearch: true },
  { label: t('pages.dict.is_use'), model: 'is_use', format: (i) => getDict(i) },
  { label: t('pages.dict.remark'), model: 'remark' }
]
const opera = [
  { label: t('common.dict'), method: 'dict' },
  { label: t('common.update'), method: 'edit' },
  { label: t('common.delete'), method: 'delete', confirm: true, type: 'danger' }
]
const buttonFields = [{ label: t('common.add'), method: 'add' }]
let skip = 0
let limit = inject('limit')
const total = ref(20)
const isUseList = ref([])
// 加载中
const loading = ref(false)
const formFields = [
  { label: t('pages.dict.title'), model: 'title' },
  { label: t('pages.dict.code'), model: 'code' },
  { label: t('pages.dict.is_use'), model: 'is_use', type: 'radio' },
  { label: t('pages.dict.remark'), model: 'remark', type: 'textarea' }
]
const rules = reactive({ title: [{ required: true, message: t('pages.dict.titleMessage'), trigger: 'blur' }], code: [{ required: true, message: t('pages.dict.codeMessage'), trigger: 'blur' }] })
const dialog = ref({ type: '1', show: false, title: t('pages.dict.dialogTitle'), top: '15vh' })
const form = ref({})
// 请求
onMounted(async () => {
  loading.value = true
  await searchOther()
  await search({ skip, limit })
  loading.value = false
})

const searchOther = async () => {
  const result = await dictDataStore.query({ code: 'isUse', is_use: '0' })
  if ($checkRes(result)) isUseList.value = result.data
}
const search = async (query = { skip: 0, limit }) => {
  const info = { skip: query.skip, limit: query.limit, ...searchForm.value }
  const res = await store.query(info)
  if (res.errcode == '0') {
    data.value = res.data
    total.value = res.total
  }
}
// 字典数据转换
const getDict = (data) => {
  const res = isUseList.value.find((f) => f.value == data)
  return get(res, 'label')
}
// 添加
const toAdd = () => {
  dialog.value = { type: '1', show: true, title: t('pages.dict.addDialogTitle'), top: '15vh' }
}
// 字典数据
const toDict = (data) => {
  form.value = data
  dialog.value = { type: '2', show: true, title: `【${data.title}】 ` + t('pages.dictData.codeDialogTitle'), top: '5vh' }
}
// 修改
const toEdit = (data) => {
  form.value = data
  dialog.value = { type: '1', show: true, title: t('pages.dict.upDialogTitle'), top: '15vh' }
}
// 删除
const toDelete = async (data) => {
  const res = await store.del(data._id)
  if ($checkRes(res, true)) {
    search({ skip: 0, limit })
  }
}
const toSave = async () => {
  const data = cloneDeep(form.value)
  let res
  if (get(data, '_id')) res = await store.update(data)
  else res = await store.create(data)
  if ($checkRes(res, true)) {
    search({ skip: 0, limit })
    toClose()
  }
}
// 重置
const toReset = async () => {
  searchForm.value = {}
  await search({ skip, limit })
}
const toClose = () => {
  form.value = {}
  dialog.value = { show: false }
}
// provide
provide('isUseList', isUseList)
provide('codeInfo', form)
</script>
<style scoped lang="scss"></style>
