<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"
              :disabled="status !== '0'"
              :isDetail="id !== 'add'"
              :placeholder="'请选择盘点公司'"
              :names="companyName"
              @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"
              :disabled="status !== '0'"
              :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="请选择盘点类型"
              :disabled="status !== '0'"
              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
            @click="deleteById()"
            plain
            v-if="status !== '4'"
            type="danger"
            >作废盘点申请
          </el-button>
          <el-button
            type="primary"
            v-if="status === '1'"
            plain
            @click="submitForm"
            >取消流程
          </el-button>
          <el-button type="primary" @click="submitForm" v-if="status === '0'"
            >保 存
          </el-button>
          <el-button
            type="primary"
            @click="submitForm(true)"
            v-if="status === '0'"
            >保存并发起流程
          </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/detail";
import resToken from "@/mixins/resToken";
import columns from "./columns";
export default {
  name: "addEdit",
  props: ["id"],
  mixins: [resToken],
  data() {
    return {
      status: "0",
      companyCode: "",
      companyName: "",
      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");
    },
    async deleteById() {
      await this.$confirm("确定要作废？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            id: this.id,
          };
          const res = await asyncRequest.delete(model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: "作废成功！",
              message: "",
            });
            this.routeGoto("check", {});
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async initForm() {
      this.loading = true;
      if (this.id === "add") {
        this.status = "0";
        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) {
        const { status, type, wsm_code } = res.data;
        this.status = status;
        this.companyCode = "GYS-20210517-0009";
        this.companyName = "北京长跃科技发展有限公司";
        this.ruleForm = {
          wsm_code: [wsm_code],
          type: type,
          wsm_supplier: [this.companyCode],
        };
      } 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(status) {
      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");
            if (status) {
              await this.setProcess("1");
            }
          } 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;
        }
      });
    },
    async setProcess(status) {
      if (this.loading === false) {
        this.loading = true;
      }
      let model = {
        id: this.id,
        status: status,
      };

      let res = await asyncRequest.update(model);
      if (res && res.code === 0) {
        const title =
          status === "1" ? "发起审核流程成功！" : "取消审核流程成功！";
        this.$notify.success({
          title,
          message: "",
        });
        this.initForm();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
      this.loading = false;
    },
  },
};
</script>

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