<template>
  <div>
    <div class="app-container">
    <el-tabs v-model="activeName" >
      <el-tab-pane label="引流加人" name="1"></el-tab-pane>
      <el-tab-pane label="引流进群" name="2"></el-tab-pane>
    </el-tabs>
    <div v-if="activeName==='1'">
      <type1></type1>
    </div>
    <div v-if="activeName==='2'">
      <type2></type2>
    </div>
    </div>
  </div>
</template>

<script>
import { listDrainageLink, getDrainageLink, delDrainageLink, addDrainageLink, updateDrainageLink, exportDrainageLink } from "@/api/qw/drainageLink";
import { listContactWay} from "@/api/qw/contactWay";
import type1 from '@/views/qw/drainageLink/type1.vue'
import type2 from '@/views/qw/drainageLink/type2.vue'
export default {
  name: "DrainageLink",
   components: { type1,type2},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName:'1',
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS",
      baseUrl: process.env.VUE_APP_BASE_API,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引流链接表格数据
      drainageLinkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        wayId: null,
        title: null,
        nickName: null,
        phone: null,
        companyName: null,
        createTime: null,
        createBy: null
      },
      // 表单参数
      form: {},
      WayList:[],
      // 表单校验
      rules: {
        name:[{required: true, message: "引流链接名称不能为空", trigger: "blur"}],

        wayId:[{required: true, message: "渠道活码不能为空", trigger: "change"}],

        title:[{required: true, message: "标题不能为空", trigger: "blur"}],


      }
    };
  },
  created() {
    this.getList();
    listContactWay().then(response => {
        this.WayList = response.rows;
      });
  },
  methods: {
    /** 查询引流链接列表 */
    getList() {
      this.loading = true;
      listDrainageLink(this.queryParams).then(response => {
        this.drainageLinkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        wayId: null,
        qrCode: null,
        linkUrl: null,
        title: null,
        nickName: null,
        phone: null,
        companyName: null,
        avatarUrl: null,
        content: null,
        clickNum: null,
        addNum: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加引流链接";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDrainageLink(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改引流链接";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDrainageLink(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrainageLink(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除引流链接编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDrainageLink(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    handleAvatarSuccess(res, file) {
        if(res.code==200){
          this.form.avatarUrl=res.url;
          self.$forceUpdate()
        }
        else{
          this.msgError(res.msg);
        }
    },
    beforeAvatarUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1;
      if (!isLt1M) {
        this.$message.error('上传图片大小不能超过 1MB!');
      }
      return   isLt1M;
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有引流链接数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDrainageLink(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style scoped>
:deep(.el-tabs__nav-wrap::after) {
  position: static !important;
}

.avatar-uploader .el-upload {
   border: 1px dashed #d9d9d9;
   border-radius: 6px;
   cursor: pointer;
   position: relative;
   overflow: hidden;
 }
 .avatar-uploader .el-upload:hover {
   border-color: #409EFF;
 }

 .avatar-uploader-icon {
   font-size: 28px;
   color: #8c939d;
   width: 150px;
   height: 150px;
   line-height: 150px;
   text-align: center;
 }
</style>
