<template>
  <div class="page">
    <el-card shadow="never">
      <el-form :model="tableData.param" ref="queryRef" inline label-width="68px">
        <!-- 关键字 -->
        <el-form-item :label="$t('message.device.formI18nLabel.keyword')" prop="name">
          <el-input v-model="tableData.param.name" :placeholder="$t('message.device.formI18nPlaceholder.keyword')" clearable style="width: 150px" @keyup.enter.native="getList" />
        </el-form-item>
        <!-- 类型 -->
        <el-form-item :label="$t('message.device.formI18nLabel.type')" prop="deviceType" style="width: 180px">
          <el-select v-model="tableData.param.deviceType" :placeholder="$t('message.device.formI18nPlaceholder.type')" clearable style="width: 240px" @keyup.enter.native="getList">
            <!-- 网关 -->
            <el-option :label="$t('message.device.formI18nOption.gateway')" :value="'网关'" />
            <!-- 设备 -->
            <el-option :label="$t('message.device.formI18nOption.device')" :value="'设备'" />
            <!-- 子设备 -->
            <el-option :label="$t('message.device.formI18nOption.subDevice')" :value="'子设备'" />
            <!-- <el-option label="直连设备" :value="'设备'" />
            <el-option label="网关设备" :value="'网关'" /> -->
          </el-select>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item :label="$t('message.device.formI18nLabel.status')" prop="status" style="width: 180px">
          <el-select v-model="tableData.param.status" :placeholder="$t('message.device.formI18nPlaceholder.status')" clearable style="width: 240px">
            <!-- 已启用 -->
            <el-option :label="$t('message.device.formI18nOption.on')" :value="1" />
            <!-- 未启用 -->
            <el-option :label="$t('message.device.formI18nOption.off')" :value="0" />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="创建时间" prop="dateRange">-->
        <!--          <el-date-picker v-model="tableData.param.dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>-->
        <!--        </el-form-item>-->
        <el-form-item>
          <!-- 查询 -->
          <el-button type="primary" class="ml10" @click="getList">
            <el-icon>
              <ele-Search />
            </el-icon>
            {{ $t('message.formI18nButton.query') }}
          </el-button>
          <!-- 重置 -->
          <!-- <el-button @click="resetQuery(queryRef)">
            <el-icon>
              <ele-Refresh />
            </el-icon>
            {{ $t('iotmanagerI18n.formI18nButton.reset') }}
          </el-button> -->
          <!-- 新增产品 -->
          <el-button type="primary" class="ml10" @click="onOpenAddDic" v-auth="'add'">
            <el-icon>
              <ele-FolderAdd />
            </el-icon>
            {{ $t('message.device.formI18nButton.addProduct') }}
          </el-button>
          <!-- 删除 -->
          <el-button type="info" class="ml10" @click="onRowDel()" v-auth="'del'">
            <el-icon>
              <ele-Delete />
            </el-icon>
            {{ $t('message.formI18nButton.delete') }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData.data" style="width: 100%" @selection-change="handleSelectionChange" v-loading="tableData.loading">
        <el-table-column type="selection" width="55" align="center" />
        <!-- 标识 -->
        <el-table-column :label="$t('message.device.tableI18nColumn.key')" prop="key" min-width="130" show-overflow-tooltip v-col="'key'">
          <template #default="{ row }"><copy :text="row.key"></copy></template>
        </el-table-column>
        <!-- 名称 -->
        <el-table-column :label="$t('message.device.tableI18nColumn.name')" prop="name" min-width="160" show-overflow-tooltip v-col="'name'" />
        <!-- 分类 -->
        <el-table-column :label="$t('message.device.tableI18nColumn.category')" prop="categoryName" min-width="160" align="center" show-overflow-tooltip v-col="'categoryName'" />
        <!--        <el-table-column label="消息协议" prop="messageProtocol" align="center" min-width="150" show-overflow-tooltip v-col="'messageProtocol'" />-->
        <!-- 接入方式 -->
        <el-table-column :label="$t('message.device.tableI18nColumn.transportProtocol')" prop="transportProtocol" min-width="120" align="center" show-overflow-tooltip v-col="'transportProtocol'" />
        <!-- 类型 -->
        <el-table-column :label="$t('message.device.tableI18nColumn.deviceType')" prop="deviceType" min-width="90" align="center" show-overflow-tooltip v-col="'deviceType'" />
        <!-- 状态 -->
        <el-table-column prop="status" :label="$t('message.device.tableI18nColumn.status')" min-width="90" align="center" v-col="'status'">
          <template #default="scope">
            <el-tag type="success" size="small" v-if="scope.row.status">{{ $t('message.device.tableI18nStatus.enabled') }}</el-tag>
            <el-tag type="info" size="small" v-else>{{ $t('message.device.tableI18nStatus.disabled') }}</el-tag>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column :label="$t('message.tableI18nColumn.operation')" align="center" fixed="right" :min-width="currentLang === 'en' ? 240 : 160">
          <template #default="scope">
            <!-- 详情 -->
            <router-link :to="'/iotmanager/device/product/detail/' + scope.row.key" class="link-type" style="padding-right: 12px; font-size: 12px; color: #409eff">
              <span>{{ $t('message.tableI18nAction.detail') }}</span>
            </router-link>
            <!-- 设备管理 -->
            <router-link :to="{ path: '/iotmanager/device/instance', query: { productKey: scope.row.key } }" class="link-type" style="padding-right: 12px; font-size: 12px; color: #409eff">
              <span>{{ $t('message.device.tableI18nAction.deviceManagement') }}</span>
            </router-link>
            <!-- <el-button size="small" text type="warning" @click="onOpenEditDic(scope.row)" v-auth="'edit'">修改</el-button>-->
             <!-- 删除 -->
            <el-button size="small" text type="info" @click="onRowDel(scope.row)" v-auth="'del'">{{ $t('message.tableI18nAction.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="getList" />
    </el-card>
    <EditDic ref="editDicRef" @typeList="getList" />
  </div>
</template>

<script lang="ts" setup>
import { toRefs, reactive, onMounted, ref, defineComponent } from "vue";
import { ElMessageBox, ElMessage, FormInstance } from "element-plus";
import EditDic from "./component/editPro.vue";
import api from "/@/api/device";
import { computed } from "vue";
import { useI18n } from "vue-i18n";

const { t, locale } = useI18n();

const currentLang = computed(() => locale.value);


// 定义接口来定义对象的类型
interface TableDataRow {
  id: number;
  key: string;
  name: string;
  deviceType: string;
  status: number;
  desc: string;
  createBy: string;
}


// const addDicRef = ref();
const editDicRef = ref();
const queryRef = ref();
const keys = ref<any>([]);
const tableData = ref({
  data: [],
  total: 0,
  loading: false,
  param: {
    pageNum: 1,
    pageSize: 20,
    status: "",
    name: "",
    deviceType: "",
    dateRange: [],
  },
})
// 初始化表格数据
// const initTableData = () => {
//   typeList();
// };
const getList = () => {
  tableData.value.loading = true;
  api.product
    .getList(tableData.value.param)
    .then((res: any) => {
      tableData.value.data = res.product;
      tableData.value.total = res.total;
    })
    .finally(() => (tableData.value.loading = false));
};
// 打开新增产品弹窗
const onOpenAddDic = () => {
  editDicRef.value.openDialog();
};
// 打开修改产品弹窗
// const onOpenEditDic = (row: TableDataRow) => {
//   editDicRef.value.openDialog(row);
// };
// 删除产品
const onRowDel = (row?: TableDataRow) => {
  let msg = t('message.tableI18nConfirm.deleteSelectedMessage');
  let keysData: string[] = [];
  if (row) {
    msg = t('message.device.tableI18nConfirm.deleteProductMessage', { name: row.name });
    keysData = [row.key];
  } else {
    keysData = keys.value;
  }
  if (keysData.length === 0) {
    ElMessage.error(t('message.tableI18nConfirm.selectDataFirst'));
    return;
  }
  ElMessageBox.confirm(msg, t('message.tableI18nConfirm.deleteTitle'), {
    confirmButtonText: t('message.tableI18nConfirm.confirmText'),
    cancelButtonText: t('message.tableI18nConfirm.cancelText'),
    type: "warning",
  })
    .then(() => {
      api.product.delete(keysData).then(() => {
        ElMessage.success(t('message.tableI18nConfirm.deleteSuccess'));
        getList();
      });
    })
    .catch(() => {});
};
// 页面加载时
onMounted(() => {
  // initTableData();
  getList()
});
/** 重置按钮操作 */
// const resetQuery = (formEl: FormInstance | undefined) => {
//   if (!formEl) return;
//   formEl.resetFields();
//   typeList();
// };
// 多选框选中数据
const handleSelectionChange = (selection: TableDataRow[]) => {
  keys.value = selection.map((item) => item.key);
};

</script>
