<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="500px"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="160px">
      <el-form-item label="列表预览图图片">
        <img
          :src="form.listPreview"
          class="pre-img"
          @click="
            () =>
              this.$AdvanceViewImageModal({
                items: [{ src: form.listPreview }]
              })
          "
        />
      </el-form-item>
      <el-form-item label="详情页面预览图图片">
        <img
          :src="form.detailPreview"
          class="pre-img"
          @click="
            () =>
              this.$AdvanceViewImageModal({
                items: [{ src: form.detailPreview }]
              })
          "
        />
      </el-form-item>
      <el-form-item label="高清图">
        <img
          :src="form.hdLogoPhoto"
          class="pre-img"
          @click="
            () =>
              this.$AdvanceViewImageModal({
                items: [{ src: form.hdLogoPhoto }]
              })
          "
        />
      </el-form-item>
      <el-form-item label="原图">
        <img
        :src="form.originPhoto"
        class="pre-img"
        @click="
            () =>
              this.$AdvanceViewImageModal({
                items: [{ src: form.originPhoto }]
              })
          "
        />
      </el-form-item>

      <el-form-item label="是否精选">
        <el-radio-group v-model="form.isBeautiful" prop="isShow">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="状态">
        <el-radio-group v-model="form.isShow" prop="isShow">
          <el-radio :label="true">上架</el-radio>
          <el-radio :label="false">下架</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getItem, updateItem } from '@/api/photoWarehouse';

export default {
  name: 'ImageGoodsItemModal',

  props: {
    id: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      modal: true,

      form: {
        isShow: false,
        isBeautiful: false,
        hdLogoPhotoOriginPrice: 0,
        hdLogoPhotoPrice: 0,
        originPhotoOriginPrice: 0
        originPhotoPrice: 0
      },
      rules: {
        originPhotoOriginPrice: [
          { required: true, message: '请输入正确的价格', trigger: 'blur' }
        ],
        originPhotoPrice: [
          { required: true, message: '请输入正确的价格', trigger: 'blur' }
        ],
        hdLogoPhotoOriginPrice: [
          { required: true, message: '请输入正确的价格', trigger: 'blur' }
        ],
        hdLogoPhotoPrice: [
          { required: true, message: '请输入正确的价格', trigger: 'blur' }
        ]
      },

      typeOptions: []
    };
  },

  computed: {
    title() {
      if (this.id) {
        return '编辑照片';
      } else {
        return '新增照片';
      }
    }
  },

  watch: {
    id: {
      handler(id) {
        id && this.loadData();
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    async loadData() {
      const { success, data, msg } = await getItem({
        id: this.id
      });
      if (success) {
        let result = data;
        result.imgUrl = [
          {
            url: data.imgUrl
          }
        ];
        this.form = result;
      }
    },

    handleConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          let params = Object.assign({}, this.form);
          if (this.id) params.id = this.id;
          const { success, msg } = await updateItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('photo_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.pre-img {
  height: 80px;
}
</style>
