<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    status-icon
    ref="ruleForm"
    label-width="110px"
    :size="'mini'"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="12">
        <show-data-table
          :newTime="newTime"
          v-if="sitem.companyNo"
          :sitem="sitem"
          :columns="showColumns"
          border
        >
          <template slot="wsm_type">
            <el-tag :size="'mini'">销售仓</el-tag>
          </template>
        </show-data-table>
      </el-col>

      <el-col :span="12" style="text-align: right; padding: 0 0 20px 0">
        <el-form-item label="仓库所在公司" prop="supplier_code">
          <search-supplier
            :size="'mini'"
            :placeholder="'仓库所在公司'"
            :disabled="sitem.companyNo === ''"
            :value="ruleForm.supplier_code"
            :names="''"
            @searchChange="selectSupplierStock"
          />
        </el-form-item>
        <el-form-item label="仓库名称" prop="wsm_code">
          <search-stock
            :isDetail="false"
            :size="'mini'"
            :disabled="ruleForm.supplier_code.length === 0"
            :value="ruleForm.wsm_code"
            :placeholder="'仓库名称'"
            :isRelation="true"
            :wsmtype="'5'"
            :companyNo="sitem.companyNo"
            :companyCode="supplier_code"
            :names="''"
            @searchChange="change_wsm_code"
          />
        </el-form-item>
        <el-button type="primary" @click="submitForm" :size="'mini'"
          >保 存
        </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
   <script>
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/stockApply";
export default {
  name: "stockApply",
  props: ["id", "sitem", "newTime"],

  mixins: [resToken],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockApplyDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      showColumns: [
        {
          prop: "company_name",
          label: "业务公司",
          span: 24,
        },

        {
          prop: "companyNo",
          label: "业务公司编号",
          span: 16,
        },
        {
          prop: "wsm_type",
          label: "仓库类型",
          _slot_: "wsm_type",
          span: 8,
        },
      ],

      supplier_code: "",
      loading: false,
      ruleForm: {
        supplier_code: [],
        wsm_code: [],
      },
      rules: {
        supplier_code: [
          {
            type: "array",
            required: true,
            message: "请选择仓库所在公司",
            trigger: "change",
          },
        ],
        wsm_code: [
          {
            type: "array",
            required: true,
            message: "请选择入库仓库",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initForm();
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      await this.resetForm();
      this.loading = false;
    },

    //初始化表单
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.supplier_code = "";
          this.ruleForm = {
            supplier_code: [],
            wsm_code: [],
          };
        }
      });
    },
    // 保存更改
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let item = JSON.parse(JSON.stringify(this.ruleForm));
          item.wsm_code = item.wsm_code.toString();
          delete item["supplier_code"];
          item.bk_code = this.sitem.bk_code;
          const res = await asyncRequest.purchcgd(item);
          if (res && res.code === 0) {
            this.$notify.success({
              title: "添加成功",
              message: "",
            });
            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;
        }
      });
    },

    // 选择仓库
    change_wsm_code(e) {
      const { code } = e;
      this.ruleForm.wsm_code = code ? [code] : [];
      this.$refs.ruleForm.validateField("wsm_code");
    },
    // 选择供应商
    selectSupplierStock(e) {
      const { code } = e;
      this.ruleForm.supplier_code = code ? [code] : [];
      this.supplier_code = code || "";
      this.ruleForm.wsm_code = [];
      this.$refs.ruleForm.validateField("supplier_code");
      this.$refs.ruleForm.validateField("wsm_code");
    },
  },
};
</script>
   