<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rulesThis"
      status-icon
      ref="ruleForm"
      label-width="80px"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="盘点公司" prop="wsm_supplier">
            <search-supplier
              :value="ruleForm.wsm_supplier"
              :placeholder="'请选择盘点公司'"
              @searchChange="supplierChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="盘点仓库" prop="wsm_code">
            <search-stock
              :value="ruleForm.wsm_code"
              :isDetail="true"
              :placeholder="'请选择盘点仓库'"
              :isRelation="true"
              :companyCode="companyCode"
              :names="''"
              @searchChange="stockChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="盘点类型" prop="type">
            <el-select
              v-model="ruleForm.type"
              placeholder="请选择盘点类型"
              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" v-if="!isDetail"
            >保 存
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <search-good-modal
      :show-model="showGoodsModel"
      @cancel="showGoodsModel = false"
      @resultList="addGoodsRes"
    />
  </div>
</template>
   <script>
import asyncRequest from "@/apis/service/stock/check";
import resToken from "@/mixins/resToken";
import columns from "./columns";
export default {
  name: "addEdit",
  props: ["id"],
  mixins: [resToken],
  data() {
    return {
      companyCode: "",
      loading: false,
      showGoodsModel: false,
      typeList: [
        {
          value: "1",
          label: "全盘",
        },
        {
          value: "2",
          label: "抽盘",
        },
      ],
      columns: columns,
      ruleForm: {
        wsm_supplier: [], //供应商
        wsm_code: [], // 盘点仓库
        type: "2", // 盘点类型
        // good_type_code: [], //商品列表
      },
      rulesThis: this.rules,
      rules: {
        wsm_supplier: [
          {
            type: "array",
            required: true,
            message: "请选择盘点公司",
            trigger: "change",
          },
        ],
        wsm_code: [
          {
            type: "array",
            required: true,
            message: "请选择盘点仓库",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择盘点类型",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.initForm();
  },
  watch: {
    id: function (val) {
      console.log(val);
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    //供应商选择
    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");
    },
    deleteById(index) {
      this.ruleForm.good_type_code.splice(index, 1);
      this.$refs.ruleForm.validateField("good_type_code");
    },
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        this.rulesThis = this.rules;
        await this.resetForm();
        await this.initData();
      }
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        this.ruleForm = res.data;
        this.ruleForm.role_id = this.ruleForm.role;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();

          this.ruleForm = {
            wsm_supplier: [], // 盘点公司
            wsm_code: [], // 盘点仓库
            type: "2", // 真实姓名
          };
        }
      });
    },
    getId(list) {
      let arr = JSON.parse(JSON.stringify(list));
      return arr.join(",");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { wsm_code, type, good_type_code } = JSON.parse(
            JSON.stringify(this.ruleForm)
          );
          const model = {
            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.$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>
   