<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: zwq
 * @LastEditTime: 2023-01-03 11:35:26
 * @Description:
-->
<template>
  <el-dialog title="绑定机台" :visible.sync="visible">
    <el-transfer
      v-model="machineArr"
      :titles="['总机台列表', '绑定机台列表']"
      :props="{
        key: 'id',
        label: 'name',
      }"
      :data="platArr"
    ></el-transfer>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        id: "",
      },
      machineArr: [],
      platArr: [],
      visible: false,
    };
  },
  methods: {
    setDataForm() {
      if (this.dataForm.machineId) {
        this.machineArr = this.dataForm.machineId.split(",");
      }else{
        this.machineArr = []
      }
    },
    init(id) {
      this.dataForm.id = id || "";
      this.visible = true;
      this.getArr();
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.$http
            .get("/basic/workingProcedure" + "/" + this.dataForm.id)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.dataForm = res.data;
              this.setDataForm();
            })
            .catch(() => {});
        }
      });
    },
    getArr() {
      this.platArr.splice(0,this.platArr.length)
      this.$http
        .get("/basic/machine/page", {
          params: {
            page: 1,
            limit: 500,
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.platArr = res.data.list;
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.machineId = this.machineArr.toString();
      this.$http.put("/basic/workingProcedure/", this.dataForm)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("successSubmit");
            },
          });
        })
        .catch(() => {});
    },
  },
};
</script>
