<template>
  <el-form ref="formRef" :model="formData" :rules="rules">
    <el-form-item
      v-for="(item, index) in 35"
      :label="index + 1 + '：' + showLable(`DDLSD${index + 1}`)"
      :prop="`DDLSD${index + 1}`"
      :key="index"
    >
      <template
        v-if="
          index + 1 === 1 ||
          index + 1 === 2 ||
          index + 1 === 9 ||
          index + 1 === 17
        "
      >
        <el-select
          :disabled="lookStatus"
          v-model="formData[`DDLSD${index + 1}`]"
          style="width: 160px"
        >
          <el-option
            v-for="(item, indexa) in labelOption(index + 1)"
            :key="indexa"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </template>
      <template v-else>
        <el-input-number
          :disabled="lookStatus"
          v-model="formData[`DDLSD${index + 1}`]"
          controls-position="right"
          :precision="
            index + 1 === 4 ||
            index + 1 === 7 ||
            index + 1 === 8 ||
            index + 1 === 10 ||
            index + 1 === 11 ||
            index + 1 === 15 ||
            index + 1 === 16 ||
            index + 1 === 19 ||
            index + 1 === 21 ||
            index + 1 === 23 ||
            index + 1 === 25 ||
            index + 1 === 25 ||
            index + 1 === 27 ||
            index + 1 === 29 ||
            index + 1 === 31 ||
            index + 1 === 33 ||
            index + 1 === 35
              ? 0
              : 2
          "
        />
      </template>
    </el-form-item>
  </el-form>
</template>
<script setup>
import { add } from "@/api/excel";

const props = defineProps({
  propIndex: {
    type: Number,
  },
});
const formRef = ref(null);
const rules = {
  DDLSD1: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD1 == null || formData.value.DDLSD1 === "") {
        callback(new Error("请输入或选择值"));
      } else {
        callback();
      }
    },
  },
  DDLSD2: {
    required: true,
    message: "",
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD2 == null || formData.value.DDLSD2 === "") {
        callback(new Error("请输入或选择值"));
      } else {
        callback();
      }
    },
  },
  DDLSD3: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD3 == null || formData.value.DDLSD3 === "") {
        callback(new Error("请输入或选择值"));
      } else {
        callback();
      }
    },
  },
  DDLSD4: {
    required: true,
    trigger: "change",
    message: "范围0-4000",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD4 == null || formData.value.DDLSD4 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD4) > 4000 ||
        Number(formData.value.DDLSD4) < 0
      ) {
        callback(new Error("范围 0 - 4000"));
      } else {
        callback();
      }
    },
  },
  DDLSD5: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD5 == null || formData.value.DDLSD5 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD5) > 100.0 ||
        Number(formData.value.DDLSD5) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD6: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD6 == null || formData.value.DDLSD6 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD6) > 100.0 ||
        Number(formData.value.DDLSD6) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD7: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD7 == null || formData.value.DDLSD7 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD7) > 9999 ||
        Number(formData.value.DDLSD7) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD8: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD8 == null || formData.value.DDLSD8 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD8) > 1000 ||
        Number(formData.value.DDLSD8) < -1000
      ) {
        callback(new Error("范围 -1000 - 1000"));
      } else {
        callback();
      }
    },
  },
  DDLSD9: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD9 == null || formData.value.DDLSD9 === "") {
        callback(new Error("请输入或选择值"));
      } else {
        callback();
      }
    },
  },
  DDLSD10: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD10 == null || formData.value.DDLSD10 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD10) > 9999 ||
        Number(formData.value.DDLSD10) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD11: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD11 == null || formData.value.DDLSD11 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD11) > 9999 ||
        Number(formData.value.DDLSD11) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD12: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD12 == null || formData.value.DDLSD12 === "") {
        callback(new Error("请输入或选择值"));
      } else {
        callback();
      }
    },
  },
  DDLSD13: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD13 == null || formData.value.DDLSD13 === "") {
        callback(new Error("请输入或选择值"));
      } else {
        callback();
      }
    },
  },
  DDLSD14: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD14 == null || formData.value.DDLSD14 === "") {
        callback(new Error("请输入或选择值"));
      } else if (
        Number(formData.value.DDLSD14) > 100.0 ||
        Number(formData.value.DDLSD14) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD15: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD15 == null || formData.value.DDLSD15 === "") {
        callback(new Error("请输入值"));
      } else {
        callback();
      }
    },
  },
  DDLSD16: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD16 == null || formData.value.DDLSD16 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD16) > 4000 ||
        Number(formData.value.DDLSD16) < 0
      ) {
        callback(new Error("范围 0 - 4000"));
      } else {
        callback();
      }
    },
  },
  DDLSD17: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD17 == null || formData.value.DDLSD17 === "") {
        callback(new Error("请输入值"));
      } else {
        callback();
      }
    },
  },
  DDLSD18: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD18 == null || formData.value.DDLSD18 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD18) > 100.0 ||
        Number(formData.value.DDLSD18) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD19: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD19 == null || formData.value.DDLSD19 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD19) > 9999 ||
        Number(formData.value.DDLSD19) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD20: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD20 == null || formData.value.DDLSD20 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD20) > 100.0 ||
        Number(formData.value.DDLSD20) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD21: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD21 == null || formData.value.DDLSD21 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD21) > 9999 ||
        Number(formData.value.DDLSD21) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD22: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD22 == null || formData.value.DDLSD22 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD22) > 100.0 ||
        Number(formData.value.DDLSD22) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD23: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD23 == null || formData.value.DDLSD23 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD23) > 9999 ||
        Number(formData.value.DDLSD23) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD24: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD24 == null || formData.value.DDLSD24 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD24) > 100.0 ||
        Number(formData.value.DDLSD24) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD25: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD25 == null || formData.value.DDLSD25 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD25) > 9999 ||
        Number(formData.value.DDLSD25) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD26: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD26 == null || formData.value.DDLSD26 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD26) > 100.0 ||
        Number(formData.value.DDLSD26) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD27: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD27 == null || formData.value.DDLSD27 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD27) > 9999 ||
        Number(formData.value.DDLSD27) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD28: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD28 == null || formData.value.DDLSD28 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD28) > 100.0 ||
        Number(formData.value.DDLSD28) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD29: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD29 == null || formData.value.DDLSD29 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD29) > 9999 ||
        Number(formData.value.DDLSD29) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD30: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD30 == null || formData.value.DDLSD30 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD30) > 100.0 ||
        Number(formData.value.DDLSD30) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD31: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD31 == null || formData.value.DDLSD31 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD31) > 9999 ||
        Number(formData.value.DDLSD31) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD32: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD32 == null || formData.value.DDLSD32 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD32) > 100.0 ||
        Number(formData.value.DDLSD32) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD33: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD33 == null || formData.value.DDLSD33 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD33) > 9999 ||
        Number(formData.value.DDLSD33) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
  DDLSD34: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD34 == null || formData.value.DDLSD34 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD34) > 100.0 ||
        Number(formData.value.DDLSD34) < 0.0
      ) {
        callback(new Error("范围 0.00 - 100.00"));
      } else {
        callback();
      }
    },
  },
  DDLSD35: {
    required: true,
    trigger: "change",
    validator: (rule, value, callback) => {
      if (formData.value.DDLSD35 == null || formData.value.DDLSD35 === "") {
        callback(new Error("请输入值"));
      } else if (
        Number(formData.value.DDLSD35) > 9999 ||
        Number(formData.value.DDLSD35) < 0
      ) {
        callback(new Error("范围 0 - 9999"));
      } else {
        callback();
      }
    },
  },
};
const lookStatus = inject("lookStatus");
const ruleForm = inject("ruleForm");
const addStatus = inject("addStatus");

const formData = ref({});
const labelOption = (num) => {
  switch (num) {
    case 1:
      return [
        { label: "精度优先", value: 0 },
        { label: "速度优先", value: 2 },
      ];
    case 2:
      return [
        { label: "CW（顺时针）", value: 0 },
        { label: "CCW（逆时针）", value: 1 },
      ];
    case 9:
      return [
        { label: "不开启", value: 0 },
        { label: "开启", value: 1 },
      ];
    case 17:
      return [
        { label: "CW（顺时针）", value: 0 },
        { label: "CCW（逆时针）", value: 1 },
      ];
  }
};
const showLable = (key) => {
  switch (key) {
    case "DDLSD1":
      return "拧紧的模式";
    case "DDLSD2":
      return "拧紧旋转方向";
    case "DDLSD3":
      return "目标扭力";
    case "DDLSD4":
      return "扭力保持时间";
    case "DDLSD5":
      return "浮高界定圈数";
    case "DDLSD6":
      return "滑牙界定圈数";
    case "DDLSD7":
      return "触发速度切换的扭力比值";
    case "DDLSD8":
      return "扭力补偿值";
    case "DDLSD9":
      return "开启浮高滑牙检测";
    case "DDLSD10":
      return "触发速度切换的扭力";
    case "DDLSD11":
      return "触发速度切换的速度比值";
    case "DDLSD12":
      return "扭力偏差上限";
    case "DDLSD13":
      return "扭力偏差下限";
    case "DDLSD14":
      return "扭力免检圈数";
    case "DDLSD15":
      return "拧松有效触发的扭力阀值";
    case "DDLSD16":
      return "拧松有效触发的保持时间";
    case "DDLSD17":
      return "自由旋转方向";
    case "DDLSD18":
      return "STEP-00拧紧圈数";
    case "DDLSD19":
      return "STEP-00拧紧速度";
    case "DDLSD20":
      return "STEP-01拧紧圈数";
    case "DDLSD21":
      return "STEP-01拧紧速度";
    case "DDLSD22":
      return "STEP-02拧紧圈数";
    case "DDLSD23":
      return "STEP-02拧紧速度";
    case "DDLSD24":
      return "STEP-03拧紧圈数";
    case "DDLSD25":
      return "STEP-03拧紧速度";
    case "DDLSD26":
      return "STEP-04拧紧圈数";
    case "DDLSD27":
      return "STEP-04拧紧速度";
    case "DDLSD28":
      return "STEP-00拧松圈数";
    case "DDLSD29":
      return "STEP-00拧松速度";
    case "DDLSD30":
      return "STEP-01拧松圈数";
    case "DDLSD31":
      return "STEP-01拧松速度";
    case "DDLSD32":
      return "STEP-02拧松圈数";
    case "DDLSD33":
      return "STEP-02拧松速度";
    case "DDLSD34":
      return "STEP-00自由圈数";
    case "DDLSD35":
      return "STEP-00自由速度";
    default:
      return key;
  }
};
//获取当前 表格数据
const getFormData = () => {
  return JSON.stringify(formData.value);
};
const dialog = inject("dialog");
const checkData = (index) => {
  return new Promise(async (resolve, reject) => {
    await formRef.value.validate((valid) => {
      if (valid) {
        resolve();
      } else {
        reject(index);
      }
    });
  });
};
watch(
  () => dialog.visible,
  (value) => {
    if (value == true) {
      if (
        JSON.stringify(ruleForm.value[`task${props.propIndex}`]) == "{}" &&
        addStatus.value == true
      ) {
        formData.value = {
          DDLSD1: 0,
          DDLSD2: 1,
          DDLSD3: 6.0,
          DDLSD4: 60,
          DDLSD5: 2.0,
          DDLSD6: 4.0,
          DDLSD7: 60,
          DDLSD8: 0,
          DDLSD9: 0,
          DDLSD10: 300,
          DDLSD11: 100,
          DDLSD12: 15.0,
          DDLSD13: 15.0,
          DDLSD14: 4.5,
          DDLSD15: 0,
          DDLSD16: 0,
          DDLSD17: 1,
          DDLSD18: 0.25,
          DDLSD19: 60,
          DDLSD20: 5.5,
          DDLSD21: 800,
          DDLSD22: 6.0,
          DDLSD23: 300,
          DDLSD24: 0.0,
          DDLSD25: 0,
          DDLSD26: 0.0,
          DDLSD27: 0,
          DDLSD28: 8.0,
          DDLSD29: 360,
          DDLSD30: 0.0,
          DDLSD31: 0,
          DDLSD32: 0.0,
          DDLSD33: 0,
          DDLSD34: 0.0,
          DDLSD35: 360,
        };
      } else {
        formData.value = JSON.parse(ruleForm.value[`task${props.propIndex}`]);
      }
    }
  },
  { immediate: true, deep: true }
);
defineExpose({
  getFormData,
  checkData,
});
</script>
<style lang="scss" scoped></style>
