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

export default {
  data() {
    return {
      drawerShow: false,
      setting: {},
      form: {
        x: 0,
        rate: 0,
        sx: 0,
        zIndex: 0,
        sh: 0,
        sw: 0,
        sy: 0,
        y: 0,
        w: 0,
        h: 0,
        r: 0
      }
    };
  },
  computed: {
    ...mapState(["isPreView"])
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {}
    },
    newDesktopDevice: {
      type: [String, Number],
      default: "1"
    }
  },
  watch: {
    visible: {
      handler() {
        this.drawerShow = this.visible;
      },
      immediate: true
    },
    item: {
      handler(val) {
        // console.log("item11", this.item);

        if (val && this.newDesktopDevice == 3) {
          return;
        }
        if (this.item) {
          this.getCurrentItem(this.item);
        } else {
          this.form = {
            x: 0,
            rate: 0,
            sx: 0,
            zIndex: 0,
            sh: 0,
            sw: 0,
            sy: 0,
            y: 0,
            w: 0,
            h: 0,
            r: 0
          };
        }
        // console.log("item", this.item);
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    pxToUnit,
    unitToPx,
    getCurrentItem(item) {
      // let setting = this.$dataStore.get("setting");
      console.log(84, item);

      this.item.y = item.y - item.h / 2;
      this.item.x = item.x - item.w / 2;
      this.$dataStore.set(
        "position_before_px",
        JSON.parse(JSON.stringify(this.item))
      );
      console.log("this.item", this.item);

      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))
      );
    },
    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.item != null && !this.isPreView)) {
        return;
      }
      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) {
      // console.log(this.item);
      // console.log(this.isPreView);

      if (!(this.item != null && !this.isPreView)) {
        return;
      }
      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;
      }
      if (f && f === "zIndex") {
        if (this.form[f] < 0) this.$set(this.form, f, 0);
      }
      this.formChange();
    }
  }
};
</script>

<template>
  <div class="drawer">
    <div class="img-form" v-show="newDesktopDevice != 3">
      <div class="title">
        图片编辑（单位：{{ $dataStore.get("setting").unit }}）
      </div>
      <el-form
        size="small"
        :disabled="!(item && !isPreView)"
        style=""
        :model="form"
        ref="formRef"
        label-position="left"
        label-width="30px"
      >
        <el-row :gutter="10">
          <el-col :span="8">
            <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="8">
            <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="8">
            <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="8">
            <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="8">
            <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="8">
            <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"
            style="display: flex;justify-content: space-between;padding:0 30px;"
          >
            <el-tooltip content="铺满">
              <el-button @click="$emit('ev', 'cover')" class="btn">
                <img
                  style="width: 13px;height: 13px"
                  src="@/static/icon/pm.png"
                  alt=""
                />
              </el-button>
            </el-tooltip>
            <el-tooltip content="居中">
              <el-button @click="$emit('ev', 'center')" class="btn">
                <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')" class="btn">
                <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')" class="btn">
                <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')" class="btn">
                <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')" class="btn">
                <img
                  style="width: 13px;height: 13px"
                  src="@/static/icon/xiayi.png"
                  alt=""
                />
              </el-button>
            </el-tooltip>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <slot name="content"></slot>
  </div>
</template>

<style lang="less" scoped>
.img-form {
  padding: 5px 10px;
  border: 1px solid #ececec;
  border-bottom: none;
}

::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: 500px;
  // 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;
  position: relative;
  z-index: 1;
  .title {
    padding-bottom: 10px;
    font-weight: 700;
    font-size: 20px;
    // margin-bottom: 8px;
    // border-bottom: 1px solid #ececec;
  }
}
.btn {
  height: 40px;
  width: 60px;
  border-color: #c0c4cc;
}
::v-deep.el-scrollbar {
  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);
  .el-scrollbar__wrap {
    overflow-x: hidden;
  }
}
::v-deep.el-form-item {
  margin-bottom: 10px;
}

@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>
