<template>
  <el-form
    ref="ruleForm"
    :loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    label-width="135px"
    style="width: 100%"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="8">
        <el-form-item label="采购总数" prop="cgd_total" required>
          <el-input
            disabled
            v-model="ruleForm.cgd_total"
            placeholder="采购总数"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="已入库商品数" prop="cgd_send" required>
          <el-input
            disabled
            v-model="ruleForm.cgd_send"
            placeholder="已入库商品数"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="未入库商品数" prop="cgd_wsend" required>
          <el-input
            disabled
            v-model="ruleForm.cgd_wsend"
            placeholder="未入库商品数"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="未入库数量可退否" prop="wsend_th">
          <el-select
            v-model="ruleForm.wsend_th"
            style="width: 100%"
            placeholder="未入库数量可退否"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="16">
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            placeholder="备注"
            v-model="ruleForm.remark"
            :disabled="disabled"
            maxlength="250"
            show-word-limit
          />
        </el-form-item>
      </el-col>

      <el-col :span="24" style="text-align: right; padding: 0 0 20px 0">
        <el-button type="primary" :size="'mini'" @click="submitForm"
          >保 存</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/zxReturn";
import resToken from "@/mixins/resToken";
export default {
  name: "handover",
  props: ["id", "sitem", "showModel", "newTime"],
  mixins: [resToken],
  data() {
    return {
      options: [
        {
          value: "1",
          label: "是",
        },
        {
          value: "0",
          label: "否",
        },
      ],
      loading: false,

      status: "",
      showModelThis: this.showModel,

      ruleForm: {
        returnCode: "",
        cgd_total: "",
        cgd_wsend: "",
        cgd_send: "",
        wsend_th: "1",
        remark: "",
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        wsend_th: [
          {
            required: true,
            message: "请选择未入库数量可退否",
            trigger: "change",
          },
        ],

        remark: [
          {
            required: true,
            message: "请输入备注",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "zxReturnDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  mounted() {
    this.initForm();
  },
  watch: {
    id: function (val) {
      if (val) {
        this.initForm();
      }
    },
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      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();
          let {
            cgd_wsend,
            cgd_send,
            status,
            cgd_total,
            wsend_th,
            remark,
            returnCode,
          } = this.sitem;
          this.status = status;
          this.ruleForm = {
            returnCode: returnCode || "",
            cgd_total: cgd_total || "",
            cgd_wsend: cgd_wsend || "",
            cgd_send: cgd_send || "",
            wsend_th: wsend_th || "1",
            remark: remark || "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          model.status = model.wsend_th === "1" ? "3" : "4";
          delete model["cgd_total"];
          delete model[" cgd_wsend"];
          delete model[" cgd_send"];
          const res = await asyncRequest.status(model);

          if (res && res.code === 0) {
            this.$notify.success({
              title: "提交成功",
              message: "",
            });
            this.$emit("refresh", false);
          } 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 statusConfirm(status, message) {
      await this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (status === "-1") {
            await this.deleteById(message);
          } else {
            await this.setStatus(status, message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },

    async deleteById(message) {
      const res = await asyncRequest.delete({ id: this.id });
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });

        this.routeGoto("handover", {});
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async setStatus(status, message) {
      let model = {
        id: this.id,
        remark: "",
        status: status,
      };
      const res = await asyncRequest.status(model);
      this.loading = false;
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });
        this.initForm();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-form-item-error-img {
  width: 100%;
  ul {
    width: 100%;
    li {
      float: left;
      border: 1px solid rgb(220, 223, 230);
      // border-left: 0;
      box-sizing: border-box;
      width: 254px;
      height: 164px;
      line-height: 164px;
      position: relative;
      img {
        display: inline-block;
        width: 100%;
        height: 100%;
      }
      .el-icon-close {
        position: absolute;
        top: 0;
        right: 0;
        z-index: 2;
        width: 25px;
        height: 25px;
        text-align: center;
        line-height: 25px;
        font-size: 19px;
        &:hover {
          color: #63cbe7;
        }
      }
    }
  }
}
.images_li {
  // width: 254px;
  // height: 164px;
  float: left;
}
.btnupload {
  float: left;
  border: 1px solid rgb(220, 223, 230);
  // border-left: 0;
  box-sizing: border-box;
  width: 254px;
  height: 164px;
  line-height: 164px;
  text-align: center;
}
.Upload {
  width: 254px;
  height: 164px;
  line-height: 164px;
  text-align: center;
  position: absolute;
  line-height: 0px;
  top: 0;
  left: 0;
  z-index: 2;
  line-height: 164px;
}
.avatar-uploader-icon {
  font-size: 33px;
  color: #8c939d;
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
</style>
