<template>
  <div style="padding-left: 5px">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="#">内容管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/sys/configure">信息配置管理</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-table
      ref="formTable"
      :data="tableData"
      v-loading="loading"
      stripe
      :height="tableHeight"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55"></el-table-column> -->
      <!-- <el-table-column  prop="logo"   label="logo" >
        <template slot-scope="{ row }">
            <a v-if="row.logo" :href="row.logo" target="_blank">
                <el-image
                :size="20"
                :src="row.logo"
                :key="row.id"
                ></el-image>
            </a>
        </template>
      </el-table-column>
      <el-table-column prop="homePageImg"   label="首页图" >
        <template slot-scope="{ row }">
            <a v-if="row.homePageImg" :href="row.homePageImg" target="_blank">
                <el-image
                :size="20"
                :src="row.homePageImg"
                :key="row.id"
                ></el-image>
            </a>
        </template>
      </el-table-column> -->
     <el-table-column
        prop="title"
        label="标题"
      ></el-table-column>
      <el-table-column prop="value"  label="内容">
          <template slot-scope="{ row }">
            <a v-if="row.type=='img'" :href="row.value" target="_blank">
                <el-image
                style="width: 100px; height: 100px"
                :src="row.value"
                :key="row.id"
                ></el-image>
            </a>
            <div v-else-if="row.type=='text'">
                {{row.value}}
            </div>
            <div v-else-if="row.type=='longtext'">
                {{row.value|ellipsis}}
            </div>
          </template>
      </el-table-column>
      <el-table-column prop="valueUd"  label="内容(优电联盟)">
          <template slot-scope="{ row }">
            <a v-if="row.type=='img'" :href="row.valueUd" target="_blank">
                <el-image
                style="width: 100px; height: 100px"
                :src="row.valueUd"
                :key="row.id"
                ></el-image>
            </a>
            <div v-else-if="row.type=='text'">
                {{row.valueUd}}
            </div>
            <div v-else-if="row.type=='longtext'">
                {{row.valueUd|ellipsis}}
            </div>
          </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
      ></el-table-column>
      <el-table-column label="操作" width="350" fixed="right">
        <template slot-scope="{ row }">
          <el-row>
            <el-col>
              <el-link
                type="primary"
                :underline="false"
                @click="handleEdit(row)"
                >编辑</el-link
              >
              -
              <el-link
                type="primary"
                :underline="false"
                @click="handleEditUd(row)"
                >编辑优电联盟</el-link
              >
            </el-col>
            
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="pageIndex"
      :total="totalElements"
      :page-sizes="pageSizeList"
      @current-change="changePage"
      @size-change="pageSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <configure-detail
      v-if="showModal"
      :businessKey="businessKey"
      :title="modalTitle"
      @close="onDetailModalClose"
    ></configure-detail>
    <configure-detailUd
      v-if="showModalUd"
      :businessKey="businessKey"
      :title="modalTitle"
      @close="onDetailModalClose"
    ></configure-detailUd>
  </div>
</template>
<script>

import configureDetail from "./configure-detail";
import configureDetailUd from "./configure-detailUd";
import configureApi from "@/api/sys/configure";

export default {
  name: "SysConfigureList",
  data() {
    var self = this;
    return {
      queryModel: {
        name: "",
      },
      loading: false,
      tableData: [],
      treeData: [],
      pageIndex: 1,
      pageSize: 10,
      totalPages: 0,
      totalElements: 0,
      field: "",
      direction: "",
      pageSizeList: [10, 20, 30],
      multipleSelection: [],
      showModal: false,
      modalTitle: "",
      businessKey: "",
      tableHeight: 300,
      showDeviceModal: false,
      companyId: "",
      qrCode: "",
      companyResult: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
      batchImportVisible: false,
      batchImportFileList: [],
      xlsLoading: false,
      sceneData: [],
      typeData: [],
      deviceRelationTitle: "",
      showIconModal: false,
      iconModalTitle: "",
      showModalUd:false,
    };
  },
  filters:{
      ellipsis(value){
          if(!value)return "";
          if(value.length>20){
              return value.slice(0,20) + "...";
          }
          return value
      }
  },
  created() {

    this.changePage(1);
  },
  methods: {
    changePage(pageIndex) {
      var self = this;

      self.loading = true;

      self.pageIndex = pageIndex;
      var formData = new FormData();

      formData.append("pageIndex", self.pageIndex);
      formData.append("pageSize", self.pageSize);

      formData.append("name", self.queryModel.name);

      configureApi
        .pageList(formData)
        .then(function (response) {
          self.loading = false;

          var jsonData = response.data.data;

          self.tableData = jsonData.data;
          self.totalPages = jsonData.totalPages;
          self.totalElements = jsonData.recordsTotal;
          //45为分页栏的高度
          //页面高度-列表上面的高度-分页栏高度
          self.tableHeight =
            window.innerHeight - self.$refs.formTable.$el.offsetTop - 100;
        })
        .catch((error) => {
          self.loading = false;
          // self.$message.error(error + "");
        });
    },
    pageSizeChange(pageSize) {
      this.pageSize = pageSize;
    },
    sortChange(data) {
      this.field = data.column.field;
      this.direction = data.order == "ascending" ? "asc" : "desc";

      this.changePage(this.pageIndex);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },
    handleAdd() {
      this.modalTitle = "新增";
      this.businessKey = "";
      this.showModal = true;
    },
    handleEdit(record) {
      this.modalTitle = "编辑";
      this.businessKey = record.id;
      this.showModal = true;
    },
    handleEditUd(record){
      this.modalTitle = "编辑优电联盟";
      this.businessKey = record.id;
      this.showModalUd = true;
    },
    handleDelete(record) {
      var self = this;

      self
        .$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          configureApi.remove(record.id).then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              // var index = self.tableData.indexOf(record);
              // self.tableData.splice(index, 1);
              self.changePage(self.pageIndex);

              self.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        });
    },
    handleBatchDelete() {
      var self = this;

      var idList = this.multipleSelection.map((record) => {
        return record.id;
      });

      this.$confirm("是否确认删除选中项？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        configureApi.batchRemove(idList).then(function (response) {
          var jsonData = response.data;

          if (jsonData.result) {
            self.changePage(self.pageIndex);

            self.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        });
      });
    },
    onDetailModalClose(refreshed) {
      //保存成功后回调
      this.showModal = false;
      this.showModalUd = false;
      this.showDeviceModal = false;

      if (refreshed) {
        this.changePage(this.pageIndex);
      }
    },
  },
  mounted: function () {},
  components: {
    "configure-detail": configureDetail,
    "configure-detailUd": configureDetailUd,
  },
};
</script>
<style lang="scss" scoped>
.el-breadcrumb {
  margin: 10px;
  line-height: 20px;
}

.el-divider {
  margin: 5px 0;
}

.demo-form-inline {
  margin-left: 10px;
  text-align: left;
}

.button-group {
  margin-left: 10px;
  text-align: left;
}
</style>