<template>
  <el-form
    :label-position="'right'"
    label-width="68px"
    :model="menuForm"
    :rules="formRules"
    ref="menuRef"
    style="width: 856px"
    class="menu_form"
  >
    <el-form-item label="菜单名称" prop="menu_name">
      <el-input v-model="menuForm.menu_name" show-word-limit maxlength="8" placeholder="请输入菜单名称" />
    </el-form-item>
    <el-form-item label="类型" prop="menu_type">
      <el-radio-group v-model="menuForm.menu_type">
        <el-radio :label="0">目录</el-radio>
        <el-radio :label="1">菜单</el-radio>
        <el-radio :label="2">子页面</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="分组" prop="dict_group_id" v-if="menuForm.menu_type == 0">
      <el-select v-model="menuForm.dict_group_id" placeholder="请选择分组">
        <el-option
          v-for="item in groupList"
          :key="item.dict_id"
          :label="item.name"
          :value="item.dict_id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="是否启用" prop="state">
      <el-switch
        class="bg-switch-ele"
        v-model="menuForm.state"
        :active-value="1"
        :inactive-value="0"
        inline-prompt
        active-text="是"
        inactive-text="否"
      />
    </el-form-item>
    <el-form-item label="菜单路径" prop="menu_url">
      <el-input v-model="menuForm.menu_url" placeholder="请输入菜单路径" />
    </el-form-item>
    <el-form-item label="菜单图标" prop="menu_logo" v-if="menuForm.menu_type !== 2">
      <div class="iconBox" :style="iconFlag ? {'border-color':'#2b4695'} : {} " @click="openIconbox">
        <div>
          <span>
            <bg-icon
              class="bgIcon"
              style="margin-right: 8px"
              :icon="'#' + menuForm.menu_logo"
            ></bg-icon>
            <span>{{menuForm.menu_logo.replace("bg-ic-","")}}</span>
          </span>
        </div>
        <div>
          <bg-icon
            class="bgIcon clearIcon"
            icon="#bg-ic-circle-close"
            v-if="menuForm.menu_logo != ''"
            @click.stop="clearIcon"
          ></bg-icon>
          <bg-icon
            class="bgIcon"
            :icon="iconFlag ? '#bg-ic-caret-top' : '#bg-ic-caret-bottom'"
          ></bg-icon>
        </div>
      </div>
      <div class="icon_box" v-show="iconFlag">
        <div class="input_box">
          <el-input
            placeholder="请输入内容"
            :prefix-icon="Search"
            v-model="icon_search"
            @input="searchIcon"
          >
          </el-input>
        </div>
        <div class="icon_content bg-scroll">
          <div class="icon_item" v-for="(e,i) in iconArr" :key="i+100" @click="changeIcon(e)">
            <span> 
              <bg-icon
                class="selectBgIcon"
                :icon="'#' + e.icon"
              ></bg-icon>
              {{e.name}}
            </span>
          </div>
        </div>
      </div>
    </el-form-item>
    
  </el-form>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { reactive, ref, onMounted, toRefs } from 'vue'
import axios from '../../../../request/http.js'
import { ElMessage } from 'element-plus'
import allIconArr from '@/assets/js/bg_icon.js'

const menuRef = ref(null)
const props = defineProps({
  data: {
    type: Object,
    default: null,
  }
})
const state = reactive({
  menuRef,
  menuForm: {
    menu_name: '',
    menu_type: 0,
    dict_group_id: "",
    state: 1,
    menu_url: "",
    menu_logo: "",
  },
  formRules: {
    menu_name: [
      { required: true, message: '请输入菜单名称', trigger: 'blur' },
    ],
    menu_type: [
      { required: true, message: '请选择菜单类型', trigger: 'change' },
    ],
    dict_group_id: [
      { required: true, message: '请选择菜单分组', trigger: 'change' },
    ],
    menu_url: [
    { required: true, message: '请输入菜单路径', trigger: 'blur' },
    ]
  },
  groupList: [],
  iconFlag: false,
  icon_search: "",
  iconArr: [],
})



const emit = defineEmits(['action'])

const submitForm = async () => {
  if (!state.menuRef) return
  await state.menuRef.validate((valid, fields) => {
    if (valid) {
      emit('action', state.menuForm)
    } else {
      emit('action',null)
    }
  })
}

const getGroupList = () => {
  axios
    .get(`/apaas/system/v5/dictionary/list?state=1&id=5`)
    .then((res) => {
      if (res.data.code == 200) {
        state.groupList = res.data.data || []
        console.log(state.groupList)
      }else {
        ElMessage.error(res.data.data)
      }
    }).catch((err) => {
      console.log(err)
    })
}
const openIconbox = () => {
  state.iconFlag = !state.iconFlag
}
const searchIcon = () => {

}
const clearForm = () => {
  if (!state.menuRef) return
  state.menuRef.resetFields()
} 
const setForm = (data) => {
  state.menuForm = Object.assign(state.menuForm, data);
}
const clearIcon = () => {
  state.menuForm.menu_logo = ""
}
const changeIcon = (e) => {
  console.log(e)
  state.menuForm.menu_logo = e.icon
  state.iconFlag = false
}
onMounted(() => {
  allIconArr.forEach(e => {
    state.iconArr.push({
      icon: e,
      name: e.replace("bg-ic-","")
    })
  })
})
const {
  menuForm,
  formRules,
  groupList,
  iconFlag,
  iconArr
} = toRefs(state)

onMounted(() => {
  getGroupList()
})


defineExpose({ submitForm, clearForm, setForm , menuForm, menuRef })
</script>

<style lang="scss" scoped>
.menu_form {
  .el-form-item {
    margin-bottom: 24px;
    :deep().el-form-item__label {
      padding: 0;
      margin-right: 8px;
    }
    .el-form-item__content {
      .el-input,
      .el-select {
        width: 100%;
      }
      .bg-switch-ele {
        width: 52px;
        height: 24px;
        :deep().el-switch__core {
          width: 100%;
          height: 24px;
          .el-switch__inner,
          .el-switch__action {
            top: 3px;
          }
        }
      }
      .iconBox {
        width: 100%;
        display: flex;
        padding: 0 12px;
        height: 36px;
        line-height: 36px;
        justify-content: space-between;
        border: 1px solid #dadee7;
        border-radius: 4px;
        cursor: pointer;
        .bgIcon {
          color: #202531;
          width: 8px;
          margin-top: 11px;
        }
        .clearIcon {
          margin-right: 8px;
          width: 14px;
          display: none;
        }
        &:hover,
        &:focus,
        &:active {
          border-color: #2b4695;
          .clearIcon {
            display: inline-block;
          }
        }
        &:hover {
          border-color: #a9b1c7;
        }
      }
      .icon_box {
        width: 100%;
        height: 236px;
        box-shadow: 0px 4px 12px 0px 
	      	rgba(18, 30, 63, 0.1);
	      border-radius: 4px;
	      border: solid 1px #e6e9ef;
        margin-top: 10px;
        padding: 12px 0px 12px 8px;
        .input_box {
          margin-right: 8px;
        }
        .icon_content {
          width: 100%;
          height: 168px;
          display: flex;
          flex-wrap: wrap;
          margin-top: 8px;
          .icon_item {
            display: inline-block;
            border: 1px solid #dadee7;
            border-radius: 3px;
            width: 182px;
            height: 36px;
            margin: 0 8px 8px 0;
            color: #202531;
            padding-left: 16px;
            line-height: 36px;
            cursor: pointer;
            &:hover,
            &:active {
              background-color: #eaedf5;
              border-color: #95a3ca;
            }
            &:nth-child(4n) {
              margin-right: 0;
            }
            .selectBgIcon {
              width: 14px;
              margin-right: 10px;
            }
          }
        }
      }
    }
  }
}
</style>