<template>
    <!-- 对话框表单 -->
    <el-dialog
        custom-class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? baTable.form.operate == 'edit' ? '编辑' : '添加' : '无标题' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <el-form-item prop="productId" label="产品">
                        <el-select v-model="baTable.form.items!.productId" style="width: 100%">
                            <el-option v-for="item in productList" :label="item.productName" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="versionName" label="版本名称">
                        <el-input v-model="baTable.form.items!.versionName"></el-input>
                    </el-form-item>
                    <el-form-item prop="versionCode" label="版本号">
                        <el-input v-model="baTable.form.items!.versionCode"></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item prop="upgradeType" label="版本类型">
                                <el-select style="width: 100%" v-model="baTable.form.items!.upgradeType">
                                    <el-option label="固件" :value="0"></el-option>
                                    <el-option label="大屏" :value="1"></el-option>
                                    <el-option label="APP" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item prop="osType" label="操作系统">
                                <el-select style="width: 100%" v-model="baTable.form.items!.osType">
                                    <el-option label="Android" :value="0"></el-option>
                                    <el-option label="IOS" :value="1"></el-option>
                                    <el-option label="Harmony" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item prop="isForce" label="强制升级">
                                <el-select style="width: 100%" v-model="baTable.form.items!.isForce">
                                    <el-option label="否" :value="0"></el-option>
                                    <el-option label="是" :value="1"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
<!--                        <el-col :span="12">-->
<!--                            <el-form-item prop="isRelease" label="是否发布">-->
<!--                                <el-select style="width: 100%" v-model="baTable.form.items!.isRelease">-->
<!--                                    <el-option label="否" :value="0"></el-option>-->
<!--                                    <el-option label="是" :value="1"></el-option>-->
<!--                                </el-select>-->
<!--                            </el-form-item>-->
<!--                        </el-col>-->
                    </el-row>
                    <el-form-item prop="upgradeDescription" label="更新说明">
                        <el-input type="textarea" v-model="baTable.form.items!.upgradeDescription"></el-input>
                    </el-form-item>
                    <el-form-item prop="upgradePackageUrl" label="升级包">
                        <ossUploadItem v-model:data="baTable.form.items!.upgradePackageUrl" @success="uploadSuccess"></ossUploadItem>
                        <div style="font-size: 10px;color: #92969a">
                            仅限zip文件
                        </div>
                    </el-form-item>

                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">取消</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                           type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? '保存并编辑下一项' : '保存' }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import {ref, reactive, inject, onMounted, watch} from 'vue'
import type baTableClass from '/@/utils/baTable'
import {validatorInteger} from '/@/utils/validate'
import type {ElForm, FormItemRule} from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import {getOrgTree, getEmployees} from '/@/api/backend/auth/account'
import {authGroup, product, sysConfig} from '/@/api/controllerUrls'
import editor from '/@/components/editor/index.vue'
import request from "/@/api/request";
import {ElNotification} from "element-plus/es";
import {document} from '/@/api/controllerUrls'
import ossUploadItem from '/@/components/pictureUpload/ossUploadView.vue'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass


watch(()=>baTable.form.operate,(n)=>{
    if(n){
        getInitData()
    }
})

let productList = ref([])

function getInitData() {
    request.index(product, undefined, 'index').then(res => {
        productList.value = res.data.list
    })
}


function uploadSuccess(file:any){
    baTable.form.items!.upgradePackageSize = file.size
    baTable.form.items!.extensionName = file.fileType
}




const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    productId: [
        {
            required: true,
            message: '请选择产品',
            trigger: 'blur',
        },
    ],
    versionName: [
        {
            required: true,
            message: '请输入版本名称',
            trigger: 'blur',
        },
    ],
    productId: [
        {
            required: true,
            message: '请选择产品',
            trigger: 'blur',
        },
    ],
    upgradeType: [
        {
            required: true,
            message: '请选择版本类型',
            trigger: 'blur',
        },
    ],
    osType: [
        {
            required: true,
            message: '请选择操作系统类型',
            trigger: 'blur',
        },
    ],
    versionCode: [
        {
            required: true,
            message: '请输入版本号',
            trigger: 'blur',
        },
        {validator: validatorInteger, trigger: 'blur'}
    ],
    upgradeDescription: [
        {
            required: true,
            message: '请输入升级说明',
            trigger: 'blur',
        },
    ],
    upgradePackageUrl: [
        {
            required: true,
            message: '请上传升级包',
            trigger: 'blur',
        },
    ],
    isForce: [
        {
            required: true,
            message: '请选择是否强制升级',
            trigger: 'blur',
        },
    ],
    // isRelease: [
    //     {
    //         required: true,
    //         message: '请选择是否发布',
    //         trigger: 'blur',
    //     },
    // ],
})


</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--color-sub-1);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}

.avatar-uploader:hover {
    border-color: var(--color-primary);
}

.avatar {
    width: 110px;
    height: 110px;
    display: block;
}

.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
