<template>
  <div id="advertising">
    <search-box ref="search_box" :formList="formList" @search="search" @init="init" />
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row, 2)"
          >详情</el-button
        >
        <el-button
          type="text"
          @click="changeStatus(props.scope.row, 3)"
          v-if="props.scope.row.status === 1 || props.scope.row.status === 2"
          >发布</el-button
        >
        <el-button
          type="text"
          @click="changeStatus(props.scope.row, 2)"
          v-if="props.scope.row.status === 3"
          >撤回</el-button
        >
        <el-button
          type="text"
          @click="addClick(props.scope.row, 0)"
          v-if="props.scope.row.status === 1 || props.scope.row.status === 2"
          >修改</el-button
        >
        <el-button
          type="text"
          @click="changeStatus(props.scope.row, 0)"
          v-if="props.scope.row.status === 1"
          >删除</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      :visible.sync="dialogBox"
      width="920px"
      :show-close="false"
      :destroy-on-close="true"
      :fullscreen="fullscreen"
      :close-on-click-modal="false"
    >
      <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>
      <el-row class="contentBox" :span="24">
        <el-col :span="12">
          <el-col :span="24">
            <header>广告名称：</header>
            <el-input
              :disabled="statusPop === 2"
              placeholder="请输入广告名称"
              v-model="formLabelAlign.advName"
          /></el-col>
          <el-col :span="24">
            <header>跳转链接：</header>
            <el-input
              :disabled="statusPop === 2"
              placeholder="请输入跳转链接"
              v-model="formLabelAlign.jumpUrl"
            />
          </el-col>
          <el-col :span="24">
            <header>发布方式：</header>
            <el-radio-group v-model="formLabelAlign.reStatus">
              <el-radio :label="0" :disabled="statusPop === 2"
                >手动发布</el-radio
              >
              <el-radio :label="1" :disabled="statusPop === 2"
                >定时发布</el-radio
              >
            </el-radio-group>
          </el-col>
          <el-col :span="24">
            <header>广告类型：</header>
            <el-radio-group v-model="formLabelAlign.type">
              <el-radio :label="1" :disabled="statusPop === 2"
                >学习中心</el-radio
              >
              <el-radio :label="2" :disabled="statusPop === 2"
                >求职中心</el-radio
              >
            </el-radio-group>
          </el-col>
          <el-col :span="24">
            <header>有效期：</header>
            <el-date-picker
              :disabled="statusPop === 2"
              v-model="releaseTime"
              type="datetimerange"
              range-separator="~"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="timestamp"
            >
            </el-date-picker>
          </el-col>
          <el-col :span="24">
            <header>备注：</header>
            <el-input
              :disabled="statusPop === 2"
              placeholder="请输入备注"
              v-model="formLabelAlign.remarks"
            />
          </el-col>
        </el-col>
        <el-col :span="12">
          <el-col :span="24">
            <header>广告图片：</header>
            <div class="imgBox">
              <label class="imgLabel" for="inputs">
                <div class="msPhoto" v-if="formLabelAlign.adverUrl">
                  <img
                    :src="$methodsTools.splitImgHost(formLabelAlign.adverUrl)"
                    alt="图片加载失败"
                  />
                </div>
                <div class="imgbbx" v-else>
                  <p style="margin-top: 49px">点击添加或将文件拖拽到这里上传</p>
                  <i class="el-icon-plus"></i>
                  <p style="margin-bottom: 37px">
                    图片格式：.jpg/.png/jpeg/bmp
                  </p>
                </div>
                <input
                  id="inputs"
                  type="file"
                  ref="file"
                  :disabled="statusPop === 2"
                  style="display: none"
                  @change="getImgFile"
                />
              </label>
              <p>请上传比例为16：9且小于2m的图片</p>
            </div>
          </el-col>
        </el-col>
      </el-row>
      <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,
      releaseTime: [],
      //搜索
      formList: [
        {
          label: "发布状态",
          prop: "status",
          placeholder: "请选择发布状态",
          scope: "select",
          options: [
            {
              label: "已删除",
              value: 0,
            },
            {
              label: "待发布",
              value: 1,
            },
            {
              label: "已撤回",
              value: 2,
            },
            {
              label: "已发布",
              value: 3,
            },
          ],
        },
        {
          label: "发布方式",
          prop: "reStatus",
          placeholder: "请选择发布方式",
          scope: "select",
          options: [
            {
              label: "手动发布",
              value: 0,
            },
            {
              label: "定时发布",
              value: 1,
            },
          ],
        },
        {
          label: "广告类型",
          prop: "type",
          placeholder: "请选择广告类型",
          scope: "select",
          options: [
            {
              label: "学习中心",
              value: 1,
            },
            {
              label: "求职中心",
              value: 2,
            },
          ],
        },
        {
          label: "添加时间",
          prop: "startTimeList",
          scope: "datePicker",
        },
        {
          label: "发布时间",
          prop: "issueTimeList",
          scope: "datePicker",
        },
        {
          label: "到期时间",
          prop: "TimeList",
          scope: "datePicker",
        },
        {
          label: "广告名称",
          prop: "advName",
          placeholder: "请输入广告名称",
        },
      ],
      // 表单
      tableSet: [
        {
          label: "名称",
          prop: "advName",
          hidden: true,
        },
        {
          label: "添加时间",
          prop: "createTime",
          hidden: true,
          scope: "aTimeList",
        },
        {
          label: "发布时间",
          prop: "startTime",
          hidden: true,
          scope: "aTimeList",
        },
        {
          label: "发布方式",
          prop: "reStatus",
          hidden: true,
          scope: "reStatus",
        },
        {
          label: "广告类型",
          prop: "type",
          hidden: true,
          scope: "ggType",
        },
        {
          label: "有效期",
          prop1: "startTime",
          prop2: "endTime",
          hidden: true,
          scope: "TimeLists",
        },
        {
          label: "发布状态",
          prop: "status",
          hidden: true,
          scope: "statusRe",
        },
        {
          label: "备注",
          prop: "remarks",
          hidden: true,
        },
      ],
      formLabelAlign: {
        reStatus: 1,
      },
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      beif: {},
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    getImgFile() {
      var self = this;
      var file = this.$refs.file.files[0];
      if (file === undefined) {
        self.$set(self.formLabelAlign, "adverUrl", "");
        return;
      }
      if (file.size > 2 * 1024 * 1024) {
        this.$message.error("图片不得大于2MB");
        return;
      }
      var type = this.$refs.file.value.toLowerCase().split(".").splice(-1);
      if (
        type[0] != "jpg" &&
        type[0] != "png" &&
        type[0] != "jpeg" &&
        type[0] != "bmp"
      ) {
        this.$message.error("上传格式需为：.jpg/.png/.jpeg/bmp");
        this.$refs.file.value = "";
        return;
      }
      var reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = function (ev) {
        self.$set(self.formLabelAlign, "adverUrl", ev.target.result);
      };
    },
    search(v,os) {
      this.loading = true;
      if (os) {
        this.currentPage = 1;
      }
      if(v === undefined){
        v = {}
      }
      var data = {
        advName: v.advName,
        status: v.status,
        reStatus: v.reStatus,
        type: v.type,
        startCreateTime: v.startTimeList
          ? (v.startTimeList[0] / 1000).toFixed(0)
          : "",
        endCreateTime: v.startTimeList
          ? (v.startTimeList[1] / 1000).toFixed(0)
          : "",
        starIssueTime: v.issueTimeList
          ? (v.issueTimeList[0] / 1000).toFixed(0)
          : "",
        endIssueTime: v.issueTimeList
          ? (v.issueTimeList[1] / 1000).toFixed(0)
          : "",
        startTime: v.TimeList ? (v.TimeList[0] / 1000).toFixed(0) : "",
        endTime: v.TimeList ? (v.TimeList[1] / 1000).toFixed(0) : "",
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api.inquireAdvertisingStorage(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 = {};
      } else {
        this.beif = JSON.stringify(v);
        this.formLabelAlign = JSON.parse(this.beif);
        this.releaseTime = [
          this.formLabelAlign.startTime * 1000,
          this.formLabelAlign.endTime * 1000,
        ];
        this.statusPop = int;
      }
      this.dialogBox = true;
    },
    closeBefore() {
      this.dialogBox = false;
      this.fullscreen = false;
      this.formLabelAlign = {};
    },
    async rulesTableSumbit() {
      if (!this.formLabelAlign.advName) {
        this.$message.error("请输入广告名称");
        return;
      }
      if (!this.formLabelAlign.jumpUrl) {
        this.$message.error("请输入跳转链接");
        return;
      }
      if (
        this.formLabelAlign.reStatus !== 0 &&
        this.formLabelAlign.reStatus !== 1
      ) {
        this.$message.error("请选择发布方式");
        return;
      }
      if (this.formLabelAlign.type !== 1 && this.formLabelAlign.type !== 2) {
        this.$message.error("请选择广告类型");
        return;
      }
      if (!this.releaseTime) {
        this.$message.error("请选择有效期");
        return;
      }
      if (
        this.formLabelAlign.adverUrl === "" ||
        this.formLabelAlign.adverUrl === undefined
      ) {
        this.$message.error("请上传广告图片");
        return;
      }
      if (this.statusPop === 0) {
        var objNew = JSON.parse(this.beif);
        if (this.formLabelAlign.adverUrl !== objNew.adverUrl) {
          const awtimg = await this.imgUpload(0);
        }
      } else {
        const awtimg = await this.imgUpload(0);
      }
      var data = {
        advName: this.formLabelAlign.advName,
        jumpUrl: this.formLabelAlign.jumpUrl,
        reStatus: this.formLabelAlign.reStatus,
        type: this.formLabelAlign.type,
        startTime: Number(this.releaseTime[0]) / 1000,
        endTime: Number(this.releaseTime[1]) / 1000,
        remarks: this.formLabelAlign.remarks,
        adverUrl: this.formLabelAlign.adverUrl,
      };
      if (this.statusPop === 1) {
        this.$api.addAdvertisingStorage(data).then((sult) => {
          this.$message.success("添加成功!");
          this.dialogBox = false;
          this.search();
        });
      } else {
        data.id = this.formLabelAlign.id;
        this.$api.modifyAdvertisingStorage(data).then((sult) => {
          this.$message.success("修改成功!");
          this.dialogBox = false;
          this.search();
        });
      }
    },
    changeStatus(v, int) {
      var self = this;
      var data = {
        id: v.id,
        status: int,
      };
      this.$api.modifyAdvertisingStorage(data).then((res) => {
        if (res.code === 200) {
          if (int === 0) {
            self.$message.success("删除成功");
          } else if (int === 2) {
            self.$message.success("撤回成功");
          } else if (int === 3) {
            self.$message.success("发布成功");
          }
          this.search();
        }
      });
    },
    imgUpload(int) {
      return new Promise((resolve, reject) => {
        this.$upload
          .upload(this.$refs.file.files[0], int)
          .then((res) => {
            this.$set(this.formLabelAlign, "adverUrl", res);
            resolve();
          })
          .catch((err) => {
            this.$message.error("图片上传错误");
          });
      });
    },
    close() {},
    handleSizeChange(v) {
      this.pageSize = v;
      this.currentPage = 1;
      this.search(this.$refs.search_box.formData);
    },
    handleCurrentChange(v) {
      this.currentPage = v;
      this.search(this.$refs.search_box.formData);
    },
  },
};
</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>

