<!-- 编辑弹窗 -->
<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-table :data="form.visitTag" :border="true" style="width: 100%">
          <el-table-column label="ID" align="center" width="70px">
            <template v-slot="{ row }">{{ row.id }}</template>
          </el-table-column>
          <el-table-column label="回访标题" align="center" width="150px">
            <template v-slot="{ row }">{{ row.title }}</template>
          </el-table-column>
          <el-table-column label="自定义文案" align="center">
            <template v-slot="{ row }">
              <el-input v-model="row.tag" placeholder="请输入自定义文案" />
            </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 { updateActivityVisitTag } from '@/api/sylive/activity';
  export default {
    components: {},
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        activityId: null,
        visitTag: []
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };
          updateActivityVisitTag(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);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data
            });
            /* 升序排序 */
            const visitTag = this.form.visitTag;
            if (visitTag) {
              this.form.visitTag = visitTag.sort((a, b) => a.id - b.id);
            }
            this.isUpdate = true;
          } else {
            this.form.visitTag = [];
            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>
