<script>
import { pxToUnit, unitToPx } from "@/utils";

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) {
          let setting = this.$dataStore.get("setting");
          this.item.y = this.item.y - this.item.h / 2;
          this.item.x = this.item.x - this.item.w / 2;
          this.$dataStore.set(
            "position_before_px",
            JSON.parse(JSON.stringify(this.item))
          );
          console.log("setting", setting);

          let x_mm, w_mm, h_mm, y_mm;
          x_mm = this.pxToUnit(this.item.x);
          w_mm = this.pxToUnit(this.item.w);
          h_mm = this.pxToUnit(this.item.h);
          y_mm = this.pxToUnit(this.item.y);

          // 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.r = this.item.r;
          this.$dataStore.set(
            `position_after_px`,
            JSON.parse(JSON.stringify(this.form))
          );
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    pxToUnit,
    unitToPx,
    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;
      }
      this.form.h = this.form.h? Math.abs(this.form.h):this.form.h
      this.form.w = this.form.w? Math.abs(this.form.w):this.form.w
      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;
      f.x = this.unitToPx(f.x);
      f.y = this.unitToPx(f.y);
      f.w = this.unitToPx(f.w);
      f.h = this.unitToPx(f.h);
      f.zIndex = Number(f.zIndex);
      for (let k in f) {
        f[k] = Number(f[k]);
      }

      this.$emit("change", f);
      this.$dataStore.set("position_unit", JSON.parse(JSON.stringify(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` }">
    <div class="title">
      图片编辑
    </div>
    <div class="img-form">
      <el-form
        size="small"
        :disabled="!item"
        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>
    <slot></slot>
  </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, 0.2),
    0 16px 24px 2px rgba(0, 0, 0, 0.14), 0 6px 30px 5px rgba(0, 0, 0, 0.12);
  left: 0;
  position: fixed;
  border-right: 1px solid #ececec;
  height: calc(100vh - 50px);
  display: flex;
  flex-direction: column;
  //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>
