<template>
    <HcCard>
        <template #header>
            <span class="mr-2">任务分类:</span>
            <el-button _icon hc-btn size="small" type="primary" @click="openTypeEdit(1)">
                <HcIcon name="add"/>
            </el-button>
        </template>
        <HcTable :column="tasktableColumn" :datas="tasktableData" :loading="tableLoaing">
            <template #dictValue="{row}">
                        <div >{{row?.dictValue==='1'?'固定任务':'临时任务'}}</div>
            </template>
            <template #action="{row, index}">
                <el-button size="small" type="primary" @click="openTypeEdit(2,row)">编辑</el-button>
                <el-button size="small" type="primary" @click="delTasktype(row)">删除</el-button>
            </template>
        </HcTable>
     

        <!--任务类型弹窗-->
        <HcDialog bgColor="#ffffff" isToBody widths="24rem" :show="tasktypeModal" :title="taskTypeTitle" @close="taskTypeModalClose" @save="saveparentClick">
            <el-form label-position="top" :model="formtaskType" label-width="auto" size="large">
                <el-form-item label="任务类型名称">
                    <el-input v-model="formtaskType.dictName"/>
                </el-form-item>
                <el-form-item label="类型">
                    <el-select v-model="formtaskType.dictValue" class="block" placeholder="请选择">
                        <el-option v-for="item in typeoptions" :key="item.value" :label="item.label" :value="item.value"/>
                    </el-select>
                </el-form-item>
            </el-form>
        </HcDialog>

    </HcCard>
</template>

<script setup>
import {onMounted, ref, watch} from "vue";
import {submitDictionary,removeDictionary,getParentList,getChildList} from '~api/system/parameter.js';
import {getArrValue} from "js-fast-way"
onMounted(() => {
    getParentListData()
   
})
const props = defineProps({
    cur: {
        type: [String,Number],
        default: ''
    },
    type:{
        type: [String,Number],
        default: ''
    }
})

const tabsKey = ref(props.cur)
const tabsType = ref(props.type)

//监听
watch(() => [
    props.cur,
    props.type,
], ([key,type]) => {
    tabsKey.value = key
    tabsType.value = type
    console.log(key)
})


const tasktableColumn = [
    {key: 'dictName', name: '任务类型名称'},
    {key: 'dictValue', name: '类型'},
    {key: 'action', name: '操作', width: 200}
]
const tasktableData = ref([])

const taskTypeTitle = ref('')
const tasktypeModal = ref(false)
const editItem=ref({})
const openTypeEdit = (type,row) => {
    if (type === 1) {
        taskTypeTitle.value = '新增任务'
        formtaskType.value={}
        editItem.value={}
    } else {
        taskTypeTitle.value = '编辑任务'
        editItem.value=row
        formtaskType.value=row
    }
    tasktypeModal.value = true
}

//新增一级科目
const saveparentClick=async()=>{
    const { error, code, data,msg } = await submitDictionary({
        type:tabsType.value,
        dictValue:formtaskType.value?.dictValue,
        dictName:formtaskType.value?.dictName,
        id:editItem.value.id||null
      
    })
    tasktypeModal.value=false
    if (!error && code === 200) {
        window.$message?.success(msg)
        getParentListData()
    } 
    else {
        window.$message?.warning(msg)
    }
}
const tableLoaing=ref(false)
const getParentListData=async()=>{
    tableLoaing.value=true
    const { error, code, data,msg } = await getParentList({
        type:tabsType.value,
    })
    tableLoaing.value=false
    if (!error && code === 200) {
        tasktableData.value = getArrValue(data['records'])
       
    } 
    else {
        tasktableData.value=[]
        window.$message?.warning(msg)
    }
}
const formtaskType = ref({})
const typeoptions = ref([
    {
        value: '1',
        label: '固定任务',
    },
    {
        value: '2',
        label: '临时任务',
    },
])

const taskTypeModalClose = () => {
    tasktypeModal.value = false
}

const delTasktype = (item) => {
    window?.$messageBox?.alert('您确定要删除该任务信息吗? 一旦注销数据将彻底清除，请谨慎操作？', '删除提醒', {
            showCancelButton: true,
            confirmButtonText: '确认注销',
            cancelButtonText: '取消',
            type: 'warning',
            callback: async(action) => {
                if (action === 'confirm') {
                        const {error, code, msg} = await removeDictionary({
                            ids: item?.id,
                        })
                        if (!error && code === 200) {
                            window?.$message?.success('删除成功')
                            getParentListData()
                        } else {
                            window?.$message?.warning(msg)
                        }
                }
            }
        })
}
</script>

<style scoped lang="scss">

</style>
