<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" >
    <template #footer>
    </template>
    <BasicTable @register="registerTable" :rowSelection="rowSelection" >
        <!-- update-begin-author:taoyan date:2022-5-25 for: VUEN-1112一对多 用户选择 未显示选择条数，及清空 -->
        <template #tableTitle></template>
        <template #bodyCell="{ text, record, index, column }">
            <template v-if="column.key === 'index'">{{ index+1 }}</template>
        </template>
        <template #action="{ record }">
          <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
        </template>
        <!-- update-end-author:taoyan date:2022-5-25 for: VUEN-1112一对多 用户选择 未显示选择条数，及清空 -->
    </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
import { computed, ref, unref } from 'vue';
import { queryPerpetratePageList,editProjectCheckDuty,batchEditProjectDutyCheck} from '../../StProjectConfig.api';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);   
    const actinfo = ref({
        id: null,
        ids: [],
        projectCreId:null,
        caNo: null,
    })
    const isDetail = ref(false);
    const isBatch = ref(false)
    //表单配置
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        isDetail.value = !data?.showFooter;
        isBatch.value = data?.isBatch
        if (unref(isUpdate)) {
            if(unref(isBatch)){
                actinfo.value.ids = data.record.ids
                actinfo.value.projectCreId = data.record.projectCreId
            }else{
                //表单赋值
                actinfo.value.id = data.record.id
                actinfo.value.projectCreId = data.record.projectCreId
                actinfo.value.caNo = data.record.caNo
                // 改变pFlag状态
            }
            console.log(actinfo)
        }
    });
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '填写检查方法' : (unref(isDetail)?'查看检查方法':'配置检查方法')));
    // table
    const columns = [
            {
                title: '序号',
                align:"left",
                key: 'index',
                dataIndex: 'index',
            },
            {
            title: '人员姓名',
            align:"left",
            dataIndex: 'userName',
        },
        {
            title: '所属部门',
            align:"left",
            dataIndex: 'depName'
        },
        {
            title: '岗位',
            align:"left",
            dataIndex: 'postName'
        },
        {
            title: '项目角色',
            align:"left",
            dataIndex: 'projectRoleName'
        },
    ];
    const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
        tableProps:{
            title: '',
            api: queryPerpetratePageList,
            columns,
            canResize:false,
            useSearchForm: false,
            showIndexColumn: false,
            scroll: { x: false },
            actionColumn: {
                width: 160
            },
            showTableSetting: false,
            beforeFetch(params) {
                params = Object.assign(params,{projectCreId:actinfo.value.projectCreId})
                },
        },
    })
    const [registerTable, {setProps,reload},{ rowSelection, selectedRowKeys }] = tableContext
    async function handleEdit(record: Recordable) {
    //  console.log(record)
    //  console.log(actinfo.value)
     try {
            setModalProps({confirmLoading: true});
            if(unref(isBatch)){
                let saveData = {}
                saveData.ids = actinfo.value.ids.join(",")
                saveData.dutyCode = record.userNo
            //提交表单
                await batchEditProjectDutyCheck(saveData);
            }else{
                let saveData = {}
                saveData.id = actinfo.value.id
                saveData.dutyCode = record.userNo
            //提交表单
                await editProjectCheckDuty(saveData);
            }
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
    function getTableAction(record){
       return [
            {
                label: '选择',
                onClick: handleEdit.bind(null, record),
                },
            ]
        }
    function getDropDownAction(record){
        return [
            
        ]
    }
    // table
    //表单提交事件
    // async function handleSubmit(v) {
    //     try {
    //         let values = await validate();
    //         setModalProps({confirmLoading: true});
    //         //提交表单
    //         await editProjectCheck(values);
    //         //关闭弹窗
    //         closeModal();
    //         //刷新列表
    //         emit('success');
    //     } finally {
    //         setModalProps({confirmLoading: false});
    //     }
    // }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>