
<template>
  <HcCard>
    <template #header>
      <div class="w-36 ml-2">
                <el-select v-model="searchForm.projectId" block clearable placeholder="项目名称" size="large">
                    <el-option v-for="item in projectType" :label="item.projectName" :value="item.projectId"/>
                </el-select>
      </div>
     
      <div class="ml-4">
                <el-button type="primary" @click="searchClick" size="large">
                    <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 type="warning" @click="toImportTempClick" size="large">
          <HcIcon name="delete-bin-2"/>
                    <span>草稿箱{{draftNum > 0 ? `(${draftNum})` : ''}}</span>
         </el-button>
         <el-button type="primary" size="large" class="ml-2" @click="addinfoClick">
                    <HcIcon name="add"/>
                    <span>新增出差申请</span>
         </el-button>
    </template>
    <HcTable :column="tableColumn" :datas="tableData" :loading="tableLoading">
      <template #tripDesc="{row}">
                <span class="text-blue text-hover">{{row.tripDesc}}</span>
        </template>
      <!-- <template #action="{row, index}">
         <el-button hc-btn type="primary" size="small">撤销</el-button>
       </template> -->
       <template #action="{row,index}">
                <el-popconfirm title="是否确认撤销?" hide-icon @confirm="rowCancel(row)">
                    <template #reference>
                        <el-button size="small" type="primary"
                                   :disabled="row.status !== 1"
                                   :loading="row.isCancelLoading"
                        >撤销</el-button>
                    </template>
                </el-popconfirm>
        </template>
      </HcTable>
      <template #action>
            <HcPages :pages="searchForm" @change="pageChange"></HcPages>
      </template>
            <!--草稿箱弹窗-->
            <HcDialog bgColor="#ffffff" widths="62rem" isToBody  :show="importModal" title="草稿箱" @close="importModalClose" isTable >
              <el-alert title="3个月内未更新的草稿将被自动删除" type="warning"  show-icon/>
              <div class="table_box">
                <HcTable :column="drafttableColumn" :datas="drafttableData" ui="hc-test-drop-table" isRowDrop isSort @row-drop="rowDropTap" @row-sort="rowSortTap" :loading="draftLoad">
                  <template #action="{row, index}">
                    <el-button hc-btn type="primary" size="small" @click="editinfoClick(row)">继续编辑</el-button>
                    <el-button hc-btn type="primary" size="small" @click="delRowClick(row)">删除</el-button>
                  </template>
                </HcTable>
              </div>
               
             </HcDialog>
  </HcCard>
</template>

<script setup>
import {ref, watch,onMounted} from  'vue'
import dayjs from "dayjs"
import 'dayjs/locale/zh-cn'
import {useRouter} from 'vue-router'

import businessApi from '~api/attendance/business-trip.js';
import {getArrValue} from "js-fast-way"
import {getProjectList, getDictInfo} from "~api/other";
import {delMessage} from "~uti/tools";
const router = useRouter()
onMounted(()=>{
  getProjectData()
  getTableData()
  getdraftTableData()
})

const tableColumn = [
    {key: 'tripDesc', name: '出差事由'},
    {key: 'projectName', name: '关联项目'},
    {key: 'duration', name: '实际出勤天数'},
    {key: 'durationAll', name: '出差天数'},
    {key: 'fellowTravelerUserNames', name: '同行人'},
    {key: 'approvalResultName', name: '审批结果'},
    {key: 'approvalStatusName', name: '审批状态'},
    {key: 'createName', name: '创建人'},
    {key: 'createTime', name: '创建时间'},
    {key: 'action', name: '操作'  }


]
const tableData = ref([
    {name: '名称1',id:1},
    {name: '名称2', },
    {name: '名称3', }
])
const searchForm = ref({
  
    current: 1, size: 20, total: 0
})
//项目类型
const projectType = ref([

])

//获取项目数据

const getProjectData = async () => {
    const {error, code, data} = await getProjectList()
    //判断状态
    if (!error && code === 200) {
        projectType.value = getArrValue(data)
    } else {
        projectType.value = []
    }
}

//分页被点击
const pageChange = ({current, size}) => {
    searchForm.value.current = current
    searchForm.value.size = size
    getTableData()
}
const tableLoading=ref(false)
const getTableData = async() => {
    tableLoading.value = true
    const {error, code, data} = await businessApi.getPage(searchForm.value)
    tableLoading.value = false
    if (!error && code === 200) {
        tableData.value = getArrValue(data['records'])
        searchForm.value.total = data['total'] || 0
    } else {
        tableData.value = []
        searchForm.value.total = 0
    }
}
//搜索
const searchClick = () => {
    searchForm.value.current = 1;
    getTableData()
}

//重置搜索表单
const resetClick = () => {
    searchForm.value = {current: 1, size: 20, total: 0}
}
const defaultTime = ref([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
])
//草稿箱
//草稿箱数据
const draftNum = ref(0)
const importModal=ref(false)
const importModalClose=()=>{
  importModal.value=false
}
const drafttableColumn = [
    {key: 'title', name: '标题'},
    {key: 'updateTime', name: '更新时间'},
    {key: 'action', name: '操作',widths:120},
]
const drafttableData = ref([
    {name: '名称1', text: '2023-5-23 15:21:08', },
    {name: '名称1', text: '2023-5-23 15:21:08', },
    
 
])
const draftLoad=ref(false)
const getdraftTableData = async() => {
  draftLoad.value = true
    const {error, code, data} = await businessApi.getDraftList()
    draftLoad.value = false
    if (!error && code === 200) {
      drafttableData.value = getArrValue(data)
      draftNum.value=getArrValue(data).length
     
    } else {
      drafttableData.value = []
      
    }
}
// 行拖拽
const rowDropTap = (rows) => {
    tableData.value = rows
}

// 点击排序
const rowSortTap = (rows) => {
    tableData.value = rows
}

const toImportTempClick=()=>{
  importModal.value=true
  getdraftTableData()

}
//编辑出差申请
const addinfoClick=()=>{
  router.push({
        name: 'attendance-business-trip-info',
        query: {
            type: 'add'
        }
    })
}
const editinfoClick=(row)=>{
  importModal.value=false
  router.push({
        name: 'attendance-business-trip-info',
        query: {
          id: row.emdraftIds,
          type: 'draft'
        }
    })
}
//查看
const rowClick = (row) => {
    router.push({
      name: 'attendance-business-trip-info',
        query: {
            id: row.id,
            type: 'view'
        }
    })
}

//撤销
const rowCancel = async (row) => {
    row.isCancelLoading = true
    const {error, code, msg} = await businessApi.cancel({
        id: row.id
    })
    //判断状态
    row.isCancelLoading = false
    if (!error && code === 200) {
        window.$message?.success(msg)
        getTableData().then()
    } else {
        window.$message?.error(msg)
    }
}
//删除
const delRowClick = async(row) => {
    delMessage(async() => {
        const {error, code, data,msg} = await businessApi.remove({groupId:row.groupId})  
            if (!error && code === 200) {
                window.$message.success(msg)
                getdraftTableData().then()
            } else {
              window.$message?.error(msg)
            }
    })
}
</script>
<style lang='scss' scoped>
</style>