<template>
  <div>

    <div class="tab-header webtop">
      <!-- 标题 -->
      <div>包装物信息</div>
      <!-- start 添加修改按钮 -->
      <div>
        <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveOrUpdate">保存</el-button>
        <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
      </div>
      <!-- end 添加修改按钮 -->
      <!-- end 详情按钮 -->
    </div>

    <div class="listconadd">

      <el-card class="box-card">
        <div class="item">
          <span class="item_text">包装物名称：</span>
          <el-input v-model="formobj.name" placeholder="" class="item_input" clearable />
        </div>
        <div class="item">
          <span class="item_text">包装物编码：</span>
          <el-input v-model="formobj.number" placeholder="" class="item_input" clearable />
        </div>
        <div class="item">
          <span class="item_text">包装物类型：</span>
          <el-select v-model="formobj.typeName" filterable placeholder="请选择包装物类型" class="item_input" @change="getType">
            <el-option v-for="item in typeList" :key="item.sid" :label="item.name" :value="item.sid" />
          </el-select>
          <!-- <el-input v-model="formobj.number" placeholder="" class="item_input" clearable /> -->
        </div>
        <div class="item">
          <span class="item_text">包装物单位：</span>
          <el-input v-model="formobj.unit" placeholder="" class="item_input" clearable />
        </div>
        <div class="item">
          <span class="item_text">包装物规格：</span>
          <el-input v-model="formobj.proSpec" placeholder="" class="item_input" clearable />
        </div>
        <div class="item">
          <span class="item_text">包装物初始库存：</span>
          <el-input v-model="formobj.initialInventory" @input="limitInput1($event)" placeholder="" class="item_input" clearable />
        </div>
      </el-card>

    </div>

  </div>
</template>

<script>
  import req from '@/api/packagInfo/packagInfo'
  export default {
    data() {
      return {
        submitdisabled: false,
        typeLists: [],
        typeList: [],
        formobj: {
          sid: "",
          name: "",
          number: "",
          inventory: "",
          initialInventory: "",
          typeSid: "",
          typeName: "",
          unit: "",
          proSpec: "",

        }
      }
    },
    created() {
      this.packageType()
    },
    methods: {
      /**
       * 1、必须为数字
       * 2、只能有一个小数点
       * 3、小数点后保留两位小数
       * 4、当第一位输入小数点的时候自动补全，补为 0.
       * 5、除非是小数，否则数字不能以0开头
       * @param {string} value - 输入的值
       * @param {string} index - 匹配的对象属性 [mkPrice | slPrice]
       */
      limitInput1(value, index) {
        this.formobj.initialInventory =
          ("" + value) // 第一步：转成字符串
          .replace(/[^\d^\.]+/g, "") // 第二步：把不是数字，不是小数点的过滤掉
          .replace(/^0+(\d)/, "$1") // 第三步：第一位0开头，0后面为数字，则过滤掉，取后面的数字
          .replace(/^\./, "0.") // 第四步：如果输入的第一位为小数点，则替换成 0. 实现自动补全
          .match(/^\d*(\.?\d{0,2})/g)[0] || ""; // 第五步：最终匹配得到结果 以数字开头，只有一个小数点，而且小数点后面只能有0到2位小数
      },
      packageType() {
        req.packageType().then((res) => {
          if (res.success) {
            console.log(">>>>>>>>>2222222", res.data)
            this.typeLists = res.data
            for (var i = 0; i < this.typeLists.length; i++) {

              let item = {
                name: this.typeLists[i].name,
                sid: this.typeLists[i].sid,

              }
              this.typeList.push(item)
            }

          }
        })
      },
      saveOrUpdate() {
        req.addInfo(this.formobj)
          .then(resp => {
            if (resp.success) {
              this.$message({
                showClose: true,
                type: 'success',
                message: resp.msg
              })
              this.handleReturn("true")
            } else {
              // 根据resp.code进行异常情况处理
            }
          })
          .catch(() => {})

      },
      handleReturn(isreload) {
        if (isreload === 'true') this.$emit('reloadlist')
        this.formobj = {
          sid: "",
          name: "",
          number: "",
          inventory: "",
          initialInventory: "",
          typeSid: "",
          typeName: "",
          unit: "",
          proSpec: "",

        }
        this.$emit('doback')
      },
      getType(value) {
        console.log(">>>>>>>>>getType", value)
        const choose = this.typeLists.filter((item) => item.sid === value)
        console.log(">>>>>>>>>getType", choose[0])
        this.formobj.typeSid = choose[0].sid
        this.formobj.typeName = choose[0].name
      },
    }
  }
</script>
<style lang="scss">
  .box-card {
    margin-left: 60px;
    margin-right: 60px;
    min-width: 70%;
    margin-top: 20px;

    .item {
      display: flex;
      flex-direction: row;
      align-items: center;
      margin-top: 15px;
      height: 40px;
      line-height: 40px;

      .item_text {
        flex: 0.8;
        font-size: 18px;
        text-align: right;
      }

      .item_input {
        flex: 4;
        font-size: 16px;
        margin-left: 10px;
        margin-right: 80px;
      }

      .item_left_input {
        width: 20%;
      }

      .item_left_text {
        height: 30px;
        margin-left: 20px;
        line-height: 30px;
        color: #018AD2;
        padding: 0px 15px;
        border: 1.5px solid #018AD2;
        border-radius: 5px;

      }

      .item_right {
        flex: 1;
        justify-items: center;

        .item_right_list_text {
          font-size: 16px;
        }

        .item_right_list_delect {
          color: #5E94FF;
          margin-left: 20px;
          font-size: 16px;
          text-decoration: underline;
        }
      }

    }

  }
</style>
