<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogShow"
      width="800px"
      :before-close="close"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="banner名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 300px"
            size="small"
            placeholder="请输入banner名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-select
            v-model="form.location"
            placeholder="请选择位置"
            clearable
            style="width: 300px"
          >
            <el-option
              :label="item.dictLabel"
              :value="item.dictValue"
              v-for="(item, index) in bannerLocationList"
              :key="index"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model.number="form.sort"
            style="width: 300px"
            size="small"
            placeholder="请输入排序"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="picUrl">
          <image-upload
            v-model="bannerPicUrl"
            :limit="1"
          />
          <div class="tip">图片尺寸 700 * 300</div>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type" size="small" @change="changeType">
            <el-radio label="link">自定义链接</el-radio>
            <el-radio label="tag">商品标签</el-radio>
            <el-radio label="show">仅展示</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="链接地址" prop="linkUrl" v-if="form.type == 'link'">
          <el-input
            v-model="form.linkUrl"
            style="width: 300px"
            size="small"
            placeholder="请输入链接地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品标签" prop="goodsTags" v-if="form.type == 'tag'">
          <el-select
            v-model="form.goodsTags"
            placeholder="请选择商品标签"
            multiple
            clearable
            style="width: 300px"
          >
            <el-option
              :label="item.name"
              :value="item.tagId"
              v-for="(item, index) in goodsTagItemsList"
              :key="index"
            />
          </el-select>
        </el-form-item>
      </el-form>

      <!-- 按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" :disabled="loading" @click="saveClick"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import CustomFieldsMixin from "@/mixins/CustomFields";
import { goodsBannerCreate, goodsBannerDetail, goodsBannerUpdate } from "@/api/business/banner";
import { goodsTagItems } from "@/api/business/tag";
export default {
  mixins: [CustomFieldsMixin],
  props: {
    // 弹框显示
    dialogShow: {
      type: Boolean,
      default: false,
    },
    // ID
    bannerId: {
      type:[Number, String],
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      form: {
        type: 'link',
        sort: 0
      },
      bannerLocationList: [],
      goodsTagItemsList: [],
      rules: {
        name: [
          { required: true, message: "请输入banner名称", trigger: "blur" },
        ],
        location: [
          { required: true, message: "请选择位置", trigger: "change" },
        ],
        sort: [
          { required: true, message: "请输入排序", trigger: "blur" },
          {
            pattern: /^([0-9]\d*)$/,
            message: "请输入合法的数字",
            trigger: ["blur", "change"],
          },
        ],
        picUrl: [
          {
            required: true,
            message: "请上传banner图片",
            trigger: "change",
          },
        ],
        type:  [
          { required: true, message: "请选择类型", trigger: "change" },
        ],
        linkUrl: [
          { required: true, message: "请输入链接地址", trigger: "blur" },
        ],
        goodsTags: [
          { required: true, message: "请选择商品标签", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    title() {
      return this.bannerId ? '编辑banner' : '添加banner';
    },
    bannerPicUrl: {
      get() {
        return this.form.picUrl ? this.form.picUrl.split(',').map(item => {
          return {
            fileName: item
          }
        }) : []
      },
      set(val) {
        this.$set(this.form, 'picUrl', val.map(item => { return item.fileName }).toString())
      }
    }
  },
  created() {
    this.getBannerLocation();
    this.getGoodsTagItems();
    if(this.bannerId){
      goodsBannerDetail({ bannerId: this.bannerId }).then(res => {
        const { location, name, sort, picUrl, type, linkUrl, goodsTags } = res.data
        this.form = {
          location: JSON.parse(location).value,
          goodsTags: goodsTags ? goodsTags.split(',').map(item => Number(item)) : [],
          name, sort, picUrl, type, linkUrl
        }
      })
    }
  },
  methods: {
    getBannerLocation() {
      this.getDicts("banner_location").then((res) => {
        this.bannerLocationList = res && res.data;
      });
    },
    getGoodsTagItems() {
      goodsTagItems({}).then((res) => {
        this.goodsTagItemsList = res && res.data;
      });
    },

    changeType(){
      this.$refs["form"].clearValidate();
    },

    // 关闭弹框
    close() {
      this.$emit("close");
      this.loading = false;
      this.$refs["form"].resetFields();
    },

    // 确认
    saveClick() {
      this.loading = true;
      const subForm = this.$refs["form"];
      subForm.validate((valid) => {
        if (valid) {
          this.submitForm(this.form);
        } else {
          this.loading = false;
          // 提示第一个error
          this.getFormErrorMessage(subForm);
          return false;
        }
      });
    },

    // 提交
    submitForm(form) {
      let data = {
        ... form,
        goodsTags: form.goodsTags ? form.goodsTags.join(',') : ''
      };
      let request = this.bannerId ? goodsBannerUpdate : goodsBannerCreate
      if(this.bannerId){
        data.bannerId = this.bannerId
      }
      request(data)
        .then((res) => {
          if (res.code == 0) {
            this.msgSuccess(this.bannerId ? "修改成功" : '添加成功');
            this.close();
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
<style scoped>
.channel {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tip {
    font-size: 12px;
    color: #999;
  }
</style>
