<template>
    <HcCard>
        <template #header>
            <div class="w-36">
                <el-select v-model="searchForm.departmentType" block clearable placeholder="选择部门" size="large">
                    <el-option v-for="item in department" :label="item.deptName" :value="item.deptCategory"/>
                </el-select>
            </div>
            <div class="w-36 ml-4">
                <el-date-picker class="block" v-model="searchForm.planStartDate" type="month" value-format="YYYY-MM" placeholder="开始日期" clearable size="large"/>
            </div>
            <div class="mx-2">~</div>
            <div class="w-36">
                <el-date-picker class="block" v-model="searchForm.planEndDate" type="month" value-format="YYYY-MM" placeholder="结束日期" clearable size="large"/>
            </div>
            <div class="ml-4">
                <el-button size="large" type="primary" @click="searchClick">
                    <HcIcon name="search-2"/>
                    <span>搜索</span>
                </el-button>
            </div>
            <div class="ml-2">
                <el-button size="large" @click="resetClick">
                    <HcIcon name="close-circle"/>
                    <span>重置</span>
                </el-button>
            </div>
        </template>
        <template #extra>
            <el-button size="large" type="primary" hc-btn @click="addRowClick">
                <HcIcon name="add"/>
                <span>新增预算计划</span>
            </el-button>
        </template>
        <HcTable :column="tableColumn" :datas="tableData" :loading="tableLoading">
            <template #planName="{row}">
                <span class="text-blue" @click="rowNameClick(row)">{{row.planName}}</span>
            </template>
            <template #key2="{row}">
                <span >{{row.planStartDate?row.planStartDate:''}}</span>
                <span  v-if="row.planStartDate">~</span>
                <span >{{row.planStartDate?row.planStartDate:''}}</span>
            </template>
            <template #planTotal="{row}">
                <span>{{row.planTotal}}条</span>
            </template>
            <template #finishPlanTotal="{row}">
                <span>{{row.finishPlanTotal}}条</span>
            </template>
            <template #beginPlanTotal="{row}">
                <span>{{row.beginPlanTotal}}条</span>
            </template>
            <template #action="{row,index}">
                <el-button size="small" type="primary" @click="editRowClick(row)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRowClick(row)">删除</el-button>
            </template>
        </HcTable>
        <template #action>
            <HcPages :pages="searchForm" @change="pageChange"/>
        </template>

        <!--选择计划所属月份-->
        <HcDialog bgColor="#ffffff" isToBody title="选择计划所属月份" widths="26rem" saveText="确认"
                  :show="monthModal" @close="monthCloseClick" @save="monthSaveClick"
        >
            <el-form ref="formMonthRef" label-position="top" size="large" :model="formMonthModel" :rules="formMonthRules">
                <el-form-item label="选择部门" prop="section">
                    <el-select v-model="formMonthModel.section" block placeholder="选择部门" @change="changeDpt">
                        <el-option v-for="item in department" :label="item.deptName" :value="item.deptCategory"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="选择月份" prop="month">
                    <el-date-picker type="month" class="block" v-model="formMonthModel.month" format="YYYY-MM" value-format="YYYY-MM"/>
                </el-form-item>
            </el-form>
        </HcDialog>

    </HcCard>
</template>

<script setup>
import {ref,onMounted,onActivated,watch} from "vue";
import {useRouter} from 'vue-router'
import sectionApi from '~api/program/section.js';
import {getArrValue,getObjValue,formValidate} from "js-fast-way"
import { getdepartmentList} from "~api/other";
import {delMessage} from "~uti/tools";

const router = useRouter()
onActivated(()=>{
    getTableData() 
    getDepartmentDict()//获取部门字典

})
//选择部门
const department = ref([
   
])

//搜索表单
const searchForm = ref({
    planType: null, planStartDate: null, planEndDate: null, departmentType: null,
    current: 1, size: 20, total: 0
})

//搜索
const searchClick = () => {
    searchForm.value.current = 1;
    getTableData()
}

//重置搜索表单
const resetClick = () => {
    searchForm.value = {current: 1, size: 20, total: 0}
}

//分页被点击
const pageChange = ({current, size}) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}

//获取数据
const tableLoading = ref(false)
const tableColumn = [
    {key: 'planName', name: '计划名称'},
    {key: 'key2', name: '计划起止日期', width: '220', align: 'center'},
    {key: 'planTotal', name: '计划数量', width: '120', align: 'center'},
    {key: 'finishPlanTotal', name: '已完成计划', width: '120', align: 'center'},
    {key: 'beginPlanTotal', name: '未完成计划', width: '120', align: 'center'},
    {key: 'planDesigner', name: '计划制定人', width: '120', align: 'center'},
    {key: 'action', name: '操作', width: '160', align: 'center'},
]
const tableData = ref([
    {id: 1, key1: '2023年5月度计划', key2: '2022-07-01~2027-04-12', key3: '36', key4: '30', key5: '6', key6: '张三'},
    {id: 2, key1: '2023年5月度计划', key2: '2022-07-01~2027-04-12', key3: '36', key4: '30', key5: '6', key6: '张三'},
    {id: 3, key1: '2023年5月度计划', key2: '2022-07-01~2027-04-12', key3: '36', key4: '30', key5: '6', key6: '张三'},
])

const getTableData = async() => {
    tableLoading.value = true
    const {error, code, data} = await sectionApi.getPage(searchForm.value)
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data)
        searchForm.value.total = data['total'] || 0
    } else {
        tableData.value = []
        searchForm.value.total = 0
    }
}

//表格名称被点击
const rowNameClick = (row) => {

}

//新增计划
const addRowClick = () => {
    monthModal.value = true
   
}
//获取所属部门列表
const getDepartmentDict=async()=>{
    const {error, code, data} = await getdepartmentList({deptType:2})
    if (!error && code === 200) {
        department.value = getArrValue(data)

    } else {
        department.value = []

    }
}


//选择月份
const monthModal = ref(false)
//选择月份的表单数据
const formMonthRef = ref(null)
const formMonthModel = ref({
    section: null, month: null
})
const formMonthRules = {
    section: [{required: true, message: '请选择部门', trigger: 'blur'}],
    month: [{required: true, message: '请选择月份', trigger: 'blur'}],
}

const monthCloseClick = () => {
    monthModal.value = false
}
//选择部门
const deptId=ref('')
const changeDpt=(val)=>{
    department.value.forEach((ele)=>{
        if(ele.deptCategory=val){
            deptId.value=ele.id
        }
    })
}
//新增部门月计划
const addPlan=async(obj)=>{;
    const {error, code, data,msg} = await sectionApi.addDepartmentPlan( {
        planDate:obj.month,
        departmentType:obj.section
    })
    if (!error && code === 200) {
       window.$message.success(msg)
        monthModal.value = false
        router.push({
            name: 'program-section-form',
            query: {...formMonthModel.value,type:'add',deptId:deptId.value},
    
        })
    } 

}
//确认下一步
const monthSaveClick =async() => {
    const res = await formValidate(formMonthRef.value)
    if(res){
        addPlan(formMonthModel.value)
    }
    monthModal.value = false

}

//编辑
const editRowClick = (row) => {
    changeDpt(row.departmentType)
    router.push({
        name: 'program-section-form',
        query: {
            id: row.id,
            month:row.planDate,
            section:row.departmentType,
            type:'edit',
            deptId:deptId.value
        }
    })
}

//删除
const delRowClick = async(row) => {
    delMessage(async() => {
        const {error, code, data,msg} = await sectionApi.deleteById({id:row.id})  
            if (!error && code === 200) {
                window.$message.success(msg)
                getTableData()
            } else {
                getTableData()
            }
    })
}
</script>
