<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-18 14:03:14
 -->
<!--
 <image-table :categoryId="currentCategoryId"  :textTableData="tableData" @handleSelectionChange="handleSelectionChange" @refreshTable="refreshTable" @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange"></image-table>
  import imageTable from '@/components/set/image-table.vue';
  -->
<template>
  <div class="text-table-body">
    <el-table class="select-table" ref="multipleTable" :data="tableData" tooltip-effect="dark" :style="{ width: '100%', minHeight: tableH }" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45"> </el-table-column>
      <!-- <el-table-column width="25">
        <template slot="header" slot-scope="scope">
          <el-dropdown style="line-height: 10px; padding: 0; margin-left: -15px; transform: translateY(4px); -webkit-transform: translateY(4px);" @command="handleCommand" placement="bottom-start">
            <span class="el-dropdown-link"><i class="iconfont icongengduo"></i> </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="0"><span :style="{ color: selectRadio == 0 ? '#2f54eb' : '#606266' }">选择当页</span></el-dropdown-item>
              <el-dropdown-item command="1"><span :style="{ color: selectRadio == 1 ? '#2f54eb' : '#606266' }">选择全部</span></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
        <template slot-scope="scope"> </template>
      </el-table-column> -->
      <el-table-column label="标题" width="120" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.materialTitle }} </template>
      </el-table-column>
      <el-table-column prop="" label="内容">
        <template slot-scope="scope"
          ><div style="width: 40px;height:40px;"><img :src="scope.row.imgUrl" alt="" style="width: 40px;height:40px;" /></div
        ></template>
      </el-table-column>
      <el-table-column prop="" label="编辑人" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.staffName }}</template>
      </el-table-column>
      <el-table-column prop="" label="最近编辑时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <div class="color-606266 line-18">{{ scope.row.updateTime | timeStampToYmd }}</div>
          <div class="color-606266 line-18">{{ scope.row.updateTime | timeStampToHms }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="toDistribute(scope.$index, scope.row)" v-if="scope.row.fromMaterialId == '-1'">分配企业</el-button>
          <el-button type="text" @click="toEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="text" @click="toDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
      <dm-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </dm-pagination>
    </div>
    <image-edit v-if="textShow" :editRow="editRow" :categoryId="categoryId" @closeText="closeText" @submitText="submitText"></image-edit>
    <distribution-enterprise :editRow="distributionEditRow" :show="showDistribution" @closeText="closeTextDistribution" @submitText="submitTextDistribution"></distribution-enterprise>
  </div>
</template>
<script>
import distributionEnterprise from '@/components/set/distribution-enterprise.vue';
import imageEdit from '@/components/set/image-edit.vue';
import { postRequest } from '@/api/api';
// import { _debounce } from '@/common/js/public';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    categoryId: {
      type: String,
      default() {
        return '';
      }
    },
    textTableData: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    currentPage: {
      type: [String, Number],
      default() {
        return 1;
      }
    },
    pageSize: {
      type: [String, Number],
      default() {
        return 20;
      }
    },
    total: {
      type: [String, Number],
      default() {
        return 0;
      }
    }
  },
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      textShow: false,
      editRow: {},
      showDistribution: false,
      distributionEditRow: {}
    };
  },
  computed: {},
  methods: {
    /**
     * 文本弹窗关闭
     */
    closeText() {
      const that = this;
      that.textShow = false;
      that.editRow = {};
    },
    submitText() {
      const that = this;
      that.textShow = false;
      that.editRow = {};
      that.$emit('refreshTable');
    },
    closeTextDistribution() {
      this.showDistribution = false;
      this.distributionEditRow = {};
    },
    submitTextDistribution() {
      const that = this;
      that.showDistribution = false;
      that.distributionEditRow = {};
      that.$emit('refreshTable');
    },
    toDistribute(index, row) {
      this.showDistribution = true;
      this.distributionEditRow = row;
    },
    /**
     * 编辑
     */
    toEdit(index, row) {
      const that = this;
      that.textShow = true;
      that.editRow = row;
    },
    /**
     * 删除
     */
    toDel(index, row) {
      const that = this;
      that
        .$confirm('确定删除当前素材?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postDel(index, row);
        })
        .catch(() => {});
    },
    postDel(index, row) {
      const that = this;
      let para = {
        materialIds: row.materialId
      };
      postRequest('/haoban-manage3-web/material-del', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.$emit('refreshTable');
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 表格选择
     */
    handleSelectionChange(val) {
      const that = this;
      that.multipleSelection = val;
      that.$emit('handleSelectionChange', val);
    },

    /**
     * 批量删除
     */
    multDel() {
      const that = this;
      if (!that.multipleSelection.length) {
        that.$message.error({
          duration: 1000,
          message: '请选择删除项'
        });
        return false;
      }
      that.showDialog = true;
    },

    /* *
     *  批量删除---弹窗关闭
     */
    hideDialog(obj) {
      const that = this;
      that.showDialog = false;
      if (!Object.keys(obj).length) {
        return false;
      }
      that.postMultDel(obj);
    },

    /**
     *  分页---页码变化
     *  @param {Number} val
     */
    handleSizeChange(val) {
      const that = this;
      that.$emit('handleSizeChange', val);
    },

    /**
     * 分页---当前页变化
     *  @param {Number} val
     */
    handleCurrentChange(val) {
      const that = this;
      that.$emit('handleCurrentChange', val);
    }
  },
  watch: {
    textTableData: function(newData, oldData) {
      let that = this;
      if (!!newData.length) {
        that.tableData = JSON.parse(JSON.stringify(newData));
      } else {
        that.tableData = [];
      }
    }
  },
  mounted() {
    const that = this;
    that.activeBrand = that.brandId;
    if (!!that.textTableData.length) {
      that.tableData = JSON.parse(JSON.stringify(that.textTableData));
    } else {
      that.tableData = [];
    }
  },
  components: {
    imageEdit,
    distributionEnterprise
  }
};
</script>
<style type="text/scss" lang="scss" scoped>
.text-table-body {
  padding: 0 20px;
}
.color-c0c4cc {
  display: inline-block;
  line-height: 34px;
  padding-left: 10px;
  color: #c0c4cc;
}
.condition-tips {
  display: inline-block;
  vertical-align: middle;
  line-height: 32px;
}
.table-condition {
  // min-width: 1200px;
}
.daily-set-wrap {
  height: 100%;
  .daily-set-content {
    padding: 20px;
    .table-condition-left {
      font-size: 0;
    }
  }
  .depart-item-content {
    width: 213px;
    height: 32px;
    overflow: hidden;
    white-space: nowrap;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    cursor: pointer;
  }
  .item-cell-select {
    >>> .el-select__tags {
      white-space: nowrap;
      overflow: hidden;
    }
  }
  .depart-cell {
    position: relative;
    margin: 0 24px 24px 0;
    .el-icon-circle-close {
      position: absolute;
      top: -10px;
      right: -10px;
      font-size: 16px;
      color: #808995;
      cursor: pointer;
    }
  }
}
</style>
