<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: zwq
 * @LastEditTime: 2023-06-27 15:57:01
 * @Description:
-->
<template>
  <el-dialog :title="$t('module.basicData.bindMachine')" :visible.sync="visible">
    <el-transfer
      v-model="machineArr"
      :titles="[$t('module.basicData.machineList'), $t('module.basicData.bindMachineList')]"
      :props="{
        key: 'id',
        label: 'name',
      }"
      :data="platArr"
    ></el-transfer>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('cancel')}}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{$t('save')}}</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>
