<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      status-icon
      ref="ruleForm"
      label-width="100px"
      :size="'mini'"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col
          :span="12"
          v-if="status === '1' && powers.some((item) => item == '024')"
        >
          <el-form-item label="采购供应商" prop="supplierNo">
            <search-supplier
              :disabled="
                !(status == '1' && powers.some((item) => item == '024'))
              "
              :size="'mini'"
              :value="ruleForm.supplierNo"
              :placeholder="'请选择供应商'"
              :names="supplierName"
              :isDetail="!(status === '0' && status == '1')"
              @searchChange="selectSupplier"
            />
          </el-form-item>
        </el-col>
        <el-col
          :span="12"
          v-if="status === '1' && powers.some((item) => item == '024')"
        >
          <el-form-item label="仓库类型" prop="wsm_type">
            <el-select
              v-model="ruleForm.wsm_type"
              placeholder="仓库类型"
              style="width: 100%"
            >
              <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="12"
          v-if="
            status === '1' &&
            powers.some((item) => item == '024') &&
            ruleForm.wsm_type === '1'
          "
        >
          <el-form-item label="入库公司" prop="supplier_code">
            <search-supplier
              :disabled="
                !(status == '1' && powers.some((item) => item == '024'))
              "
              :value="ruleForm.supplier_code"
              :placeholder="'入库公司'"
              :names="''"
              :size="'mini'"
              @searchChange="selectSupplierStock"
            />
          </el-form-item>
        </el-col>
        <el-col
          :span="12"
          v-if="
            status === '1' &&
            powers.some((item) => item == '024') &&
            ruleForm.wsm_type === '1'
          "
        >
          <el-form-item label="入库仓库" prop="stock_code">
            <search-stock
              :disabled="
                !(status == '1' && powers.some((item) => item == '024'))
              "
              :value="ruleForm.stock_code"
              :placeholder="'入库仓库'"
              :isRelation="true"
              :names="''"
              :size="'mini'"
              :type="'1'"
              :companyCode="companyCode"
              @searchChange="selectStock"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库时间" prop="expecttime">
            <el-date-picker
              :disabled="
                !(status == '1' && powers.some((item) => item == '024'))
              "
              v-model="ruleForm.expecttime"
              type="datetime"
              style="width: 100%"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="预计入库时间"
              :picker-options="pickerOptions"
              @change="selectTime"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            status === '1' &&
            powers.some((item) => item == '024') &&
            goods_class === '8'
          "
          :span="8"
        >
          <el-form-item label="实时金价" prop="now_god_price" :size="'mini'">
            <el-input
              placeholder="0"
              disabled
              :size="'mini'"
              maxlength="100"
              v-model="ruleForm.now_god_price"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>

        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="商品重量" prop="weight" :size="'mini'">
            <el-input placeholder="0" :size="'mini'" v-model="ruleForm.weight">
              <template slot="append">g</template>
            </el-input>
          </el-form-item>
        </el-col>

        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="工艺费" prop="teach_fee" :size="'mini'">
            <el-input
              placeholder="0"
              v-model="ruleForm.teach_fee"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="包装费" prop="pakge_fee" :size="'mini'">
            <el-input
              placeholder="0"
              v-model="ruleForm.pakge_fee"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="加标费" prop="mark_fee" :size="'mini'">
            <el-input
              placeholder="0"
              v-model="ruleForm.mark_fee"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="证书费" prop="cert_fee" :size="'mini'">
            <el-input
              placeholder="0"
              v-model="ruleForm.cert_fee"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="物流费" prop="delivery_fee" :size="'mini'">
            <el-input
              placeholder="0"
              v-model="ruleForm.delivery_fee"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="开模费" prop="open_fee" :size="'mini'">
            <el-input
              placeholder="0"
              v-model="ruleForm.open_fee"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>

        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="工差金额" prop="diff_price" :size="'mini'">
            <el-input
              placeholder="0"
              v-model="ruleForm.diff_price"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="工差重量" prop="diff_weight" :size="'mini'">
            <el-input placeholder="0" v-model="ruleForm.diff_weight">
              <template slot="append">g</template>
            </el-input>
          </el-form-item>
        </el-col>

        <el-col
          v-if="status === '1' && powers.some((item) => item == '024')"
          :span="6"
        >
          <el-form-item label="裸价" prop="nake_fee" :size="'mini'">
            <el-input
              v-model="ruleForm.nake_fee"
              placeholder="0"
              :size="'mini'"
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col
          :span="
            status === '1' && powers.some((item) => item == '024') ? 12 : 12
          "
          ><el-form-item label="备注" prop="remark" :size="'mini'">
            <el-input
              :disabled="
                !(status == '1' && powers.some((item) => item == '024'))
              "
              type="textarea"
              :size="'mini'"
              :autosize="{ minRows: 2, maxRows: 4 }"
              v-model="ruleForm.remark"
            /> </el-form-item
        ></el-col>
        <el-col
          :span="12"
          style="text-align: right; padding: 0 0 20px 0"
          v-if="status === '1' && powers.some((item) => item == '024')"
        >
          <el-button type="primary" @click="submitForm" :size="'mini'"
            >保 存
          </el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
   <script>
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/stockApply";
import rules from "./rules";
export default {
  name: "stockApply",
  props: ["id", "sitem", "newTime"],

  mixins: [resToken],
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockApplyDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        },
      },
      options: [
        {
          value: "1",
          label: "自建仓",
        },
        {
          value: "3",
          label: "供应商仓",
        },
      ],
      companyCode: "",
      goods_class: "",
      lasttime: "",
      supplierName: "",
      goods_class: "",
      status: "",
      loading: false,
      ruleForm: {
        id: "",
        wsm_type: "3",
        good_price: "",
        nake_fee: "",
        wsm_code: "",
        supplier_code: "",
        supplierNo: [],
        num: "",
        expecttime: "",
        weight: "",
        now_god_price: "",
        teach_fee: "",
        pakge_fee: "",
        mark_fee: "",
        cert_fee: "",
        delivery_fee: "",
        open_fee: "",
        diff_weight: "",
        diff_price: "",
        remark: "",
      },
      rules: rules,
    };
  },
  mounted() {
    this.initForm();
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.status = this.sitem.status;
      // this.ruleForm.wsm_code = this.sitem.wsm_code;
      this.supplierName = "";
      await this.resetForm();
      if (this.status !== "1") {
        await this.initData();
      }

      this.loading = false;
    },

    //初始化表单
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.goods_class = "";
          const { can } = this.sitem;
          if (can && can.length > 0) {
            this.goods_class = can[0].id;
          }
          //  this.companyCode = this.sitem.wsm_supplierNo;
          if (this.status !== "1") {
            this.ruleForm.wsm_code = this.sitem.wsm_code;

            this.ruleForm = {
              wsm_code: "",
              supplier_code: "",
              supplierNo: [],
              num: "",
              expecttime: "",
              remark: "",
            };
          } else {
            this.ruleForm = {
              id: this.id,
              wsm_type: "3",
              good_price: "0",
              nake_fee: "0",
              wsm_code: this.sitem.wsm_code,
              supplier_code: "",
              supplierNo: [],
              num: this.sitem.good_num,
              expecttime: "",
              weight: "0",
              now_god_price: "0",
              teach_fee: "0",
              pakge_fee: "0",
              mark_fee: "0",
              cert_fee: "0",
              delivery_fee: "0",
              open_fee: "0",
              diff_weight: "0",
              diff_price: "0",
              remark: "",
              stock_code: [],
              companyCode: "",
              supplier_name: "",
              supplier_code: [],
            };
          }
        }
      });
    },
    // 保存更改
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let last = new Date(this.lasttime).valueOf();
          let ch = new Date(this.ruleForm.expecttime).valueOf();
          if (last < ch) {
            this.$message.warning("预计入库时间不能大于最晚入库时间");
            return;
          }
          this.loading = true;
          const item = JSON.parse(JSON.stringify(this.ruleForm));
          item.supplierNo = item.supplierNo.toString();
          item.wsm_code = item.stock_code.toString();
          delete item["companyCode"];
          delete item["supplier_name"];
          delete item["supplier_code"];
          delete item["stock_code"];
          const res = await asyncRequest.addFee(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;
        }
      });
    },
    // 获取详情
    async initData() {
      this.id = this.sitem.id;
      const res = await asyncRequest.feeDetail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        let { expecttime, num, remark, supplier, supplier_name, lasttime } =
          res.data;
        this.supplierName = supplier_name;
        this.lasttime = lasttime;
        this.ruleForm = {
          supplierNo: supplier.split(","),
          expecttime,
          num,
          remark,
        };
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.sitem = {};
        this.status = 0;
      }
      this.loading = false;
    },
    // 选择供应商
    selectSupplier(e) {
      if (e && e.id) {
        this.ruleForm.supplierNo = [e.code];
      } else {
        this.ruleForm.supplierNo = [];
      }
      this.$refs.ruleForm.validateField("supplierNo");
    },

    // 选择仓库
    selectStock(e) {
      if (e && e.id) {
        this.ruleForm.stock_code = [e.code];
        this.stockCode = e.code;
      } else {
        this.ruleForm.stock_code = [];
        this.stockCode = "";
      }
      this.$refs.ruleForm.validateField("stock_code");
    },
    // 选择供应商
    selectSupplierStock(e) {
      if (e && e.id) {
        this.ruleForm.supplier_code = [e.code];
        this.companyCode = e.code;
      } else {
        this.ruleForm.supplier_code = [];
        this.companyCode = "";
        this.ruleForm.stock_code = [];
        this.stockCode = "";
      }
      this.$refs.ruleForm.validateField("supplier_code");
    },
  },
};
</script>
   