<template>
  <el-form
    :model="ruleForm"
    status-icon
    ref="ruleForm"
    :size="'mini'"
    :rules="rulesThis"
    label-width="95px"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="5">
        <el-form-item label="供应商反馈" prop="is_th">
          <el-select
            v-model="ruleForm.is_th"
            placeholder="供应商反馈"
            :disabled="disabled"
            @change="is_th_change"
          >
            <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="19" v-show="ruleForm.is_th === '1'">
        <el-form-item label="退货备注" prop="remark">
          <el-input
            type="textarea"
            maxlength="250"
            :disabled="disabled"
            show-word-limit
            placeholder="请填写备注"
            v-model="ruleForm.remark"
          />
        </el-form-item>
      </el-col>
      <el-col :span="11" v-show="ruleForm.is_th === '0'">
        <el-form-item label="仓库信息" prop="return_company">
          <search-supplier
            :size="'mini'"
            :disabled="disabled"
            :isDetail="disabled"
            :placeholder="'仓库所在公司'"
            :value="ruleForm.return_company"
            :names="disabled ? wsm_supplier : ''"
            @searchChange="selectSupplierStock"
          />
        </el-form-item>
      </el-col>
      <el-col :span="8" v-show="ruleForm.is_th === '0'">
        <el-form-item prop="return_wsm" label-width="10px">
          <!-- :disabled="sitem.companyNo === ''" -->
          <!-- :companyNo="sitem.companyNo" -->
          <search-stock
            :size="'mini'"
            :disabled="disabled || ruleForm.return_company.length === 0"
            :value="ruleForm.return_wsm"
            :placeholder="'仓库名称'"
            :isRelation="true"
            :isDetail="disabled"
            :wsmtype="'5'"
            :companyNo="sitem.supplierNo"
            :companyCode="return_company"
            :names="disabled ? wsm_name : ''"
            @searchChange="change_return_wsm"
          />
        </el-form-item>
      </el-col>
      <el-col :span="24" style="text-align: right; padding: 0 0 15px 0" v-if="!disabled">
        <el-button type="primary" @click="submitForm" :size="'mini'">保 存 </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import asyncRequest from "@/apis/service/sellOut/sellReturn";
import resToken from "@/mixins/resToken";
export default {
  name: "sellReturn",
  props: ["showModel", "disabled", "sitem", "newTime"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      wsm_supplier: "",
      wsm_name: "",
      showModelThis: this.showModel,
      options: [
        {
          value: "1",
          label: "同意退回",
        },
        {
          value: "0",
          label: "不同意退回",
        },
      ],
      return_company: "",
      ruleForm: {
        is_th: "1",
        returnCode: "", //发货申请单编号
        remark: "", //退货备注
        return_company: [],
        return_wsm: [], //退货原因
      },
      rulesThis: this.rules,
      rules: {
        returnCode: [
          {
            required: true,
            message: "退货单编号不能为空！",
            trigger: "blur",
          },
        ],
        is_th: [
          {
            required: true,
            message: "请选择供应商反馈意见！",
            trigger: "change",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入退货备注！",
            trigger: "blur",
          },
        ],
        return_company: [
          {
            type: "array",
            required: true,
            message: "请选择仓库所在公司！",
            trigger: "change",
          },
        ],
        return_wsm: [
          {
            type: "array",
            required: true,
            message: "请选择供仓库！",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initForm();
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },

  methods: {
    async initForm() {
      this.loading = true;
      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();
          console.log(this.sitem);
          const {
            returnCode,
            error_msg,
            remark,
            is_th,
            return_wsm,
            wsm_supplierNo,
            wsm_supplier,
            wsm_name,
          } = this.sitem;
          this.wsm_supplier = wsm_supplier || "";
          this.wsm_name = wsm_name || "";
          console.log(this.disabled, this.wsm_supplier, this.wsm_name);
          this.ruleForm = {
            is_th: is_th || "1",
            returnCode: returnCode || "",
            remark: remark || "", //退货备注
            error_msg: error_msg || "", //退货原因
            return_company: wsm_supplierNo ? [wsm_supplierNo] : [],
            return_wsm: return_wsm ? [return_wsm] : [],
          };
          this.is_th_change();
        }
      });
    },
    // 选择仓库
    change_return_wsm(e) {
      console.log(e);
      const { code } = e;
      this.ruleForm.return_wsm = code ? [code] : [];
      console.log(this.ruleForm.return_wsm);
      this.$refs.ruleForm.validateField("return_wsm");
    },
    // 选择供应商
    selectSupplierStock(e) {
      const { code } = e;
      this.ruleForm.return_company = code ? [code] : [];
      this.return_company = code || "";
      this.ruleForm.return_wsm = [];
      this.$refs.ruleForm.validateField("return_company");
      this.$refs.ruleForm.validateField("return_wsm");
    },
    is_th_change() {
      const { is_th } = this.ruleForm;
      this.rulesThis.remark[0].required = is_th === "1";
      this.rulesThis.return_company[0].required = is_th === "0";
      this.rulesThis.return_wsm[0].required = is_th === "0";
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          if (model.is_th === "0") {
            model.remark = "";
          } else {
            model.return_wsm = [];
          }
          delete model["return_company"];
          model.status = "3";
          model.return_wsm = model.return_wsm.toString();
          const { code, data, message } = await asyncRequest.status(model);
          this.loading = false;
          if (code === 0) {
            this.$notify.success({
              title: "提交成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh", false);
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style></style>
