<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :searchConfig="searchConfig"
        highlight-current-row
        :action-list="[
          {
            text: '添加设备',
            action: () => clickModal(),
          },
        ]"
        @get-list="getList"
        @clickReset="clickReset">
      </byTable>
    </el-card>

    <el-dialog :title="modalType == 'add' ? '添加设备' : '编辑设备'" v-if="openDialog" v-model="openDialog" width="600">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
        <template #productionDate>
          <div style="width: 100%">
            <el-date-picker
              v-model="formData.data.productionDate"
              type="datetime"
              placeholder="请选择日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              style="width: 100%" />
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="openDialog = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="large" v-preReClick>确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import byForm from "/src/components/byForm/index";
import { ElMessage, ElMessageBox } from "element-plus";
import { ref } from "vue";

const { proxy } = getCurrentInstance();
const craftProcessList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    craftProcessId: "",
    code: "",
    name: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "select",
      prop: "craftProcessId",
      label: "使用工序",
      data: craftProcessList.value,
    },
    {
      type: "input",
      prop: "code",
      label: "设备编码",
    },
    {
      type: "input",
      prop: "name",
      label: "设备名称",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "使用工序",
        prop: "craftProcessId",
      },
      render(val) {
        return proxy.dictKeyValue(val, craftProcessList.value);
      },
    },
    {
      attrs: {
        label: "设备编码",
        prop: "code",
      },
    },
    {
      attrs: {
        label: "设备名称",
        prop: "name",
      },
    },
    {
      attrs: {
        label: "型号规格",
        prop: "spec",
      },
    },
    {
      attrs: {
        label: "制造商",
        prop: "manufacturer",
      },
    },
    {
      attrs: {
        label: "制造商电话",
        prop: "manufacturerTelephone",
      },
    },
    {
      attrs: {
        label: "出厂日期",
        prop: "productionDate",
        width: 160,
      },
    },
    {
      attrs: {
        label: "出厂编码",
        prop: "productionNumber",
      },
    },
    {
      attrs: {
        label: "备注",
        prop: "remark",
      },
    },
    {
      attrs: {
        label: "操作",
        width: 120,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "编辑",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickUpdate(row);
            },
          },
          {
            attrs: {
              label: "删除",
              type: "danger",
              text: true,
            },
            el: "button",
            click() {
              clickDelete(row);
            },
          },
        ];
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/equipment/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const modalType = ref("add");
const openDialog = ref(false);
const submit = ref(null);
const formOption = reactive({
  inline: true,
  labelWidth: "120px",
  itemWidth: 100,
  rules: [],
  labelPosition: "right",
});
const formData = reactive({
  data: {},
});
const formConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "设备编码",
      itemType: "text",
    },
    {
      type: "input",
      prop: "name",
      label: "设备名称",
      itemType: "text",
    },
    {
      type: "select",
      prop: "craftProcessId",
      label: "使用工序",
      data: craftProcessList.value,
    },
    {
      type: "input",
      prop: "spec",
      label: "型号规格",
      itemType: "text",
    },
    {
      type: "input",
      prop: "manufacturer",
      label: "制造商",
      itemType: "text",
    },
    {
      type: "input",
      prop: "manufacturerTelephone",
      label: "制造商电话",
      itemType: "text",
    },
    {
      type: "slot",
      slotName: "productionDate",
      label: "出厂日期",
    },
    {
      type: "input",
      prop: "productionNumber",
      label: "出厂编码",
      itemType: "text",
    },
    {
      type: "input",
      prop: "remark",
      label: "备注",
      itemType: "textarea",
    },
  ];
});
const rules = ref({
  code: [{ required: true, message: "请输入设备编码", trigger: "blur" }],
  name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
  craftProcessId: [{ required: true, message: "请选择使用工序", trigger: "change" }],
});
const getDemandData = () => {
  proxy.post("/craftProcess/page", {}).then((res) => {
    if (res.rows && res.rows.length > 0) {
      craftProcessList.value = res.rows.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
};
getDemandData();
const clickModal = () => {
  modalType.value = "add";
  formData.data = {};
  openDialog.value = true;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    proxy.post("/equipment/" + modalType.value, formData.data).then(() => {
      ElMessage({
        message: modalType.value == "add" ? "添加成功" : "编辑成功",
        type: "success",
      });
      openDialog.value = false;
      getList();
    });
  });
};
const clickUpdate = (row) => {
  modalType.value = "edit";
  proxy.post("/equipment/detail", { id: row.id }).then((res) => {
    formData.data = res;
    openDialog.value = true;
  });
};
const clickDelete = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/equipment/delete", { id: row.id }).then(() => {
        ElMessage({ message: "删除成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
