<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="930px"
    :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" label-width="82px">
      <el-form-item label="导入名单:">
        <el-upload
          drag
          action=""
          class="ele-block"
          v-loading="loading"
          accept=".xls,.xlsx"
          :show-file-list="false"
          :before-upload="doUpload"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处, 或 <em>点击上传</em>
          </div>
          <template v-slot:tip>
            <div class="el-upload__tip ele-text-center">
              <span>只能上传xls、xlsx文件, </span>
              <el-link
                download
                :href="url"
                type="primary"
                :underline="false"
                style="vertical-align: baseline"
              >
                下载模板
              </el-link>
            </div>
          </template>
        </el-upload>
        <el-button
          icon="el-icon-plus"
          style="width: 100%; margin-top: 15px"
          @click="addMobile"
        >
          新增手机号
        </el-button>
      </el-form-item>
      <el-form-item label="名单列表:">
        <el-table :data="form.blacklist" :border="true" style="width: 100%">
          <el-table-column label="手机号" align="center">
            <template v-slot="{ row }">
              <el-input
                v-if="row.type == 'add'"
                v-model="row.mobile"
                placeholder="请输入手机号"
              />
              {{ row.type != 'add' ? row.mobile : '' }}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="70px"
            align="center"
            :resizable="false"
          >
            <template v-slot="{ row, $index }">
              <span class="ele-action" v-if="row.type != 'del'">
                <el-popconfirm
                  title="确定要删除此手机号?"
                  @confirm="removeMobile($index)"
                >
                  <template v-slot:reference>
                    <el-link
                      icon="el-icon-delete"
                      type="danger"
                      :underline="false"
                    >
                      删除
                    </el-link>
                  </template>
                </el-popconfirm>
              </span>
              <span class="ele-action" v-else>已删除</span>
            </template>
          </el-table-column>
        </el-table>
      </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 {
    updateActivityBlacklist,
    importActivityBlacklist
  } from '@/api/sylive/activity';
  import { API_BASE_URL } from '@/config/setting';
  export default {
    components: {},
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        activityId: null,
        blacklist: []
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        // 导入请求状态
        // 导入模板下载地址
        url: API_BASE_URL.replace('api', '') + 'temp/blacklist.xlsx'
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 上传 */
      doUpload(file) {
        if (
          ![
            'application/vnd.ms-excel',
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
          ].includes(file.type)
        ) {
          this.$message.error('只能选择 excel 文件');
          return false;
        }
        if (file.size / 1024 / 1024 > 5) {
          this.$message.error('大小不能超过 5MB');
          return false;
        }
        this.loading = true;
        importActivityBlacklist(file, this.form.activityId)
          .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);
          });
        return false;
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };
          updateActivityBlacklist(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);
            });
        });
      },
      /* 添加手机 */
      addMobile() {
        this.form.blacklist.push({ mobile: '', type: 'add' });
      },
      /* 删除手机 */
      removeMobile(index) {
        if (!this.form.blacklist[index].id) {
          this.form.blacklist.splice(index, 1);
        } else {
          this.form.blacklist[index].type = 'del';
        }
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data
            });
            this.isUpdate = true;
          } else {
            this.form.blacklist = [];
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
<style lang="scss" scoped>
  .ele-block {
    :deep(.el-upload),
    :deep(.el-upload-dragger) {
      width: 100%;
    }
  }
</style>
