<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="960px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    title="抽奖配置"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-form-item label="背景图片:" prop="bgImg">
        <ele-image-upload
          v-model="form.bgImg"
          :limit="1"
          :drag="true"
          :multiple="false"
          @upload="onUpload"
        />
        <div class="ele-text-secondary">建议尺寸1600X900</div>
      </el-form-item>
      <el-form-item label="屏幕显示:">
        <el-radio-group v-model="form.screenDisplay">
          <el-radio :label="1">横屏</el-radio>
          <el-radio :label="2">竖屏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="短信内容:">
        <el-input
          v-model="form.sms"
          :rows="2"
          type="textarea"
          placeholder="请输入中奖短信内容"
        />
        <div class="ele-text-secondary">
          当内容有 中奖标题 发短信时就会自动替换为抽奖类型的标题
        </div>
      </el-form-item>
      <el-form-item label="抽奖类型:">
        <el-table :data="form.winType" :border="true" style="width: 100%">
          <el-table-column label="标题" align="center">
            <template v-slot="{ row }">
              <el-input v-model="row.title" placeholder="标题" />
            </template>
          </el-table-column>
          <el-table-column label="标签" width="100px" align="center">
            <template v-slot="{ row }">
              <el-input v-model="row.tag" placeholder="标签" />
            </template>
          </el-table-column>
          <el-table-column label="图片" width="120px" align="center">
            <template v-slot="{ row }">
              <ele-image-upload
                v-model="row.img"
                :limit="1"
                :drag="true"
                :multiple="false"
                @upload="onUpload"
              />
            </template>
          </el-table-column>
          <el-table-column label="参与条件" align="center">
            <template v-slot="{ row }">
              <el-select
                v-model="row.itemIds"
                placeholder="请选择商品"
                clearable
                multiple
                class="ele-fluid"
              >
                <el-option
                  v-for="item in goodsList"
                  :key="item.itemId"
                  :value="item.itemId"
                  :label="item.title"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="参与抽奖" width="100px" align="center">
            <template v-slot="{ row }">
              <el-select v-model="row.show" class="ele-fluid">
                <el-option label="否" value="0" />
                <el-option label="是" value="1" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="是否核销" width="100px" align="center">
            <template v-slot="{ row }">
              <el-select v-model="row.ifCode" class="ele-fluid">
                <el-option label="否" value="0" />
                <el-option label="是" value="1" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="收货地址" width="100px" align="center">
            <template v-slot="{ row }">
              <el-select v-model="row.ifAddress" class="ele-fluid">
                <el-option label="不需要" value="0" />
                <el-option label="需要" value="1" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="70px"
            align="center"
            :resizable="false"
          >
            <template v-slot="{ row, $index }">
              <span class="ele-action">
                <el-popconfirm
                  title="确定要删除此抽奖类型吗?"
                  @confirm="removeBottoms(row, $index)"
                >
                  <template v-slot:reference>
                    <el-link
                      icon="el-icon-delete"
                      type="danger"
                      :underline="false"
                    >
                      删除
                    </el-link>
                  </template>
                </el-popconfirm>
              </span>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          icon="el-icon-plus"
          style="width: 100%; margin-top: 15px"
          @click="addBottoms"
        >
          新增抽奖类型
        </el-button>
      </el-form-item>
      <el-form-item label="中奖人数:">
        <el-table :data="form.winNum" :border="true" style="width: 100%">
          <el-table-column align="center" label="人数">
            <template v-slot="{ row }">
              <el-input-number
                :min="0"
                v-model="row.value"
                placeholder="请输入人数"
                controls-position="right"
                class="ele-fluid ele-text-left"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="100px"
            align="center"
            :resizable="false"
          >
            <template v-slot="{ row, $index }">
              <span class="ele-action">
                <el-popconfirm
                  title="确定要删除此中奖人数吗?"
                  @confirm="removeWinNums(row, $index)"
                >
                  <template v-slot:reference>
                    <el-link
                      icon="el-icon-delete"
                      type="danger"
                      :underline="false"
                    >
                      删除
                    </el-link>
                  </template>
                </el-popconfirm>
              </span>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          icon="el-icon-plus"
          style="width: 100%; margin-top: 15px"
          @click="addWinNums"
        >
          新增中奖人数
        </el-button>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import EleImageUpload from 'ele-admin/es/ele-image-upload';
  import request from '@/utils/request';
  import { updateActivityDraw } from '@/api/sylive/activity';
  import { listGoods } from '@/api/sylive/goods';
  export default {
    components: { EleImageUpload },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        activityId: null,
        sms: '',
        screenDisplay: 1,
        bgImg: [],
        winNum: [],
        winType: []
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        goodsList: [],
        imgIndex: 1,
        // 表单验证规则
        rules: {
          bgImg: [
            {
              required: true,
              message: '请上传背景图',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 查询商品 */
      goodsQuery() {
        listGoods({ activityId: this.data.activityId, type: 0 })
          .then((list) => {
            this.goodsList = list;
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };
          updateActivityDraw(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      },
      /* 添加中奖人数 */
      addWinNums() {
        this.form.winNum.push({ value: 0 });
      },
      /* 删除中奖人数 */
      removeWinNums(_row, index) {
        this.form.winNum.splice(index, 1);
      },
      addBottoms() {
        let id = 1;
        let length = this.form.winType.length;
        if (length) {
          let lengthSet = length - 1;
          id = this.form.winType[lengthSet].id + 1;
        }
        this.form.winType.push({
          id: id,
          title: '',
          show: '1',
          tag: '',
          itemIds: '',
          ifCode: '0',
          ifAddress: '0',
          img: []
        });
      },
      /* 删除 */
      removeBottoms(_row, index) {
        this.form.winType.splice(index, 1);
      },
      /* 上传文件之前的钩子 */
      onBeforeUpload(file) {
        // file 即选择后的文件
        if (!file.type.startsWith('image')) {
          this.$message.error('只能选择图片');
          return false;
        }
        if (file.size / 1024 / 1024 > 2) {
          this.$message.error('大小不能超过 2MB');
          return false;
        }
      },
      /* 上传文件 */
      onUpload(item) {
        item.status = 'uploading';
        const formData = new FormData();
        formData.append('file', item.file);
        request({
          url: '/upload',
          method: 'post',
          data: formData,
          onUploadProgress: (e) => {
            if (e.lengthComputable) {
              item.progress = (e.loaded / e.total) * 100;
            }
          }
        })
          .then((res) => {
            if (res.data.code === 0) {
              item.status = 'done';
              item.url = res.data.data.full_url;
              item.fileUrl = res.data.data.url;
            }
          })
          .catch(() => {
            item.status = 'exception';
          });
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.goodsQuery();
            this.$util.assignObject(this.form, {
              ...this.data
            });
            this.isUpdate = true;
          } else {
            this.form.sms = '';
            this.form.winType = [];
            this.form.winNum = [];
            this.form.bgImg = [];
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
<style>
  .ele-image-upload-list .ele-image-upload-item {
    margin-top: 2px;
    margin-bottom: 2px;
  }
</style>
