<template>
  <div>
    <el-card class="box-card">
      <byTable
        :hideTable="true"
        :hidePagination="true"
        :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>
      <template v-if="sourceList.data && sourceList.data.length > 0">
        <el-card v-for="(item, index) in sourceList.data" :key="index" style="margin-bottom: 20px;">
          <el-row :gutter="10">
            <el-col :span="4">产线编码: {{ item.code }}</el-col>
            <el-col :span="4">产线名称: {{ item.name }}</el-col>
            <el-col :span="4">
              <span>批量模式: </span>
              <span v-if="item.batchMode === 1" style="color: #03cf00">开启批量模式</span>
              <span v-else style="color: #ff0000">关闭批量模式</span>
            </el-col>
            <el-col :span="7">备注: {{ item.remark }}</el-col>
            <el-col :span="5" style="text-align: right">
              <el-button type="primary" @click="clickUpdate(item)" text>批量模式</el-button>
              <el-button type="primary" @click="clickUpdate(item)" text>编辑</el-button>
              <el-button type="danger" @click="clickDelete(item)" text>删除</el-button>
            </el-col>
          </el-row>
          <div class="process">
            <span v-for="(line, j) in item.craftProductionLineProcessList" :key="line.id">
              <span style="padding: 0px 30px; color: #1890ff">{{ line.craftProcessName }}</span>
              <span v-if="j < item.craftProductionLineProcessList.length - 1">></span>
            </span>
          </div>
        </el-card>
      </template>
    </el-card>

    <el-dialog :title="modalType == 'add' ? '添加产线' : '编辑产线'" v-if="openDialog" v-model="openDialog" width="70%">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
        <template #batchMode>
          <div style="width: 100%">
            <el-radio-group v-model="formData.data.batchMode" prop="batchMode">
              <el-radio :label="0">关闭批量模式</el-radio>
              <el-radio :label="1">开启批量模式</el-radio>
            </el-radio-group>
          </div>
        </template>
        <template #tableList>
          <div style="width: 100%">
            <div style="margin-bottom: 10px">
              <el-button type="primary" @click="addStep()">添加步骤</el-button>
            </div>
            <el-table :data="formData.data.craftProductionLineProcessList" :row-style="{ height: '35px' }" header-row-class-name="tableHeader">
              <el-table-column label="工序" width="160">
                <template #default="{ row, $index }">
                  <el-form-item
                    :prop="'craftProductionLineProcessList.' + $index + '.craftProcessId'"
                    :rules="rules.craftProcessId"
                    :inline-message="true"
                    style="width: 100%">
                    <el-select v-model="row.craftProcessId" placeholder="工序" @change="changeCraftProcess($index)">
                      <el-option v-for="item in craftProcessList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="设备" width="160">
                <template #default="{ row, $index }">
                  <el-form-item
                    :prop="'craftProductionLineProcessList.' + $index + '.equipmentId'"
                    :rules="rules.equipmentId"
                    :inline-message="true"
                    style="width: 100%">
                    <el-select v-model="row.equipmentId" placeholder="设备">
                      <el-option v-for="item in selectableEquipment" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="rfidIp" width="160">
                <template #default="{ row, $index }">
                  <el-form-item :prop="'craftProductionLineProcessList.' + $index + '.rfidIp'" :rules="rules.rfidIp" :inline-message="true" style="width: 100%">
                    <el-input v-model="row.rfidIp" placeholder="rfidIp" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="rfid通道" width="160">
                <template #default="{ row, $index }">
                  <el-form-item
                    :prop="'craftProductionLineProcessList.' + $index + '.rfidPassage'"
                    :rules="rules.rfidPassage"
                    :inline-message="true"
                    style="width: 100%">
                    <el-input v-model="row.rfidPassage" placeholder="rfid通道" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="接口地址" min-width="160">
                <template #default="{ row, $index }">
                  <el-form-item
                    :prop="'craftProductionLineProcessList.' + $index + '.interfaceAddress'"
                    :rules="rules.interfaceAddress"
                    :inline-message="true"
                    style="width: 100%">
                    <el-input v-model="row.interfaceAddress" placeholder="接口地址" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" fixed="right" width="60">
                <template #default="{ $index }">
                  <el-button type="danger" @click="clickTableDelete($index)" text>删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </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 equipmentList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 99,
    code: "",
    name: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "产线编码",
    },
    {
      type: "input",
      prop: "name",
      label: "产线名称",
    },
  ];
});
const config = computed(() => {
  return [];
});
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("/craftProductionLine/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 getDemandData = () => {
  proxy.post("/craftProcess/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      craftProcessList.value = res.rows;
    }
  });
  proxy.post("/equipment/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      equipmentList.value = res.rows;
    }
  });
};
getDemandData();
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: {
    code: "",
    name: "",
    batchMode: 0,
    remark: "",
    craftProductionLineProcessList: [],
  },
});
const formConfig = computed(() => {
  return [
    {
      type: "title",
      title: "基本信息",
      label: "",
    },
    {
      type: "input",
      prop: "code",
      label: "产线编码",
      itemType: "text",
    },
    {
      type: "input",
      prop: "name",
      label: "产线名称",
      itemType: "text",
    },
    {
      type: "slot",
      slotName: "batchMode",
      prop: "batchMode",
      label: "批量模式",
    },
    {
      type: "input",
      prop: "remark",
      label: "备注",
      itemType: "textarea",
    },
    {
      type: "title",
      title: "产线信息",
      label: "",
    },
    {
      type: "slot",
      slotName: "tableList",
      label: "",
    },
  ];
});
const rules = ref({
  code: [{ required: true, message: "请输入产线编码", trigger: "blur" }],
  name: [{ required: true, message: "请输入产线名称", trigger: "blur" }],
  batchMode: [{ required: true, message: "请选择批量模式", trigger: "change" }],
  craftProcessId: [{ required: true, message: "请选择工序", trigger: "change" }],
  equipmentId: [{ required: true, message: "请选择设备", trigger: "change" }],
  rfidIp: [{ required: true, message: "请输入rfidIp", trigger: "blur" }],
});
const clickModal = () => {
  modalType.value = "add";
  formData.data = {
    code: "",
    name: "",
    batchMode: 0,
    remark: "",
    craftProductionLineProcessList: [],
  };
  openDialog.value = true;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    if (formData.data.craftProductionLineProcessList && formData.data.craftProductionLineProcessList.length > 0) {
      proxy.post("/craftProductionLine/" + modalType.value, formData.data).then(() => {
        ElMessage({
          message: modalType.value == "add" ? "添加成功" : "编辑成功",
          type: "success",
        });
        openDialog.value = false;
        getList();
      });
    } else {
      return ElMessage("请添加步骤");
    }
  });
};
const clickUpdate = (row) => {
  modalType.value = "edit";
  proxy.post("/craftProductionLine/detail", { id: row.id }).then((res) => {
    formData.data = res;
    openDialog.value = true;
  });
};
const clickDelete = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/craftProductionLine/delete", { id: row.id }).then(() => {
        ElMessage({ message: "删除成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
const addStep = () => {
  if (!(formData.data.craftProductionLineProcessList && formData.data.craftProductionLineProcessList.length > 0)) {
    formData.data.craftProductionLineProcessList = [];
  }
  formData.data.craftProductionLineProcessList.push({
    craftProcessId: "",
    equipmentId: "",
    rfidIp: "",
    rfidPassage: "",
    interfaceAddress: "",
  });
};
const clickTableDelete = (index) => {
  formData.data.craftProductionLineProcessList.splice(index, 1);
};
const selectableEquipment = ref([]);
const changeCraftProcess = (index) => {
  formData.data.craftProductionLineProcessList[index].equipmentId = "";
  if (formData.data.craftProductionLineProcessList[index].craftProcessId && equipmentList.value && equipmentList.value.length > 0) {
    selectableEquipment.value = equipmentList.value.filter(
      (item) => item.craftProcessId === formData.data.craftProductionLineProcessList[index].craftProcessId
    );
  } else {
    selectableEquipment.value = [];
  }
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
.process {
  display: inline-block;
  margin-top: 20px;
  padding: 10px 20px;
  background-color: rgb(245, 247, 250);
  border-radius: 2px;
}
</style>
