<script>
import materialApi from "@/api/material/basic";
export default {
  name: "IsUsing",
  props: {
    selectData: {
      type: [Array, Object],
      required: true,
    },
  },
  computed: {
    innerValue: {
      get() {
        let { selectData } = this.$props;
        if (Array.isArray(selectData)) {
          return selectData;
        } else {
          return [{ ...selectData }];
        }
      },
      set() {},
    },
    disabled: {
      get() {
        if (this.innerValue.length === 1) {
          return false;
        }
        return true;
      },
      set() {},
    },
  },
  watch: {},
  data() {
    return {};
  },
  methods: {
    async handleCommand(command) {
      let { id, isEnable } = this.innerValue[0];
      if (command === "0" && isEnable == "已启用") return;

      if (command === "4") {
        await this.purShutdown(id);
      } else {
        let params = {
          isEnable:
            isEnable === "已停用"
              ? "0"
              : isEnable === "已采购停用"
              ? command
              : "2",
          id,
        };
        console.log(params, "params");
        await this.deactivate(params);
      }
    },
    // 停用启用
    async deactivate(prop) {
      console.log(prop, "停用启用");
      try {
        this.$modal.loading("加载中...");
        let { code, msg } = await materialApi.updateEnableMaterial(prop);
        if (code == 200) {
          this.$emit("success");
          this.$notify.success({
            message: msg,
          });
        }
      } catch (error) {
        console.log(error, "error");
      } finally {
        this.$modal.closeLoading();
      }
    },
    // 采购停用
    async purShutdown(prop) {
      console.log(prop, "采购停用");
      try {
        this.$modal.loading("加载中...");
        let { code, msg } = await materialApi.puNotEnable(prop);
        if (code == 200) {
          this.$emit("success");
          this.$notify.success({
            message: msg,
          });
        }
      } catch (error) {
        console.log(error, "error");
      } finally {
        this.$modal.closeLoading();
      }
    },
    dropDiabled(prop) {
      let data = this.innerValue[0];
      if (data) {
        switch (prop) {
          // 启用
          case "0":
            if (data.isEnable !== "已启用") return false;
            return true;
          // 停用
          case "2":
            if (data.isEnable !== "已停用") return false;
            return true;
          // 采购停用
          case "4":
            if (data.isEnable === "已启用") return false;
            return true;
          default:
            return true;
        }
      }
    },
  },
  created() {},
};
</script>
<template>
  <el-dropdown
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="handleCommand('0')"
    @command="handleCommand"
    split-button
  >
    <span
      style="display: inline-block; width: 100%; height: 100%"
      :disabled="dropDiabled('0')"
      v-hasPermi="['system:material:add']"
    >
      启 用
    </span>

    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item
        command="2"
        :disabled="dropDiabled('2')"
        v-hasPermi="['system:material:add']"
        >停 用</el-dropdown-item
      >
      <el-dropdown-item
        command="4"
        :disabled="dropDiabled('4')"
        v-hasPermi="['system:material:puNotEnable']"
        >采购停用</el-dropdown-item
      >
    </el-dropdown-menu>
  </el-dropdown>
</template>
