<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rulesThis"
      status-icon
      :size="'mini'"
      ref="ruleForm"
      label-width="80px"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col :span="id === 'add' ? 24 : 12">
          <el-form-item label="业务公司" prop="companyNo">
            <search-work-company
              :value="ruleForm.companyNo"
              :size="'mini'"
              :is-detail="id !== 'add'"
              :placeholder="'业务公司'"
              @searchChange="companyNoChange($event, 'companyNo')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="id === 'add' ? 24 : 12">
          <el-form-item label="盘点公司" prop="wsm_supplier">
            <search-supplier
              :value="ruleForm.wsm_supplier"
              :size="'mini'"
              :disabled="ruleForm.companyNo === ''"
              :is-detail="id !== 'add'"
              :placeholder="'盘点公司'"
              :names="companyName"
              @searchChange="supplierChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="id === 'add' ? 24 : 12">
          <el-form-item label="盘点仓库" prop="wsm_code">
            <search-stock
              :value="ruleForm.wsm_code"
              :size="'mini'"
              :disabled="
                !(
                  id === 'add' ||
                  (status === '0' && powers.some((i) => i == '005'))
                )
              "
              :is-detail="id !== 'add'"
              :placeholder="'盘点仓库'"
              :isRelation="true"
              :companyCode="companyCode"
              :names="''"
              @searchChange="stockChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="id === 'add' ? 24 : 12">
          <el-form-item label="盘点类型" prop="type">
            <el-select
              v-model="ruleForm.type"
              placeholder="盘点类型"
              :disabled="
                !(
                  id === 'add' ||
                  (status === '0' && powers.some((i) => i == '005'))
                )
              "
              style="width: 100%"
            >
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            type="primary"
            @click="submitForm"
            :size="'mini'"
            v-if="
              id === 'add' ||
              (status === '0' && powers.some((i) => i == '005'))
            "
            >保 存
          </el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
   <script>
import asyncRequest from "@/apis/service/stock/check/detail";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
import columns from "./columns";
export default {
  name: "addEdit",
  props: ["id", "sitem", "newTime"],
  computed: {
    ...mapGetters(["business_companyNo"]),
  },
  mixins: [resToken],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "checkDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      status: "",
      companyCode: "",
      companyName: "",
      loading: false,
      typeList: [
        {
          value: "2",
          label: "抽盘",
        },
        {
          value: "1",
          label: "全盘",
        },
      ],
      columns: columns,
      ruleForm: {
        companyNo: "",
        wsm_supplier: [], //供应商
        wsm_code: [], // 盘点仓库
        type: "2", // 盘点类型
        // good_type_code: [], //商品列表
      },
      rulesThis: this.rules,
      rules: {
        companyNo: [
          {
            required: true,
            message: "业务公司",
            trigger: "change",
          },
        ],
        wsm_supplier: [
          {
            type: "array",
            required: true,
            message: "盘点公司",
            trigger: "change",
          },
        ],
        wsm_code: [
          {
            type: "array",
            required: true,
            message: "盘点仓库",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "盘点类型",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initForm();
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    //业务公司选择
    async companyNoChange(e, key) {
      const { code } = e;
      this.ruleForm[key] = code || ""; //业务公司编码
      console.log(this.ruleForm[key]);
      this.$refs.ruleForm.validateField(key);
    },
    //供应商选择
    supplierChange(e) {
      if (e && e.id) {
        this.ruleForm.wsm_supplier = [e.code];
        this.companyCode = e.code;
      } else {
        this.ruleForm.wsm_supplier = [];
        this.companyCode = "";
      }
      this.ruleForm.wsm_code = [];
      this.$refs.ruleForm.validateField("wsm_supplier");
      this.$refs.ruleForm.validateField("wsm_code");
    },
    //仓库选择
    stockChange(e) {
      if (e && e.id) {
        this.ruleForm.wsm_code = [e.code];
      } else {
        this.ruleForm.wsm_code = [];
      }
      this.$refs.ruleForm.validateField("wsm_code");
    },
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.status = "";
      }
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { companyNo, status, type, wsm_code, code, name } = this.sitem;
          this.status = status || "";
          this.companyCode = code || "";
          this.companyName = name || "";
          this.ruleForm = {
            companyNo: companyNo || this.business_companyNo || "",
            wsm_code: wsm_code ? [wsm_code] : [],
            type: type || "2",
            wsm_supplier: this.companyCode ? [this.companyCode] : [],
          };
        }
      });
    },
    getId(list) {
      let arr = JSON.parse(JSON.stringify(list));
      return arr.join(",");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const { wsm_code, type, companyNo } = JSON.parse(
            JSON.stringify(this.ruleForm)
          );
          const model = {
            companyNo: companyNo,
            id: this.id,
            wsm_code: this.getId(wsm_code) || "", // 盘点仓库
            type: type || "", // 盘点类型
          };
          let res = {};
          if (this.id === "add") {
            delete model["id"];
            res = await asyncRequest.add(model);
          } else {
            res = await asyncRequest.update(model);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.check {
}
</style>
   