<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="图片" prop="imgUrl">
        <upload v-model="form.imgUrl" params="prefix=/banner" />
      </el-form-item>
      <el-form-item label="位置" prop="type">
        <DataSelect
          v-model="form.type"
          :params="$dict.BANNER_TYPE"
        ></DataSelect>
      </el-form-item>
      <el-form-item label="标题" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="链接" prop="link">
        <el-input v-model="form.link"></el-input>
      </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-item label="排序" prop="sort">
        <el-input-number
          v-model="form.sort"
          :precision="0"
          :min="1"
          :max="9999"
          label="序号"
        ></el-input-number>
      </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, saveItem } from '@/api/base/banner';

export default {
  name: 'BannerItemModal',

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

  data() {
    return {
      modal: true,

      form: {
        imgUrl: [],
        type: null,
        name: '',
        link: '',
        isShow: false,
        sort: 1
      },
      rules: {
        type: [
          { required: true, message: '请选择所属位置', trigger: 'change' }
        ],
        name: [{ required: true, message: '请输入标题', trigger: 'change' }],
        imgUrl: [
          {
            type: 'array',
            required: true,
            message: '请上传图片',
            trigger: 'change'
          }
        ]
        // link: [{ required: true, message: '请输入链接地址', trigger: 'change' }]
      },

      typeOptions: []
    };
  },

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

  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);
          params.imgUrl = this.form.imgUrl[0].url;
          if (this.id) params.id = this.id;
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('banner_reload');
          }
        }
      });
    }
  }
};
</script>

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