<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="100px">
      <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="高清价格图">
        <el-input-number
          v-model="form.hdLogoPhotoPrice"
          :precision="2"
          :step="0.1"
          :min="0"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="原图价格图">
        <el-input-number
          v-model="form.originPhotoPrice"
          :precision="2"
          :step="0.1"
          :min="0"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="是否随手拍">
        <el-radio-group v-model="form.isTake" 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.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,
        isTake: false,
        originPhotoPrice: 0,
        hdLogoPhotoPrice: 0
      },
      rules: {
        originPhotoPrice: [
          { 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('photot_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
