<template>
  <el-form
    ref="ruleForm"
    :loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="100px"
    style="width: 100%"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="12">
        <el-form-item label="公司" prop="wsm_supplierNo">
          <search-supplier
            :size="searchSize"
            :value="ruleForm.wsm_supplierNo"
            :disabled="
              !(status === '2' && powers.some((item) => item == '040'))
            "
            :placeholder="'仓库所在公司'"
            :names="wsm_supplier_name"
            :isDetail="
              !(status === '2' && powers.some((item) => item == '040'))
            "
            @searchChange="supplierChange"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="仓库" prop="wsm_code">
          <search-stock
            :size="searchSize"
            :value="ruleForm.wsm_code"
            :placeholder="'退库仓库'"
            :disabled="
              !(status === '2' && powers.some((item) => item == '040'))
            "
            :isDetail="
              !(status === '2' && powers.some((item) => item == '040'))
            "
            :isRelation="true"
            :names="wsm_code_name"
            :companyCode="wsm_supplier_code"
            @searchChange="stockChange"
          />
        </el-form-item>
      </el-col>
      <el-col v-if="ruleForm.wsm_code && ruleForm.wsm_code.length === 1">
        <el-col :span="12">
          <el-form-item label="收件人">
            <el-input
              disabled
              v-model="ruleForm.contactor"
              placeholder="收件人"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人电话">
            <el-input
              disabled
              v-model="ruleForm.mobile"
              placeholder="收件人电话"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="退回地址">
            <el-input
              disabled
              v-model="ruleForm.addr"
              maxlength="250"
              placeholder="退回地址"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col
        :span="24"
        style="text-align: right; margin: 0 0 20px 0"
        v-if="status === '2' && powers.some((item) => item == '040')"
      >
        <el-button type="primary" @click="submitForm" size="mini"
          >保 存</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/sellAfterApply";
import resToken from "@/mixins/resToken";
export default {
  name: "sellAfterApplyDetail",
  props: ["id", "newTime", "sitem"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      status: "", //存储详情接口返的状态
      showModelThis: this.showModel,
      wsm_supplier_name: "",
      wsm_code_name: "",
      ruleForm: {
        wsm_supplier: "",
        wsm_supplierNo: [],
        wsm_code: [],
        wsm_codeNo: "",
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        wsm_supplierNo: [
          {
            type: "array",
            required: true,
            trigger: "change",
            message: "请选择仓库所在公司",
          },
        ],
        wsm_code: [
          {
            type: "array",
            required: true,
            trigger: "change",
            message: "请选择退货仓库",
          },
        ],
      },
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
    };
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "sellAfterApplyDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  mounted() {
    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();
          const {
            status,
            returnCode,
            return_wsm,
            addr,
            mobile,
            contactor,
            wsm_supplierNo,
            wsm_supplier,
            wsm_name,
          } = this.sitem;
          this.status = status;
          this.wsm_supplier_name = wsm_supplier;
          this.wsm_code_name = wsm_name;
          this.wsm_supplier_code = wsm_supplierNo;
          this.ruleForm = {
            returnCode: returnCode || "",
            wsm_code: return_wsm ? [return_wsm] : [],
            wsm_supplierNo: wsm_supplierNo ? [wsm_supplierNo] : [],
            contactor: contactor || "",
            mobile: mobile || "",
            addr: addr || "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;

          let model = {
            returnCode: this.ruleForm.returnCode,
            wsm_code: this.ruleForm.wsm_codeNo,
          };
          const res = await asyncRequest.afterFeed(model);
          this.loading = false;
          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 supplierChange(e) {
      if (e && e.id) {
        this.wsm_supplier_code = e.code;
        this.ruleForm.wsm_supplierNo = [e.code];
      } else {
        this.ruleForm.wsm_supplier = [];
        this.wsm_supplier_code = "";
      }
      this.ruleForm.wsm_code = "";
      this.wsm_code = [];
      this.ruleForm.contactor = "";
      this.ruleForm.mobile = "";
      this.ruleForm.addr = "";
    },
    async stockChange(e) {
      this.ruleForm.wsm_codeNo = e && e.id ? e.code : "";
      this.ruleForm.wsm_code = e && e.id ? [e.code] : [];
      this.ruleForm.contactor = e && e.id ? e.contactor : "";
      this.ruleForm.mobile = e && e.id ? e.mobile : "";
      this.ruleForm.addr = e && e.id ? e.addr : "";
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
