<template>
  <div id="taskManagement">
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
    >
      <template slot="btn" slot-scope="props">
        <el-button
          type="text"
          v-if="props.scope.row.status === 1"
          @click="statusChange(props.scope.row, 0)"
          >关闭</el-button
        >
        <el-button
          type="text"
          v-if="props.scope.row.status === 0"
          @click="statusChange(props.scope.row, 1)"
          >启用</el-button
        >
        <el-button type="text" @click="addClick(props.scope.row, 0)"
          >配置</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      :visible.sync="dialogBox"
      width="520px"
      :show-close="false"
      :destroy-on-close="true"
      :before-close="closeBefore"
      :fullscreen="fullscreen"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">
          {{ statusPop === 1 ? "添加" : statusPop === 0 ? "修改" : "详情" }}
        </div>
        <div class="rightBoxs">
          <img
            src="@/assets/images/Max@2x.png"
            alt=""
            @click="fullscreen = !fullscreen"
          />
          <img src="@/assets/images/Close@2x.png" alt="" @click="closeBefore" />
        </div>
      </div>
      <div>
        <el-form
          label-position="right"
          label-width="80px"
          :model="formLabelAlign"
        >
          <el-form-item label="任务名称">
            <el-select
              :disabled="statusPop !== 1"
              v-model="formLabelAlign.taskType"
              placeholder="请选择"
              size="mini"
            >
              <el-option
                v-for="item in optionsList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="初始积分" v-if="formLabelAlign.taskType === 0">
            <el-input-number
              v-model="formLabelAlign.startIntegral"
              controls-position="right"
              :min="0"
              size="mini"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="最低分数" v-if="formLabelAlign.taskType === 5">
            <el-input-number
              v-model="formLabelAlign.minNum"
              controls-position="right"
              :min="0"
              size="mini"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="增加积分" v-if="formLabelAlign.taskType === 0">
            <el-input-number
              v-model="formLabelAlign.accIntegral"
              controls-position="right"
              :min="0"
              size="mini"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="奖励次数" v-if="formLabelAlign.taskType === 5">
            <el-input-number
              v-model="formLabelAlign.countDay"
              controls-position="right"
              :min="0"
              size="mini"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="最短时长" v-if="formLabelAlign.taskType === 4">
            <el-input-number
              v-model="formLabelAlign.longTime"
              controls-position="right"
              :min="0"
              size="mini"
            ></el-input-number>
          </el-form-item>
          <el-form-item
            label="奖励积分"
            v-if="
              formLabelAlign.taskType === 1 ||
              formLabelAlign.taskType === 2 ||
              formLabelAlign.taskType === 3 ||
              formLabelAlign.taskType === 4
            "
          >
            <el-input-number
              size="mini"
              v-model="formLabelAlign.startIntegral"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item
            :label="
              formLabelAlign.taskType === 0 || formLabelAlign.taskType === 4
                ? '积分上限'
                : formLabelAlign.taskType === 3
                ? '分享上限'
                : '未定义'
            "
            v-if="
              formLabelAlign.taskType === 0 ||
              formLabelAlign.taskType === 4 ||
              formLabelAlign.taskType === 3
            "
          >
            <el-input-number
              size="mini"
              v-model="formLabelAlign.endIntegral"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="积分奖励" v-if="formLabelAlign.taskType === 5">
            <div
              v-for="(item, index) in formLabelAlign.activityTaskTimeVo"
              :key="index"
            >
              <el-input-number
                style="width: 100px"
                size="mini"
                v-model="item.startRange"
                controls-position="right"
                :min="0"
              ></el-input-number
              >~<el-input-number
                size="mini"
                style="width: 100px"
                v-model="item.endRange"
                controls-position="right"
                :min="0"
              ></el-input-number>
              :
              <el-input-number
                size="mini"
                style="width: 100px"
                v-model="item.integral"
                controls-position="right"
                :min="0"
              ></el-input-number
              ><el-button size="mini" @click="delLists(index)">删除</el-button>
            </div>
            <el-button size="mini" @click="addListzs">新增</el-button>
          </el-form-item>
          <el-form-item label="说明">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="formLabelAlign.description"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="formLabelAlign.remarks"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeBefore">取 消</el-button>
        <el-button
          type="primary"
          @click="rulesTableSumbit"
          v-if="statusPop !== 2"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import searchBox from "@/components/searchBox";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  components: { searchBox, tableList, pagination },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "查出任务管理配置",
        index: 0,
        ch: "条",
        num: true,
        choice: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      statusPop: 0,
      fullscreen: false,
      dialogBox: false,
      // 表单
      tableSet: [
        {
          label: "任务名称",
          prop: "taskType",
          hidden: true,
          scope: "taskType",
        },
        {
          label: "说明",
          prop: "description",
          hidden: true,
        },
        {
          label: "状态",
          prop: "status",
          hidden: true,
          scope: "status",
        },
        {
          label: "备注",
          prop: "remarks",
          hidden: true,
        },
      ],
      optionsList: [
        {
          label: "每日签到",
          value: 0,
        },
        {
          label: "完善信息",
          value: 1,
        },
        {
          label: "邀请好友",
          value: 2,
        },
        {
          label: "每日分享",
          value: 3,
        },
        {
          label: "视频学习",
          value: 4,
        },
        {
          label: "模拟考试",
          value: 5,
        },
      ],
      formLabelAlign: {
        activityTaskTimeVo: [],
      },
      releaseTime: [],
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    delLists(v) {
      this.formLabelAlign.activityTaskTimeVo.splice(v, 1);
    },
    addListzs() {
      if (!this.formLabelAlign.activityTaskTimeVo) {
        this.formLabelAlign.activityTaskTimeVo = [{}];
      } else {
        this.formLabelAlign.activityTaskTimeVo.push({});
      }
    },
    statusChange(v, int) {
      var self = this;
      this.$api.activitytaskeditTask({ id: v.id, status: int }).then((res) => {
        if (res.code === 200) {
          if (int === 0) {
            self.$message.success("关闭成功");
          }
          if (int === 1) {
            self.$message.success("启用成功");
          }
        }
        this.search();
      });
    },
    search() {
      this.loading = true;
      var data = {
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api.activitytasklist(data).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.navText.index = res.total;
      });
      this.loading = false;
    },
    init() {
      this.search();
    },
    addClick(v, int) {
      if (int === undefined) {
        this.statusPop = 1;
        this.formLabelAlign = {
          activityTaskTimeVo: [],
        };
      } else {
        this.formLabelAlign = v;
        this.statusPop = int;
      }
      this.dialogBox = true;
    },
    closeBefore() {
      if (this.statusPop === 2) {
        this.dialogBox = false;
        this.fullscreen = false;
      } else {
        this.$confirm("此操作将关闭窗口, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.dialogBox = false;
            this.fullscreen = false;
            this.formLabelAlign = {};
          })
          .catch(() => {});
      }
    },
    async rulesTableSumbit() {
      if (
        this.formLabelAlign.taskType === "" ||
        this.formLabelAlign.taskType == undefined ||
        this.formLabelAlign.taskType === null
      ) {
        this.$message.error("请选择任务名称");
        return;
      }
      if (this.formLabelAlign.taskType === 0) {
        if (
          this.formLabelAlign.startIntegral === "" ||
          this.formLabelAlign.startIntegral == undefined ||
          this.formLabelAlign.startIntegral === null
        ) {
          this.$message.error("请输入初始积分");
          return;
        }
        if (
          this.formLabelAlign.accIntegral === "" ||
          this.formLabelAlign.accIntegral == undefined ||
          this.formLabelAlign.accIntegral === null
        ) {
          this.$message.error("请输入增加积分");
          return;
        }
        if (
          this.formLabelAlign.endIntegral === "" ||
          this.formLabelAlign.endIntegral == undefined ||
          this.formLabelAlign.endIntegral === null
        ) {
          this.$message.error("请输入初始积分");
          return;
        }
      }
      if (
        this.formLabelAlign.taskType === 1 ||
        this.formLabelAlign.taskType === 2
      ) {
        if (
          this.formLabelAlign.startIntegral === "" ||
          this.formLabelAlign.startIntegral == undefined ||
          this.formLabelAlign.startIntegral === null
        ) {
          this.$message.error("请输入奖励积分");
          return;
        }
      }
      if (this.formLabelAlign.taskType === 3) {
        if (
          this.formLabelAlign.startIntegral === "" ||
          this.formLabelAlign.startIntegral == undefined ||
          this.formLabelAlign.startIntegral === null
        ) {
          this.$message.error("请输入奖励积分");
          return;
        }
        if (
          this.formLabelAlign.endIntegral === "" ||
          this.formLabelAlign.endIntegral == undefined ||
          this.formLabelAlign.endIntegral === null
        ) {
          this.$message.error("请输入分享上限");
          return;
        }
      }
      if (this.formLabelAlign.taskType === 4) {
        if (
          this.formLabelAlign.longTime === "" ||
          this.formLabelAlign.longTime == undefined ||
          this.formLabelAlign.longTime === null
        ) {
          this.$message.error("请输入最短时长");
          return;
        }
        if (
          this.formLabelAlign.startIntegral === "" ||
          this.formLabelAlign.startIntegral == undefined ||
          this.formLabelAlign.startIntegral === null
        ) {
          this.$message.error("请输入奖励积分");
          return;
        }
        if (
          this.formLabelAlign.endIntegral === "" ||
          this.formLabelAlign.endIntegral == undefined ||
          this.formLabelAlign.endIntegral === null
        ) {
          this.$message.error("请输入积分上限");
          return;
        }
      }
      if (this.formLabelAlign.taskType === 5) {
        if (
          this.formLabelAlign.minNum === "" ||
          this.formLabelAlign.minNum == undefined ||
          this.formLabelAlign.minNum === null
        ) {
          this.$message.error("请输入最低分数");
          return;
        }
        if (
          this.formLabelAlign.countDay === "" ||
          this.formLabelAlign.countDay == undefined ||
          this.formLabelAlign.countDay === null
        ) {
          this.$message.error("请输入奖励次数");
          return;
        }
        if (this.formLabelAlign.activityTaskTimeVo.length === 0) {
          this.$message.error("请输入积分奖励");
          return;
        }
      }

      var data = {
        accIntegral: this.formLabelAlign.accIntegral,
        countDay: this.formLabelAlign.countDay,
        description: this.formLabelAlign.description,
        endIntegral: this.formLabelAlign.endIntegral,
        longTime: this.formLabelAlign.longTime,
        minNum: this.formLabelAlign.minNum,
        remarks: this.formLabelAlign.remarks,
        startIntegral: this.formLabelAlign.startIntegral,
        status: 1,
        taskType: this.formLabelAlign.taskType,
      };
      if (this.statusPop === 1) {
        data.activityTaskTimeAddBo = this.formLabelAlign.activityTaskTimeVo;
        this.$api.activitytaskaddTask(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("新增成功");
            this.search();
            this.dialogBox = false;
          }
        });
      } else {
        data.id = this.formLabelAlign.id;
        data.activityTaskTimeEditBoList =
          this.formLabelAlign.activityTaskTimeVo;
        this.$api.activitytaskeditTask(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.search();
            this.dialogBox = false;
          }
        });
      }
    },
    handleSizeChange(v) {
      this.pageSize = v;
      this.currentPage = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.currentPage = v;
      this.search();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  // .el-dialog__body {
  //   padding: 0;
  //   .contentBox {
  //     padding: 20px 20px 5px;
  //     .el-col {
  //       padding: 0px 20px;
  //       margin-bottom: 30px;
  //       header {
  //         margin-bottom: 6px;
  //         color: #2f4378;
  //         font-size: 14px;
  //       }
  //     }
  //   }
  // }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
.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: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

