<template>
    <el-dialog draggable v-model.trim="dialogVisible" :title="title" width="800px" :before-close="Cancel">
        <el-form :inline="false" :model="configStore" ref="formRef" label-width="auto" :rules="rules">
            <el-divider border-style="dashed">全局</el-divider>
            <div class="layout-config-global form-two">
                <el-form-item label="主题名称" prop="name">
                    <el-input maxlength="32" show-word-limit  v-model.trim="configStore.name" placeholder="请输入主题名称" />
                </el-form-item>
                <el-form-item label="后台页面切换动画">
                    <el-select v-model.trim="configStore.mainAnimation">
                        <el-option label="slide-right" value="slide-right"></el-option>
                        <el-option label="slide-left" value="slide-left"></el-option>
                        <el-option label="el-fade-in-linear" value="el-fade-in-linear"></el-option>
                        <el-option label="el-fade-in" value="el-fade-in"></el-option>
                        <el-option label="el-zoom-in-center" value="el-zoom-in-center"></el-option>
                        <el-option label="el-zoom-in-top" value="el-zoom-in-top"></el-option>
                        <el-option label="el-zoom-in-bottom" value="el-zoom-in-bottom"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="组件主题色">
                    <el-color-picker v-model.trim="configStore.elementUiPrimary[0]" />
                </el-form-item>
                <el-form-item label="表格标题栏背景颜色">
                    <el-color-picker v-model.trim="configStore.tableHeaderBackground[0]" />
                </el-form-item>
                <el-form-item label="表格标题栏文字颜色">
                    <el-color-picker v-model.trim="configStore.tableHeaderColor[0]" />
                </el-form-item>
                <el-form-item label="表格激活栏颜色">
                    <el-color-picker v-model.trim="configStore.tableCurrent[0]" />
                </el-form-item>
                <el-form-item label="组件主描述" prop="remark">
                    <el-input maxlength="32" show-word-limit 
                        v-model.trim="configStore.remark"
                        :autosize="{ minRows: 2, maxRows: 4 }"
                        type="textarea"
                        placeholder="请输入描述"
                    />
                </el-form-item>
            </div>
            <el-divider border-style="dashed">侧边栏</el-divider>
            <div class="layout-config-aside form-two">
                <el-form-item label="侧边菜单栏背景色">
                    <el-color-picker v-model.trim="configStore.menuBackground[0]" />
                </el-form-item>
                <el-form-item label="侧边菜单文字颜色">
                    <el-color-picker v-model.trim="configStore.menuColor[0]" />
                </el-form-item>
                <el-form-item label="侧边菜单激活项背景色">
                    <el-color-picker v-model.trim="configStore.menuActiveBackground[0]" />
                </el-form-item>
                <el-form-item label="侧边菜单激活项文字色">
                    <el-color-picker v-model.trim="configStore.menuActiveColor[0]" />
                </el-form-item>
                <el-form-item label="侧边菜单顶栏背景色">
                    <el-color-picker v-model.trim="configStore.menuTopBarBackground[0]" />
                </el-form-item>
            </div>

            <el-divider border-style="dashed">顶栏</el-divider>
            <div class="layout-config-aside form-two">
                <el-form-item label="顶栏背景色">
                    <el-color-picker v-model.trim="configStore.headerBarBackground[0]" />
                </el-form-item>
                <el-form-item label="顶栏文字色">
                    <el-color-picker v-model.trim="configStore.headerBarTabColor[0]" />
                </el-form-item>
                <el-form-item label="顶栏logo">
                    <el-image
                        style="height: 50px"
                        :src="logoFile.url"
                        :preview-src-list="[logoFile.url]"
                        v-if="logoFile.url"
                        class="mr10"
                    ></el-image>
                    <el-upload
                        action=""
                        :show-file-list="false"
                        :auto-upload="false"
                        accept=".png,.jpg"
                        :on-change="chooseImage"
                    >
                        <el-button type="primary">上传图片</el-button>
                    </el-upload>
                </el-form-item>
            </div>
        </el-form>

        <template #footer>
            <div class="dialog-footer">
                <el-button @click="Cancel">取消</el-button>
                <el-button type="primary" @click="onSubmit">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, inject } from 'vue'
import { reactive } from 'vue'
import router from '@/router/index'
import { ElMessage } from 'element-plus'
const dialogVisible = ref(false)
const title = ref('')
import { addTheme, updateTheme } from '@/api/system/subject/index'
const emit = defineEmits(['Cancels'])
const rules = {
    name: [{ required: true, message: '请输入主题名称', trigger: 'blur' }],
    remark: [{ required: true, message: '请输入主题描述', trigger: 'blur' }]
}
const configStore = ref({
    // 后台主页面切换动画，可选值<slide-right|slide-left|el-fade-in-linear|el-fade-in|el-zoom-in-center|el-zoom-in-top|el-zoom-in-bottom>
    mainAnimation: 'el-fade-in',
    elementUiPrimary: ['#0e8780', '#0e8780'],
    tableHeaderBackground: ['#F3F6F9', '#F3F6F9'],
    tableHeaderColor: ['#111', '#fff'],
    tableCurrent: ['#F3F6F9', '#F3F6F9'],
    /* 侧边菜单 */
    // 侧边菜单背景色
    menuBackground: ['#0e8780', '#1d1e1f'],
    // 侧边菜单文字颜色
    menuColor: ['#FFFFFF', '#CFD3DC'],
    // 侧边菜单激活项背景色
    menuActiveBackground: ['#0c7973', '#1d1e1f'],
    // 侧边菜单激活项文字色
    menuActiveColor: ['#00f5fd', '#3375b9'],
    // 侧边菜单顶栏背景色
    menuTopBarBackground: ['#0e8780', '#1d1e1f'],

    // 顶栏文字色
    headerBarTabColor: ['#FFFFFF', '#CFD3DC'],
    // 顶栏背景色
    headerBarBackground: ['#0e8780', '#1d1e1f'],

    logoFile: '',
    name: '',
    color: '',
    faviconFile: '',
    remark: ''
})

const logoFile = reactive({
    url: '',
    raw: ''
})
const formRef = ref()
const open = (e: any) => {
    formRef.value?.resetFields()
    title.value = e.text
    dialogVisible.value = true
    if (e.text == '修改主题') {
        let form = JSON.parse(JSON.stringify(e.row))
        for (let k in form) {
            if (
                k == 'elementUiPrimary' ||
                k == 'tableHeaderBackground' ||
                k == 'tableHeaderColor' ||
                k == 'tableCurrent' ||
                k == 'menuBackground' ||
                k == 'menuColor' ||
                k == 'menuActiveBackground' ||
                k == 'menuActiveColor' ||
                k == 'menuTopBarBackground' ||
                k == 'headerBarTabColor' ||
                k == 'headerBarBackground'
            ) {
                form[k] = JSON.parse(form[k])
            }
        }
        logoFile.url = form.logoUrl
        configStore.value = form
    }
}
// 确定主题
const onSubmit = () => {
    formRef.value.validate(async valid => {
        if (valid) {
            if (logoFile.url == '') {
                return ElMessage.error('请上传logo')
            }

            configStore.value.faviconFile = configStore.value.logoFile
            configStore.value.color = configStore.value.elementUiPrimary[0]
            let form = new FormData()
            for (let k in configStore.value) {
                if (
                    k == 'logoFile' ||
                    k == 'faviconFile' ||
                    k == 'name' ||
                    k == 'color' ||
                    k == 'remark' ||
                    k == 'mainAnimation' ||
                    k == 'id'
                ) {
                    form.append(k, configStore.value[k])
                } else if (k == 'logoUrl' || k == 'faviconUrl') {
                    if (configStore.value.logoFile == null) {
                        let str = configStore.value[k].match(/base64,([^"]+)/)[1]
                        let bin = atob(str)
                        let arr = new Array(bin.length)
                        for (let i = 0; i < bin.length; i++) {
                            arr[i] = bin.charCodeAt(i)
                        }
                        let b = new Uint8Array(arr)
                        const blob = new Blob([b], { type: 'image/jpeg' })
                        form.append('faviconFile', blob)
                        form.append('logoFile', blob)
                    }
                } else {
                    form.append(k, JSON.stringify(configStore.value[k]))
                }
            }
            // if(){

            // }

            if (title.value == '新增主题') {
                addTheme(form).then(res => {
                    ElMessage.success('新增成功')
                    Cancel()
                    setTimeout(() => {
                        router.go(0)
                    }, 500)
                })
            }
            if (title.value == '修改主题') {
                updateTheme(form).then(res => {
                    ElMessage.success('修改成功')
                    Cancel()
                    setTimeout(() => {
                        router.go(0)
                    }, 500)
                })
            }
        }
    })
}
// 取消
const Cancel = () => {
    // dialogVisible.value = false
    emit('Cancels')
}
/**
 * 选择图片上传
 * @param e
 */
const chooseImage = (e: any) => {
    // console.log('🚀 ~ chooseImage ~ e:', URL.createObjectURL(e.raw!))
    logoFile.url = URL.createObjectURL(e.raw!)
    configStore.value.logoFile = e.raw
    // uploadFile(e.raw, 'sgGovern/').then(res => {
    //     logoFile.name = res.data.name
    //     logoFile.url = res.data.url
    //     configStore.logoFile = res.data.name
    //     ElMessage.success('新增成功')
    // })
}
defineExpose({ open })
</script>
