<script>
export default {
  data() {
    return {
      drawerShow: false,
      form: {
        x: 0,
        rate: 0,
        sx: 0,
        zIndex: 0,
        sh: 0,
        sw: 0,
        sy: 0,
        y: 0,
        w: 0,
        h: 0,
        r: 0
      }
    };
  },

  props: {
    visible: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {
      }
    }

  },
  watch: {
    visible: {
      handler() {
        this.drawerShow = this.visible;
      },
      immediate: true
    },
    item: {
      handler() {
        if (this.item) {
          this.item.y = (this.item.y - this.item.h / 2);
          this.item.x = (this.item.x - this.item.w / 2);
          console.log(this.item.x, this.item.y, this.item.h);
          let setting = this.$dataStore.get("setting");
          let x_mm, w_mm, h_mm, y_mm;
          if (setting.unit === "mm") {
            x_mm = Number((this.item.x * 0.84183).toFixed(2));
            w_mm = Number((this.item.w * 0.84183).toFixed(2));
            h_mm = Number((this.item.h * 0.84183).toFixed(2));
            y_mm = Number((this.item.y * 0.84183).toFixed(2));
          } else if (setting.unit === "inch") {
            x_mm = (Number((this.item.x * 0.84183) / 25.4).toFixed(2));
            w_mm = (Number((this.item.w * 0.84183) / 25.4).toFixed(2));
            h_mm = (Number((this.item.h * 0.84183) / 25.4).toFixed(2));
            y_mm = (Number((this.item.y * 0.84183) / 25.4).toFixed(2));

          }

          // return
          this.form.x = x_mm;
          this.form.rate = this.item.rate;
          this.form.y = y_mm;
          this.form.w = w_mm;
          this.form.h = h_mm;
          this.form.zIndex = this.item.zIndex;
          // this.form.sx = x_mm
          // this.form.sy = y_mm
          // this.form.sh = h_mm
          // this.form.sw = w_mm
          this.form.r = this.item.r;
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    formChange(t) {

      if (t && t === "w") {
        this.form.h = this.form.w * this.form.rate;
      }
      if (t && t === "h") {
        this.form.w = this.form.h * this.form.rate;
      }
      let f = JSON.parse(JSON.stringify(this.form));
      f.y = (Number(f.y) + Number(f.h) / 2);
      f.x = (Number(f.x) + Number(f.w) / 2);

      let setting = this.$dataStore.get("setting");
      if (setting.unit === "mm") {
        f.x = Number((f.x / 0.84183).toFixed(2));
        f.y = Number((f.y / 0.84183).toFixed(2));
        f.w = Number((f.w / 0.84183).toFixed(2));
        f.h = Number((f.h / 0.84183).toFixed(2));
      } else if (setting.unit === "inch") {
        f.x = (Number((f.x / 0.84183) *  25.4).toFixed(2));
        f.y = (Number((f.y / 0.84183) *  25.4).toFixed(2));
        f.w = (Number((f.w / 0.84183) *  25.4).toFixed(2));
        f.h = (Number((f.h / 0.84183) *  25.4).toFixed(2));
      }
      f.zIndex = Number((f.zIndex));
      for(let k in f){
        f[k] = Number(f[k])
      }
      this.$emit("change", f);
    },
    addValue(f) {

      if (!this.form[f]) this.$set(this.form, f, 0);
      let nw = Number(this.form[f]) + 1;
      this.$set(this.form, f, nw);
      if (f && f === "w") {
        this.form.h = this.form.w * this.form.rate;
      }
      if (f && f === "h") {
        this.form.w = this.form.h * this.form.rate;
      }
      this.formChange();
    },
    reduceValue(f) {
      if (!this.form[f]) this.$set(this.form, f, 0);
      let nw = Number(this.form[f]) - 1;
      this.$set(this.form, f, nw);
      if (f && f === "w") {
        this.form.h = this.form.w * this.form.rate;
      }
      if (f && f === "h") {
        this.form.w = this.form.h * this.form.rate;
      }
      this.formChange();
    }
  }
};
</script>

<template>
  <div class="drawer" :style="{animation:`ltr-drawer-in .3s 1ms`}" v-show="visible">
    <div class="title">
      图片编辑
    </div>
    <div class="img-form">
      <el-form size="small" style="" :model="form" label-position="left" label-width="30px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="x：" prop="x">
              <div class="form-content">
                <el-input @blur="formChange" v-model="form.x"></el-input>
                <div class="set-value">
                  <i @click="addValue('x')" class="el-icon-arrow-up"></i>
                  <i @click="reduceValue('x')" class="el-icon-arrow-down"></i>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="y：">
              <div class="form-content">
                <el-input @blur="formChange" v-model="form.y"></el-input>
                <div class="set-value">
                  <i @click="addValue('y')" class="el-icon-arrow-up"></i>
                  <i @click="reduceValue('y')" class="el-icon-arrow-down"></i>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="w：">
              <div class="form-content">
                <el-input @blur="formChange('w')" v-model="form.w"></el-input>
                <div class="set-value">
                  <i @click="addValue('w')" class="el-icon-arrow-up"></i>
                  <i @click="reduceValue('w')" class="el-icon-arrow-down"></i>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="h：">
              <div class="form-content">
                <el-input @blur="formChange('h')" v-model="form.h"></el-input>
                <div class="set-value">
                  <i @click="addValue('h')" class="el-icon-arrow-up"></i>
                  <i @click="reduceValue('h')" class="el-icon-arrow-down"></i>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="r：">
              <div class="form-content">
                <el-input maxlength="3" @blur="formChange" v-model="form.r">

                </el-input>
                <div class="set-value">
                  <i @click="addValue('r')" class="el-icon-arrow-up"></i>
                  <i @click="reduceValue('r')" class="el-icon-arrow-down"></i>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="z：">
              <div class="form-content">
                <el-input @blur="formChange" v-model="form.zIndex">

                </el-input>
                <div class="set-value">
                  <i @click="addValue('zIndex')" class="el-icon-arrow-up"></i>
                  <i @click="reduceValue('zIndex')" class="el-icon-arrow-down"></i>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-tooltip content="居中">
              <el-button @click="$emit('ev','center')" size="mini">
                <img style="width: 13px;height: 13px" src="@/static/icon/juzhong-01.png" alt="">
              </el-button>
            </el-tooltip>
            <el-tooltip content="垂直居中">
              <el-button @click="$emit('ev','y_center')" size="mini">
                <img style="width: 13px;height: 13px" src="@/static/icon/vertical.png" alt="">
              </el-button>
            </el-tooltip>
            <el-tooltip content="水平居中">
              <el-button @click="$emit('ev','x_center')" size="mini">
                <img style="width: 13px;height: 13px" src="@/static/icon/shuipingjuzhong.png" alt="">
              </el-button>
            </el-tooltip>
            <el-tooltip content="上移一层">
              <el-button @click="$emit('ev','add_index')" size="mini">
                <img style="width: 13px;height: 13px" src="@/static/icon/shangyi.png" alt="">
              </el-button>
            </el-tooltip>
            <el-tooltip content="下移一层">
              <el-button @click="$emit('ev','reduce_index')" size="mini">
                <img style="width: 13px;height: 13px" src="@/static/icon/xiayi.png" alt="">
              </el-button>
            </el-tooltip>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>

</template>

<style lang="less" scoped>
.img-form {
  padding: 10px;
}
::v-deep {
  .el-drawer__header {
    border-bottom: 1px solid #ececec;
    padding-bottom: 10px;
    margin-bottom: 20px;
  }
}
.form-content {
  display: flex;
  align-items: center;
}
.set-value {
  height: 100%;
  margin-left: 6px;
  i {
    width: 20px;
    height: 12px;
    display: block;
    text-align: center;
    line-height: 12px;
    cursor: pointer;
    border: 1px solid transparent;
    &:hover {
      border-color: #ececec;
    }
  }
}
.drawer {
  width: 300px;
  bottom: 0;
  top: 50px;
  padding: 10px;
  z-index: 99;
  background: white;
  box-shadow: 0 8px 10px -5px rgba(0, 0, 0, .2), 0 16px 24px 2px rgba(0, 0, 0, .14), 0 6px 30px 5px rgba(0, 0, 0, .12);
  left: 0;
  position: fixed;
  border-right: 1px solid #ececec;
  height: calc(100vh - 50px);
  //animation: ltr-drawer-in .3s 1ms;
  .title {
    padding-bottom: 10px;
    border-bottom: 1px solid #ececec;
  }
}

@keyframes ltr-drawer-in {
  0% {
    transform: translate(-100%);
  }
  100% {
    transform: translate(0);
  }
}

@keyframes ltr-drawer-in {
  0% {
    transform: translate(-100%);
  }
  100% {
    transform: translate(0);
  }
}

@keyframes ltr-drawer-out {
  0% {
    transform: translate(0);
  }
  100% {
    transform: translate(-100%);
  }
}

</style>