<!-- 项目任务扩展属性列表 -->
<template>
  <ContentWrap>
    <el-form
      ref="formRef"
      :model="formData"
      style="margin-right: 4em; margin-left: 0.5em; margin-top: 1em"
      label-width="130px"
    >
      <div class="base-expandable-content">
        <el-row>
          <el-col :span="12">
            <el-form-item label="模板名称" prop="name">
              <el-input type="text" v-model="formData.name" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="deptName">
              <el-input
                v-model="formData.deptName"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          v-hasPermi="[`pms:iot-device-category-template-attrs:create`]"
          plain
          type="primary"
          @click="openForm('create')"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          添加属性
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs>
      <el-table v-loading="loading" :data="list" :show-overflow-tooltip="true" :stripe="true">
        <el-table-column align="center" label="属性名称" prop="name" />
        <el-table-column align="center" label="标识符" prop="identifier" />
        <el-table-column align="center" label="数据类型" prop="type">
          <template #default="{ row }">
            {{ dataTypeOptionsLabel(row.extProperty?.dataType) ?? '-' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template #default="scope">
            <el-button
              v-hasPermi="[`pms:iot-device-category-template-attrs:update`]"
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
            >
              编辑
            </el-button>
            <el-button
              v-hasPermi="['pms:iot-device-category-template-attrs:delete']"
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNo"
        :total="total"
        @pagination="getList"
      />
    </el-tabs>
  </ContentWrap>
  <!-- 表单弹窗：添加/修改-->
  <TaskAttrModelForm ref="formRef" @success="getList" :deviceCategoryId="deviceCategoryId" />
</template>
<script lang="ts" setup>
import { IotProjectTaskAttrsApi, IotProjectTaskAttrsVO } from '@/api/pms/iotprojecttaskattrs'
import TaskAttrModelForm from './TaskAttrModelForm.vue'
import { getDataTypeOptionsLabel } from './config'
import { useTagsViewStore } from "@/store/modules/tagsView";

const { delView } = useTagsViewStore() // 视图操作
const route = useRoute()
const { currentRoute } = useRouter()

defineOptions({ name: 'ProjectTaskTemplateAttrs' })

const deptId = route.params.deptId // 公司id

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const loading = ref(true) // 列表的加载中
const list = ref<IotProjectTaskAttrsVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type: undefined,
  deptId: -1
})

// 定义表单数据
const formData = reactive({
  name: '',
  deptName: ''
})

const queryFormRef = ref() // 搜索的表单
const dataTypeOptionsLabel = computed(() => (value: string) => getDataTypeOptionsLabel(value)) // 解析数据类型

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.deptId = deptId
    const data = await IotProjectTaskAttrsApi.getIotProjectTaskAttrsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.type = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DeviceAttrModelApi.deleteDeviceAttrModel(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  if (!deptId) {
    message.warning('参数错误，任务属性模板不能为空！')
    delView(unref(currentRoute))
    return
  }
  console.log(route.params.templateName + ' - ' + route.params.deptName)
  // 从路由参数获取模板名称和公司名称
  if (route.params.templateName) {
    formData.name = route.params.templateName as string
  }
  if (route.params.deptName) {
    formData.deptName = route.params.deptName as string
  }

  getList()
})
</script>
