<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">
        <!-- TODO 043 -->
        <upload v-model="form.imgUrl" params="prefix=/banner"/>
        <el-row>建议尺寸</el-row>
        <el-row>首页：700 * 300</el-row>
        <el-row>我的 - 开通会员专享权益：700 * 300</el-row>
        <el-row>我的 - 积分签到：750 * 500</el-row>
        <el-row>邀请拉新 - 主页：750 * 500</el-row>
        <el-row>邀请拉新 - 注册页：750 * 500</el-row>
        <el-row>会员中心 - 广告：700 * 300</el-row>
        <el-row>会员中心 - 会员权益：700 * 300</el-row>
        <el-row>会员中心 - 会员福利：700 * 300</el-row>
        <el-row>启动应用广告：1284 * 778</el-row>
        <el-row>登录页背景图/视频：540 * 960</el-row>
        <el-row>邀请注册海报：500 * 500</el-row>
        <el-row>问题反馈联系客服二维码：500 * 500</el-row>
      </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
          controls-position="right"
          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' },
        //   { validator: checkUrl, trigger: 'blur' }
        // ]
      },

      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) {
          if (this.checkUrl(this.form.link)) {
            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');
            }
          }
        }
      });
    },

    checkUrl(value) {
      /* 如果没有填，则不校验数据有效性 */
      if (!value) {
        return true;
      }

      const pattern = /http[s]{0,1}:\/\/([\w.]+\/?)\S*/;
      if (value && pattern.test(value)) {
        return true;
      }

      this.$error('请以http://或者https://');
      return false;
    }
  }
};
</script>

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