<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px] formtabel" :model="queryParams" inline label-width="100px">
                <el-form-item label="区域经理" prop="create_user_id">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.create_user_id"
                        clearable
                        placeholder="请输入区域经理"
                    />
                </el-form-item>
                <el-form-item label="公司" prop="company_id">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.company_id"
                        clearable
                        placeholder="请输入公司"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button class="el-btn" type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <!-- <el-button
        v-perms="['task_scheduling.task_scheduling/add']"
        type="primary"
        @click="handleAdd"
      >
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button> -->
            <!--
            <el-button
                v-perms="['task_scheduling.task_scheduling/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button> -->
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="区域经理" prop="admin_name" show-overflow-tooltip />
                    <el-table-column label="公司" prop="company_name" show-overflow-tooltip />
                    <el-table-column label="每日最大金额" prop="money" show-overflow-tooltip />
                    <el-table-column
                        label="公司类型"
                        prop="company_type_name"
                        show-overflow-tooltip
                    />
                    <el-table-column
                        label="状态"
                        min-width="100"
                        v-perms="['task_scheduling.task_scheduling/edit']"
                    >
                        <template #default="{ row }">
                            <el-switch
                                v-model="row.status"
                                :active-value="1"
                                :inactive-value="0"
                                @change="changeStatus(row)"
                            />
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['task.task_calendar']"
                                @click="moneyAdd(row)"
                                type="primary"
                                link
                            >
                                金额
                            </el-button>
                            <el-button
                                v-perms="['task_template.task_template/lists']"
                                type="primary"
                                link
                            >
                                <router-link
                                    :to="{
                                        path: getRoutePath('task_template.task_template/lists'),
                                        query: {
                                            id: row.id,
                                            company_id: row.company_id
                                        }
                                    }"
                                    >任务安排</router-link
                                >
                            </el-button>
                            <el-button v-perms="['task.task_calendar']" type="primary" link>
                                <router-link
                                    :to="{
                                        path: getRoutePath('task.task_calendar'),
                                        query: {
                                            id: row.id,
                                            company_id: row.company_id
                                        }
                                    }"
                                    >任务日程</router-link
                                >
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
        <money
            ref="moneyRef"
            v-if="moneyShow"
            @success="getLists"
            @close="moneyShow = false"
        ></money>
    </div>
</template>

<script lang="ts" setup name="taskSchedulingLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiTaskSchedulingLists, apiTaskSchedulingEdit } from '@/api/task_scheduling'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { dictDataLists } from '@/api/setting/dict'
import { getRoutePath } from '@/router'
import money from './money.vue'

const datalist = ref([])
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const moneyRef = shallowRef<InstanceType<typeof money>>()
// 是否显示编辑框
const showEdit = ref(false)
// 是否显示金额编辑框
const moneyShow = ref(false)

// 查询条件
const queryParams = reactive({
    create_user_id: '',
    template_id: '',
    company_id: '',
    type: '',
    status: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiTaskSchedulingLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 设置金额
const moneyAdd = async (obj: any) => {
    moneyShow.value = true
    await nextTick()
    moneyRef.value?.open(obj.money ? 'edit' : 'add')
    moneyRef.value?.setFormData(obj)
}

const changeStatus = (data: any) => {
    apiTaskSchedulingEdit({
        id: data.id,
        status: data.status
    }).finally(() => {
        getLists()
    })
}
dictDataLists({ type_id: 10 }).then((res) => {
    datalist.value = res.lists
})

getLists()
</script>
<style lang="scss" scoped>
.formtabel {
    .el-form-item {
        width: 20%;

        .el-btn {
            margin-left: 100px;
        }
    }
}
</style>
