<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="18vh"
    width="750px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="closeModel"
  >
    <el-card style="margin-top: -25px">
      <el-row :gutter="10">
        <el-col :span="24" v-if="id && id.id">
          <el-form
            label-position="left"
            label-width="100px"
            size="mini"
            inline
            class="demo-table-expand"
          >
            <el-col :span="12">
              <el-form-item label="账户名">
                <span>{{ id.username }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户名">
                <span>{{ id.nickname }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式">
                <span>{{ id.mobile }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="可用库存">
                <span>{{ id.stock_balance }}</span>
              </el-form-item>
            </el-col>
          </el-form>
        </el-col>

        <el-col :span="24">
          <el-divider />
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="200px"
            class="demo-ruleForm"
          >
            <el-form-item label="可用库存变动数量" required>
              <ul class="stock-edit">
                <li>
                  <el-select
                    slot="prepend"
                    v-model="ruleForm.type"
                    placeholder="请选择"
                  >
                    <el-option label="加 [+]" value="1" />
                    <el-option label="减 [-]" value="2" />
                  </el-select>
                </li>
                <li>
                  <el-input-number
                    v-model="ruleForm.stock"
                    :precision="0"
                    step-strictly
                    controls-position="right"
                    :min="0"
                    :max="10000"
                    :step="1"
                  ></el-input-number>
                </li>
              </ul>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="!isDetail" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/mobile/stock";
import resToken from "@/mixins/resToken";
export default {
  name: "stock",
  props: ["showModel", "id", "isDetail"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "添加可用库存",
      showModelThis: this.showModel,
      activeOptions: [],
      actionList: [],
      ruleForm: {
        type: "1",
        stock: 0,
      },
      rulesThis: this.rules,
      rules: {
        type: [
          { required: true, message: "请选择库存变动类型", trigger: "change" },
        ],
        stock: [{ required: true, message: "请输入变动数量", trigger: "bulr" }],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      this.loading = true;
      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.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            type: "1",
            stock: 0,
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.stock === 0) {
            this.$message.warning("库存的变动数量不能为零！");
            return;
          }

          this.loading = true;
          const obj = JSON.parse(JSON.stringify(this.ruleForm));
          const model = {
            id: this.id.id,
            type: obj.type,
            stock: obj.stock,
          };
          let res = await asyncRequest.update(model);
          this.loading = false;
          if (res && res.code === 0) {
            const title = "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.stock {
  .stock-edit {
    width: 100%;
    display: flex;
    li {
      &:first-child {
        width: 100px;
        position: relative;
      }
      &:last-child {
        width: calc(100% - 100px);
      }
    }
  }
}
</style>
