import { h, ref, reactive } from 'vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import { getTaskInfo, taskDel } from '@/api/task'
import { useUserStore } from '@/store/modules/user'

const roleType = ref(null)
setTimeout(() => {
  roleType.value = useUserStore().userInfo.type
})

/* 注册table */
const tableRef = ref()
const searchParams = ref()

function handleSearch(params) {
  searchParams.value = { ...params }
  tableRef.value.reFetch({ searchParams })
}

/**
 * @description: 获取数据及操作
 * @param {*} row 单行数据
 * @param {*} type 操作类型 create:创建，preview:预览，edit:编辑
 * @return {*}
 */
async function getRowData(row, type) {
  data.detail = await getTaskInfo(row.id) // 根据id获取详情
  data.rowData = row
  data.modalType = type
  data.showDraw = true
}

/**
 * @description: 编辑
 * @return {*}
 */
function editHandle(row, type) {
  getTaskInfo(row.id).then(res => {
    data.rowData = res.data
    data.modalType = type
    data.modalShow = true
  })
}

/**
 * @description: 删除用户接口
 * @param {Array} ids 用户id集合
 * @return {*}
 */
function deleteData(ids) {
  taskDel(ids).then((res) => {
    if (res.code === 0) {
      handleSearch()
    }
  }).catch(e => {
    console.log(e)
  })
}

const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  showDraw: false,
  detail: {},
  handleSearch,
  deleteData,
  columns: [
    {
      title: '任务编码',
      key: 'taskCode',
      align: 'center'
    },
    {
      title: '平台名称',
      key: 'platformName',
      align: 'center'
    },
    {
      title: '任务名称',
      key: 'taskName',
      align: 'center'
    },
    {
      title: '客户名称',
      key: 'tenantName',
      align: 'center'
    },
    {
      title: '期望执行时间',
      key: 'taskStartTime',
      align: 'center'
    },
    {
      title: '巡逻地点',
      key: 'patrolLocation',
      align: 'center'
    },
    {
      title: '任务状态',
      key: 'statusName',
      align: 'center'
    },
    {
      title: '飞手',
      key: 'flightHandName',
      align: 'center'
    },
    {
      title: '操作',
      align: 'center',
      width: 150,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            {
              label: '详情',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row)
              },
              auth: ''
            },
            {
              label: '编辑',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: editHandle.bind(null, row, 'update')
              },
              show: row.status < 11 && row.inspectionType === 1,
              auth: 'task:all:edit'
            },
            {
              label: '删除',
              type: 'popconfirm',
              tip: '确定删除这条数据吗?',
              props: {
                onPositiveClick: deleteData.bind(null, row.id)
              },
              auth: 'task:all:delete',
              show: row.status < 16 && row.inspectionType === 1,
              ButtonProps: {
                text: true,
                type: 'primary'
              }
            }
          ],
          align: 'center'
        })
      }
    }
  ]
})

export default data
