<template>
  <div class="divHome" style="margin-top: 30px">
    <div style="padding-left: 20px">
      <el-row>
        <el-col :span="3">
          <div>
            <h2>资讯列表</h2>
          </div>
        </el-col>
        <el-col :span="4">
          <div style="margin-top: 15px">
            <el-button size="mini" type="primary" icon="el-icon-plus" @click="addItem(2)">新增</el-button>
            <el-button size="mini" type="success" icon="el-icon-document-checked" @click="saveHomeDataReq()">暂存
            </el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div>
      <el-table :data="list[2]" @selection-change="handleSelectionChange" @expand-change="expandChange">
        <el-table-column type="expand">
          <template slot-scope="props">
            <div>
              <div style="padding: 10px 30px; font-size: 22px">
                发布中
              </div>
              <div style="padding: 0 30px">
                <el-table :data="[{0:props.row.hsAppHomeData}]" style="width: 100%">
                  <el-table-column label="封面" align="center">
                    <!--                -->
                    <img v-if="props.row.hsAppHomeData.imgPath === undefined " height="100" width="100"
                         src="http://114.115.142.166:8087/down/O1FKVNmmA1up"/>
                    <img v-else height="90" width="120" :src="baseURL+props.row.hsAppHomeData.imgPath"/>
                  </el-table-column>
                  <el-table-column align="center" label="资讯标题">
                    <template>
                      {{ props.row.hsAppHomeData.text }}
                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="更新时间">
                    {{ props.row.hsAppHomeData.updateTime }}
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="资讯标题" align="center" prop="text" width="180">
        </el-table-column>
        <el-table-column label="封面" align="center" prop="imgPath" width="180">
          <template slot-scope="scope">
            <img v-if="scope.row.imgPath  !== undefined && scope.row.imgPath  !== null && scope.row.imgPath  !== '' " height="90"
                 width="120"
                 :src="baseURL+scope.row.imgPath"/>
            <img v-else height="100" width="100" src="http://114.115.142.166:8087/down/O1FKVNmmA1up"/>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-promotion"
              @click="issueHomeReq(scope.row)"
            >发布
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="openConfigWindow(2, scope.$index)"
              v-hasPermi="['app:apphome:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['app:apphome:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <!-- 添加或修改app首页数据对话框 -->
      <el-dialog title="详细配置" :visible.sync="open" width="700px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item label="设置封面">
            <imageUpload v-model="form.imgPath"/>
          </el-form-item>
          <el-form-item label="文本标题">
            <el-input v-model="form.text" placeholder="请填写文本内容" maxlength="30"
                      show-word-limit/>
          </el-form-item>
          <el-form-item label="内容">
            <div style="padding: 30px">
              <editor v-model="form.link" :min-height="392"/>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm(form.homeGroupType,form.index)">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {delApphomedata, getApphomedata, listApphomedata, updateApphomedata} from "@/api/app/apphomedata";
import {getHomeData, issueHome, saveHomeData} from "@/api/app/appHomeConfig";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Apphomedata",
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API,
      list: [
        [],
        [],
        []
      ],
      // {
      //   text: '未设置',
      //     img: '',
      //   link: '',
      //   date: "0000-0-0 00:00"
      // }
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        homeParentId: null,
        text: null,
        link: null,
        imgPath: null,
        homeGroupType: null,
      },
      homeName: '',
      homeContentId: '',
      // 表单参数
      form: {
        configName: '',
        index: 0
      },
      // 表单校验
      rules: {
        homeParentId1: [
          {required: true, message: "父级id不能为空", trigger: "blur"}
        ],
        homeGroupType: [
          {required: true, message: "首页分组类型不能为空", trigger: "change"}
        ],
      }
    }
  },
  create() {
  },
  mounted() {
    this.getHomeDataReq()
  },
  methods: {
    expandChange(row, expandedRows) {
      if (row.hsAppHomeData === null) {
        this.$modal.msgError("暂无发布数据");
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.appHomeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    saveHomeDataReq() {
      const data = {
        topImg: this.list[0],
        middleIcon: this.list[1],
        bottomList: this.list[2],
      }
      saveHomeData(data).then(re => {
        if (re.code === 200) {
          this.$modal.msgSuccess("保存成功");
          setTimeout(() => {
            this.getHomeDataReq()
          }, 100);
        } else {
          this.$modal.msgError(re.msg)
        }
      })
    },
    issueHomeReq(row) {
      issueHome(row).then(re => {
        if (re.code === 200) {
          this.$modal.msgSuccess("发布成功");
          this.getHomeDataReq()
        } else {
          this.$modal.msgError(re.msg)
        }
      })
    },
    clearData() {
      this.reset()
      this.list.length = 0
    },
    getHomeDataReq() {
      listApphomedata(null).then(re => {
        this.list[2] = []
        let data = re.data
        for (let i = 0; i < data.length; i++) {
          //在数组里添加
          this.list[2].push(data[i])
        }
        this.list.splice(2, 1, this.list[2])
        console.log(this.list[2])
      })
    },
    openConfigWindow(type, index) {
      this.open = true;
      this.form = this.list[type][index]
      this.form.index = index
    },
    deleteConfigItem(type, index) {
      console.log(this.list[type])
      this.list[type].splice(index, 1)
      this.saveHomeDataReq()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        homeParentId: null,
        text: null,
        link: null,
        imgPath: null,
        homeGroupType: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.homeParentId = row.homeContentId;
      }
      getApphomedata(row.homeContentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改app首页数据";
      });
    },
    /** 提交按钮 */
    submitForm(type, index) {
      //this.addItem(type - 1, index)
      this.open = false;
      this.saveHomeDataReq()
      // updateApphomedata(this.form).then(re => {
      //   if (re.code === 200) {
      //     this.$modal.msgSuccess("保存成功");
      //   }
      // })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row);
      this.$modal.confirm('确认删除该资讯同时删除已发布的资讯吗？').then(function () {
        return delApphomedata(row.homeContentId);
      }).then(() => {
        this.getHomeDataReq();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        //
      });
    },
    addItem(type, index) {
      if (index || index === 0) {
        this.list[type].splice(index, 1, this.form)
        return;
      }
      //复制一份
      const copy = this.list[type]
      //在复制数组里添加
      copy.push({
        text: "未设置",
        link: "",
        imgPath: null,
        homeGroupType: type + 1,
        updateTime: this.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}') ,
      })
      /*使用变异的 .splice(起点（下标）,终点（下标）,newValue) 方法
        替换 list[type]中的数据 触发 Vue.set()方法让Dom元素响应该改变视图
        push()无法触发Vue.set() 所以无法改变视图
        终点为0时在起点后添加元素 newValue 不入参时为删除*/
      this.list.splice(type, 1, copy)
      console.log(this.list);
    }
  }
}
</script>

<style scoped lang="scss">
.divHome {
  width: 1424px;
  background-color: rgba(180, 188, 204, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.05);
  padding: 10px;
  margin: 0 auto;
  border-radius: 20px;
}

.topImgConfig {
  max-width: 320px;
  max-height: 200px;
  margin: 10px;
  border: 1px solid rgba(36, 33, 33, 0.13);
  border-radius: 10px;
  padding: 10px;
}

.middleGridConfig {
  margin: 0 15px;
  padding: 5px 15px;
  text-align: center;
  border: 1px solid rgba(36, 33, 33, 0.13);
  border-radius: 10px;
}

.topImgConfig:hover, .middleGridConfig:hover, .bottomList:hover {
  background-color: rgba(36, 33, 33, 0.01);
  border: 1px solid rgba(36, 33, 33, 0.4);
}

.bottomList {
  border: 1px solid rgba(0, 0, 0, 0.14);
  border-radius: 10px;
  padding: 5px;
  margin-bottom: 5px;
}

.closeBtn {
  margin: 10px 0;
  text-align: center;
}

.classify {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  padding: 0 24px 8px;
  margin-top: 20px;
  margin-left: 20px;

  .classify-li {
    flex: 0 0 13%;
    font-size: 24px;

    &__img {
      display: block;
      width: 28px;
      height: 28px;
      border-radius: 50%;
      margin: auto;
    }

    &__text {
      font-size: 16px;
      display: block;
      color: #5a5a5a;
      text-align: center;
      margin: -2px 0 4px 0;
    }
  }
}
</style>
