<template>
  <el-dialog :close-on-click-modal="false" :close-on-press-escape="true" :title="title" :type="type"
             :visible.sync="isVisible" :width="width" top="50px" v-el-drag-dialog>
    <el-form :model="creditGoodsImg" :rules="rules" label-position="right" label-width="100px" ref="form">
      <el-form-item :label="$t('table.creditGoodsImg.goodsId')" prop="goodsId">
        <el-input type="" v-model="creditGoodsImg.goodsId" placeholder="商品ID"/>
      </el-form-item>
      <el-form-item :label="$t('table.creditGoodsImg.imgPath')" prop="imgPath">
        <el-input type="" v-model="creditGoodsImg.imgPath" placeholder="图片路径"/>
      </el-form-item>
      <el-form-item :label="$t('table.creditGoodsImg.sortOrder')" prop="sortOrder">
        <el-input type="" v-model="creditGoodsImg.sortOrder" placeholder="显示顺序"/>
      </el-form-item>
      <el-form-item :label="$t('table.creditGoodsImg.typed')" prop="typed">
        <el-input type="" v-model="creditGoodsImg.typed" placeholder="类型：1-主图，2-副图"/>
      </el-form-item>
      <el-form-item :label="$t('table.creditGoodsImg.enabled')" prop="enabled">
        <el-radio-group v-model="creditGoodsImg.enabled" size="medium">
          <el-radio-button :label="true">{{ $t("common.yes") }}</el-radio-button>
          <el-radio-button :label="false">{{ $t("common.no") }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('table.creditGoodsImg.isDelete')" prop="isDelete">
        <el-input type="" v-model="creditGoodsImg.isDelete" placeholder="逻辑删除：1-删除 0-未删除"/>
      </el-form-item>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button @click="isVisible = false" plain type="warning">
        {{ $t("common.cancel") }}
      </el-button>
      <el-button @click="submitForm" :disabled="confirmDisabled" plain type="primary">
        {{ $t("common.confirm") }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import elDragDialog from '@/directive/el-drag-dialog'
import creditGoodsImgApi from "@/api/creditGoods/CreditGoodsImg.js";

export default {
  name: "CreditGoodsImgEdit",
  directives: { elDragDialog },
  components: {  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: "add"
    }
  },
  data() {
    return {
      confirmDisabled: false,
      creditGoodsImg: this.initCreditGoodsImg(),
      screenWidth: 0,
      width: this.initWidth(),
      rules: {

      },
      // 枚举
      enums: {
      },
      // 字典
      dicts: {
      }
    };
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible;
      },
      set() {
        this.close();
        this.reset();
      }
    },
    title() {
      return this.$t("common." + this.type);
    }
  },
  watch: {},
  mounted() {
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth();
      })();
    };
  },
  methods: {
    initCreditGoodsImg() {
      return {
        id: "",
        goodsId: null,
        imgPath: '',
        sortOrder: null,
        typed: null,
        enabled: true,
        isDelete: null,
      };
    },
    initWidth() {
      this.screenWidth = document.body.clientWidth;
      if (this.screenWidth < 991) {
        return "90%";
      } else if (this.screenWidth < 1400) {
        return "45%";
      } else {
        return "800px";
      }
    },
    setCreditGoodsImg(val = {}) {
      const vm = this;

      vm.dicts = val['dicts'];
      vm.enums = val['enums'];
      if (val['row']) {
        vm.creditGoodsImg = { ...val['row'] };

      }
    },
    close() {
      this.$emit("close");
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.$refs.form.clearValidate();
      this.$refs.form.resetFields();
      this.confirmDisabled = false;
      this.creditGoodsImg = this.initCreditGoodsImg();
    },
    submitForm() {
      const vm = this;
      this.$refs.form.validate(valid => {
        if (valid) {
          vm.editSubmit();
        } else {
          return false;
        }
      });
    },
    editSubmit() {
      const vm = this;
      if (vm.type === "edit") {
          vm.update();
      } else {
          vm.save();
      }
    },
    save() {
      const vm = this;
      vm.confirmDisabled = true;
      creditGoodsImgApi.save(this.creditGoodsImg).then(response => {
        const res = response.data;
        if (res.isSuccess) {
          vm.isVisible = false;
          vm.$message({
            message: vm.$t("tips.createSuccess"),
            type: "success"
          });
          vm.$emit("success");
        }
      }).finally(()=> vm.confirmDisabled = false);
    },
    update() {
      const vm = this;
      vm.confirmDisabled = true;
      creditGoodsImgApi.update(this.creditGoodsImg).then(response => {
        const res = response.data;
        if (res.isSuccess) {
          vm.isVisible = false;
          vm.$message({
            message: this.$t("tips.updateSuccess"),
            type: "success"
          });
          vm.$emit("success");
        }
      }).finally(()=> vm.confirmDisabled = false);
    }
  }
};
</script>
<style lang="scss" scoped></style>
