<template>
  <div class="divHome">
    <div style="width:724px;margin: 15px auto;color: #1e1e1e">
      <el-button style="border-radius:100px" type="primary"
                 icon="el-icon-document-checked" @click="saveHomeDataReq()">保存
      </el-button>
      <el-button style="border-radius:100px" type="success"
                 icon="el-icon-finished" @click="issueHomeReq">发布
      </el-button>
      <el-button style="border-radius:100px" type="danger"
                 icon="el-icon-brush" @click="clearData">清空
      </el-button>
    </div>
    <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" circle @click="addItem(0)"></el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div style="display: flex;justify-content: flex-start;flex-wrap: wrap;padding-left: 20px">
      <div v-for="(item,index) in list[0]">
        <div class="topImgConfig" @click="openConfigWindow(0,index)">
          <svg style="width: 210px; height: 120px" v-if="!item.imgPath" xmlns="http://www.w3.org/2000/svg" width="36"
               height="36" fill="currentColor"
               class="bi bi-cloud-plus-fill" viewBox="0 0 16 16">
            <path
              d="M8 2a5.53 5.53 0 0 0-3.594 1.342c-.766.66-1.321 1.52-1.464 2.383C1.266 6.095 0 7.555 0 9.318 0 11.366 1.708 13 3.781 13h8.906C14.502 13 16 11.57 16 9.773c0-1.636-1.242-2.969-2.834-3.194C12.923 3.999 10.69 2 8 2zm.5 4v1.5H10a.5.5 0 0 1 0 1H8.5V10a.5.5 0 0 1-1 0V8.5H6a.5.5 0 0 1 0-1h1.5V6a.5.5 0 0 1 1 0z"/>
          </svg>
          <el-image v-if="item.imgPath"
                    style="width: 210px; height: 120px"
                    :src="baseURL+item.imgPath">
          </el-image>
        </div>
        <div class="closeBtn">
          <el-button size="mini" type="danger" icon="el-icon-close" circle
                     @click="deleteConfigItem(0,index)"></el-button>
        </div>
      </div>
    </div>
    <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" circle @click="addItem(1)"></el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="classify">
      <div class="" v-for="(item, index) in list[1]" :key="index">
        <div class="classify-li middleGridConfig" @click="openConfigWindow(1,index)">
          <div>
            <svg style="width: 80px; height: 80px" v-if="!item.imgPath" xmlns="http://www.w3.org/2000/svg" width="36"
                 height="36" fill="currentColor"
                 class="bi bi-cloud-plus-fill" viewBox="0 0 16 16">
              <path
                d="M8 2a5.53 5.53 0 0 0-3.594 1.342c-.766.66-1.321 1.52-1.464 2.383C1.266 6.095 0 7.555 0 9.318 0 11.366 1.708 13 3.781 13h8.906C14.502 13 16 11.57 16 9.773c0-1.636-1.242-2.969-2.834-3.194C12.923 3.999 10.69 2 8 2zm.5 4v1.5H10a.5.5 0 0 1 0 1H8.5V10a.5.5 0 0 1-1 0V8.5H6a.5.5 0 0 1 0-1h1.5V6a.5.5 0 0 1 1 0z"/>
            </svg>
            <el-image v-if="item.imgPath"
                      style="width: 80px; height: 80px"
                      :src="baseURL+item.imgPath">
            </el-image>
          </div>
          <div class="classify-li__text">
            {{ item.text }}
          </div>
        </div>
        <div class="closeBtn">
          <el-button size="mini" type="danger" icon="el-icon-close" circle
                     @click="deleteConfigItem(1,index)">
          </el-button>
        </div>
      </div>
    </div>
    <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" circle @click="addItem(2)"></el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div>
      <el-row class="bottomList" v-for="(item,index) in list[2]">
        <div @click="openConfigWindow(2, index)">
          <el-col :span="6">
            <div style="padding-left: 5px">
              <svg style="width: 150px; height: 100px" v-if="!item.imgPath" xmlns="http://www.w3.org/2000/svg"
                   width="36" height="36" fill="currentColor"
                   class="bi bi-cloud-plus-fill" viewBox="0 0 16 16">
                <path
                  d="M8 2a5.53 5.53 0 0 0-3.594 1.342c-.766.66-1.321 1.52-1.464 2.383C1.266 6.095 0 7.555 0 9.318 0 11.366 1.708 13 3.781 13h8.906C14.502 13 16 11.57 16 9.773c0-1.636-1.242-2.969-2.834-3.194C12.923 3.999 10.69 2 8 2zm.5 4v1.5H10a.5.5 0 0 1 0 1H8.5V10a.5.5 0 0 1-1 0V8.5H6a.5.5 0 0 1 0-1h1.5V6a.5.5 0 0 1 1 0z"/>
              </svg>
              <el-image v-if="item.imgPath"
                        style="width: 160px; height: 110px"
                        :src="baseURL+item.imgPath">
              </el-image>
            </div>
          </el-col>
          <el-col :span="16">
            <div style="font-size: 22px;margin: 25px 0 20px">
              {{ item.text }}
            </div>
            <div>
              <span>
                {{ parseTime(item.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
              </span>
            </div>
          </el-col>
          <el-col :span="2">
            <div style="text-align: right">
              <el-button size="mini" type="danger" icon="el-icon-close" circle
                         @click="deleteConfigItem(2,index)">
              </el-button>
            </div>
          </el-col>
        </div>
      </el-row>
    </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="配置项">
            <el-input v-model="homeName" :disabled="true"/>
          </el-form-item>
          <el-form-item label="文本内容">
            <el-input v-model="form.text" placeholder="请填写文本内容" maxlength="10"
                      show-word-limit/>
          </el-form-item>
          <el-form-item label="链接地址" prop="link">
            <el-input v-model="form.link" placeholder="请输入链接地址"
                      show-word-limit/>
          </el-form-item>
          <el-form-item label="设置图像">
            <imageUpload v-model="form.imgPath"/>
          </el-form-item>
          <el-form-item label="首页分组类型" prop="homeGroupType">
            <el-select v-model="form.homeGroupType" placeholder="请选择首页分组类型" :disabled="true">
              <el-option
                v-for="dict in dict.type.home_grou_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </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} from "@/api/app/apphomedata";
import {getHomeData, issueHome, saveHomeData} from "@/api/app/appHomeConfig";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Apphomedata",
  props: {
    homeContentId: {
      type: Number,
      default: ''
    },
    appHomeId: {
      type: Number,
      default: ''
    }
  },
  dicts: ['home_grou_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API,
      list: [
        [{
          text: '未设置',
          img: '',
          link: ''
        }],
        [{
          text: '未设置',
          img: '',
          link: ''
        }],
        [{
          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: {
    saveHomeDataReq() {
      const data = {
        homeContentId: this.homeContentId,
        topImg: this.list[0],
        middleIcon: this.list[1],
        bottomList: this.list[2],
      }
      saveHomeData(data).then(re => {
        if (re.code === 200) {
          this.$modal.msgSuccess("保存成功");
        } else {
          this.$modal.msgError(re.msg)
        }
      })
    },
    issueHomeReq() {
      issueHome({
        appHomeId: this.appHomeId
      }).then(re => {
        if (re.code === 200) {
          this.$modal.msgSuccess("发布成功");
        } else {
          this.$modal.msgError(re.msg)
        }
      })
    },
    clearData() {
      this.reset()
      this.list.length = 0
    },
    getHomeDataReq() {
      getHomeData({
        homeId: this.homeContentId
      }).then(re => {
        const data = re.data;
        this.form = data
        this.homeName = data.text
        this.homeContentId = data.homeContentId
        this.list[0] = data.topImg
        this.list[1] = data.middleIcon
        this.list[2] = data.bottomList
      })
    },
    openConfigWindow(type, index) {
      this.open = true;
      this.form = this.list[type][index]
      this.form.homeGroupType = type + 1
      this.form.index = index
    },
    deleteConfigItem(type, index) {
      console.log(this.list[type])
      this.list[type].splice(index, 1)
      console.log(this.list[type])
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        homeContentId: null,
        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");
    },
    /** 查询app首页数据下拉树结构 */
    getTreeselect() {
      listApphomedata().then(response => {
        this.apphomedataOptions = [];
        const data = {homeContentId: 0, text: '顶级节点', children: []};
        data.children = this.handleTree(response.data, "homeContentId", "homeParentId");
        this.apphomedataOptions.push(data);
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.homeContentId) {
        this.form.homeParentId = row.homeContentId;
      } else {
        this.form.homeParentId = 0;
      }
      this.open = true;
      this.title = "添加app首页数据";
    },
    /** 修改按钮操作 */
    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;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除app首页数据编号为"' + row.homeContentId + '"的数据项？').then(function () {
        return delApphomedata(row.homeContentId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        //
      });
    },
    addItem(type, index) {
      if (index || index === 0) {
        this.list[type].splice(index, 1, this.form)
        console.log(this.list);
        return;
      }
      //复制一份
      const copy = this.list[type]
      //在复制数组里添加
      copy.push({
        text: "未设置",
        link: "",
        imgPath: "",
        homeGroupType: type + 1,
        updateTime: '',
      })
      /*使用变异的 .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: 824px;
  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>
