<template>
  <el-form
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    ref="ruleForm"
    label-width="110px"
    class="demo-ruleForm"
  >
    <!-- <p>采购工差单详情数据{{ sitem }}</p> -->
    <p>id{{ id }}</p>
    <el-form-item label="采购单信息">
      <!-- <purchase-order-addEdit
        style="padding: 0 0 20px 0"
        :newTime="newTime"
        v-if="newTime !== ''"
        :sitem="sitem"
        :id="queryId"
        @refresh="initData()"
      /> -->
    </el-form-item>
    <el-form-item label="采购单编号" prop="loginName">
      <el-input v-model="ruleForm.cgdNo" disabled></el-input>
    </el-form-item>
    <el-form-item label="商品分类" prop="loginName">
      <el-input v-model="ruleForm.loginName" disabled></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="loginName">
      <el-input v-model="ruleForm.loginName" disabled></el-input>
    </el-form-item>
    <el-form-item label="商品总克重" prop="loginName">
      <el-input v-model="ruleForm.loginName" disabled></el-input>
    </el-form-item>
    <el-form-item label="工差总克重" prop="loginName">
      <el-input v-model="ruleForm.num" :disabled="isDetail"></el-input>
    </el-form-item>
  </el-form>
</template>
   <script>
// import purchaseOrderAddEdit from "@/views/purchaseIn/components/purchase-order-addEdit";

import asyncRequest from "@/apis/service/purchaseIn/purchaseDiffOrder";
import resToken from "@/mixins/resToken";
import {
  isnumber,
  isMobile,
  validEmail,
  isAlphanumeric,
  isChinese,
  isEmoticon,
  validAlphabets,
} from "@/utils/validate";
export default {
  name: "purchaseDiffOrder",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  components: {
    // purchaseOrderAddEdit,
  },
  data() {
    return {
      loading: false,
      showModelThis: this.showModel,
      ruleForm: {
        cgdNo: "", //采购单编号

        // --
        num: "",
        loginName: "",
      },
      rulesThis: this.rules,
      rules: {
        num: [
          {
            required: true,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {
    this.initForm();
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      this.loading = true;
      // await this.getRole();
      if (this.id === "add") {
        this.title = "添加账号";
        this.rulesThis = this.rules;
        await this.resetForm();
      } else {
        if (this.isDetail) {
          this.title = "账号详情";
          this.rulesThis = {};
        } else {
          this.title = "修改账号";
          this.rulesThis = this.rules;
        }
        await this.resetForm(this.sitem);
        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(sitem) {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { loginName, num } = sitem;
          this.ruleForm = {
            loginName: loginName || "", // 账号
            num: num || "",
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { username, name, mobile, email, role_id, status } = JSON.parse(
            JSON.stringify(this.ruleForm)
          );
          const model = {
            id: this.id,
            username: username || "", // 账号
            nickname: name || "", // 真实姓名
            mobile: mobile || "",
            email: email || "",
            role: role_id || "",
            status: status || "",
          };
          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>
.purchaseDiffOrder {
}
</style>
   