<template>
  <div class="pageIndexClass">
    <!-- <Banner /> -->
    <div class="content">
      <byTable :source="sourceList.data" :pagination="sourceList.pagination" :config="config" :loading="loading" highlight-current-row
               :selectConfig="selectConfig" :table-events="{
        
        }" :action-list="[
          {
            text: '添加设备',
            action: () => openModal('add'),
          },
        ]" @get-list="getList">
        <template #slotName="{ item }">
          {{ item.createTime }}
        </template>
      </byTable>
    </div>
    <el-dialog :title="modalType == 'add' ? '添加设备' : '编辑'" v-model="dialogVisible" width="800">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="byform" v-loading="submitLoading">
      </byForm>
      <template #footer>
        <el-button @click="dialogVisible = false" size="default" v-debounce>取 消</el-button>
        <el-button type="primary" @click="submitForm('byform')" size="default" v-debounce>
          确 定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
  
<script setup>
/* eslint-disable vue/no-unused-components */
import { ElMessage, ElMessageBox } from "element-plus";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import { computed, defineComponent, ref } from "vue";
const { proxy } = getCurrentInstance();
const loading = ref(false);
const submitLoading = ref(false);
const sourceList = ref({
  data: [],
  pagination: {
    total: 3,
    pageNum: 1,
    pageSize: 10,
  },
});
const treeData = ref([]);
const companyData = ref([]);
const dialogVisible = ref(false);
const roomDialogVisible = ref(false);
const modalType = ref("add");
const rules = ref({
  companyId: [{ required: true, message: "请选择归属公司", trigger: "change" }],
  deviceCode: [
    { required: true, message: "请输入设备编码", trigger: ["blur"] },
  ],
  deviceName: [
    { required: true, message: "请输入设备名称", trigger: ["blur"] },
  ],
});
const selectConfig = computed(() => [
  {
    label: "归属公司",
    prop: "companyId",
    data: companyData.value,
  },
]);
const config = computed(() => {
  return [
    {
      attrs: {
        label: "归属公司",
        prop: "companyName",
        width: 110,
      },
    },
    {
      attrs: {
        label: "设备编码",
        prop: "deviceCode",
        width: 150,
      },
    },
    {
      attrs: {
        label: "设备名称",
        prop: "deviceName",
        width: 150,
      },
    },
    {
      attrs: {
        label: "规格型号",
        prop: "deviceSpec",
        width: 150,
      },
    },
    {
      attrs: {
        label: "制造商",
        prop: "makerName",
        width: 150,
      },
    },
    {
      attrs: {
        label: "制造商电话",
        prop: "makerPhone",
        width: 180,
      },
    },
    {
      attrs: {
        label: "出厂日期",
        prop: "deviceDeliveryTime",
        width: 110,
      },
      render(val) {
        if (val) {
          return val.slice(0, 10);
        }
      },
    },
    {
      attrs: {
        label: "备注",
        prop: "remark",
      },
    },
    {
      attrs: {
        label: "操作",
        width: "100",
        align: "center",
      },
      // 渲染 el-button，一般用在最后一列。
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "删除",
              type: "danger",
              text: true,
            },
            el: "button",
            click() {
              proxy
                .msgConfirm()
                .then((res) => {
                  proxy
                    .post("/tdaDevice/delete", {
                      id: row.id,
                    })
                    .then((res) => {
                      proxy.msgTip("删除成功", 1);
                      getList();
                    });
                })
                .catch((err) => {});
            },
          },
        ];
      },
    },
  ];
});

const formData = reactive({
  data: {},
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
});
const byform = ref(null);

const formConfig = computed(() => [
  {
    type: "treeSelect",
    prop: "companyId",
    label: "归属公司",
    data: treeData.value,
    propsTreeLabel: "deptName",
    propsTreeValue: "deptId",
    itemWidth: 100,
  },
  {
    type: "input",
    prop: "deviceCode",
    label: "设备编码",
    required: true,
  },
  {
    type: "input",
    prop: "deviceName",
    label: "设备名称",
    required: true,
  },
  {
    type: "input",
    prop: "deviceSpec",
    label: "规格型号",
    required: true,
  },
  {
    type: "input",
    prop: "makerName",
    label: "制造商",
    required: true,
  },
  {
    type: "input",
    prop: "makerPhone",
    label: "制造商电话",
    required: true,
  },
  {
    type: "date",
    itemType: "date",
    prop: "deviceDeliveryTime",
    label: "出厂日期",
    required: true,
  },
  {
    type: "input",
    itemType: "textarea",
    prop: "remark",
    label: "备注",
    required: true,
  },
]);
const getList = (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/tdaDevice/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};

const getDict = () => {
  proxy
    .get("/tenantDept/list", {
      pageNum: 1,
      pageSize: 9999,
      keyword: "",
      tenantId: proxy.useUserStore().user.tenantId,
      type: 0,
    })
    .then((res) => {
      companyData.value = res.data.map((x) => ({
        ...x,
        label: x.deptName,
        value: x.deptId,
      }));
      treeData.value = proxy.handleTree(res.data, "deptId");
    });
};
getDict();
const openModal = () => {
  dialogVisible.value = true;
  modalType.value = "add";
  formData.data = {};
};

const submitForm = () => {
  byform.value.handleSubmit((valid) => {
    submitLoading.value = true;
    proxy.post("/tdaDevice/" + modalType.value, formData.data).then(
      (res) => {
        proxy.msgTip(modalType.value == "add" ? "添加成功" : "编辑成功");
        dialogVisible.value = false;
        submitLoading.value = false;
        getList();
      },
      (err) => (submitLoading.value = false)
    );
  });
};

const getDtl = (row) => {
  modalType.value = "edit";
  proxy.post("/tdaDevice/detail", { id: row.id }).then((res) => {
    formData.data = res;
    console.log(formData);
    dialogVisible.value = true;
  });
};

getList();
</script>
  
<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>