<template>
  <div class="common-page-shadow">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="分类编号">
          <el-input
            size="small"
            v-model="querys[0].value"
            clearable
            placeholder="请输入分类编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input
            size="small"
            v-model="querys[1].value"
            clearable
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="container-btn">
        <el-button
          size="small"
          icon="el-icon-search"
          type="danger"
          class="btn btn-plus"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          icon="el-icon-refresh-right"
          class="btn btn-plus"
          @click="clearSearchForm"
        >
          重置
        </el-button>
      </div>
    </div>
    <el-row
      class="common-table-container flex searchPanel"
      :style="SearchPanel"
    >
      <el-col :span="24" class="common-table-wrapper">
        <div class="common-btn-group">
          <!-- <el-button
            size="small"
            icon="el-icon-plus"
            type="primary"
            class="btn btn-plus"
            @click="handleAdd"
          >
            新增
          </el-button>
          <el-button
            :disabled="handleDeleteActive"
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete"
          >
            删除
          </el-button> -->
          <el-button
            size="small"
            icon="el-icon-bottom"
            type="primary"
            plain
            :loading="exporting"
            class="btn btn-plus"
            @click="dataExport"
          >
            导出数据
          </el-button>
        </div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation" ref="tableContent">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            row-key="id"
            v-loading="tableLoading"
            :max-height="tableHeight"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
            @row-dblclick="handleDetail"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column type="selection" min-width="15%" align="center">
            </el-table-column>
            <el-table-column
              prop="code"
              min-width="20%"
              label="分类编号"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="name"
              show-overflow-tooltip
              min-width="25%"
              align="center"
              label="分类名称"
              resizable
            >
            </el-table-column>

            <el-table-column
              prop="yqcCategoryCode"
              min-width="20%"
              label="渝企采分类编码"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="yqcCategoryName"
              min-width="20%"
              label="渝企采分类名称"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              min-width="25%"
              align="center"
              label="状态"
              resizable
            >
              <template v-slot="{row}">
                <el-switch
                  @change="(val) => switchChange(val, row)"
                  v-model="row.status"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  inactive-text="禁用"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </template>
            </el-table-column>
            <!-- <el-table-column min-width="15%" align="center" label="操作"> -->
              <!-- <template v-slot="{row}"> -->
                  <!-- <el-button
                    icon="el-icon-edit"
                    type="primary"
                    plain
                    v-if="row.edit"
                    size="small"
                    @click="handleEdit(row)"
                    >编辑</el-button
                  > -->
                  <!-- <el-button
                    icon="el-icon-plus"
                    type="primary"
                    plain
                    size="small"
                    @click="LowerBtn(row)"
                    >添加下级</el-button
                  > -->
              <!-- </template> -->
            <!-- </el-table-column> -->
          </el-table>
        </div>
        <!-- <div class="common-pagenation">
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParam.pageBean.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="queryParam.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryParam.pageBean.total"
          >
          </el-pagination>
        </div> -->
      </el-col>
    </el-row>
  </div>
</template>

<script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import basicData from '@/api/basicData'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      tableData: [],
      tableHeight: 1000,
      handleDeleteActive: true,
      handleDeleteId: [],
      tableLoading: false,
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      exporting:false,
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'code',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'name',
          relation: 'AND',
          value: '',
        },
      ],
    }
  },
  mounted() {
    this.calcTableHeight()
    //页面缩放从新计算表格的高度
    window.addEventListener('resize', this.calcTableHeight)
  },
  created() {
    this.loadData()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calcTableHeight)
  },
  methods: {
    //改变列表状态
    switchChange(val, row) {
      console.log(row)
      let statusValue = {
        ids: [row.id],
        status: val,
      }
      function recursivefilter(arr) {
        for (let item of arr) {
          if (item.id) {
            statusValue.ids.push(item.id)
          }
          if (item.children.length > 0) {
            recursivefilter(item.children)
          }
        }
        return arr
      }
      recursivefilter(row.children)
      basicData.bizMaterialCategoryUpdateStatus(statusValue).then((res) => {
        if (res.state) {
          this.$message.success(res.message)
        }
        this.loadData()
      })
    },
    // 动态计算表格的高度，自适应当前容器
    calcTableHeight() {
      this.tableHeight = this.$refs.tableContent.offsetHeight
    },
    //导出数据
    dataExport() {
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      let portal = window.context.portal
      let query = {
        url: `${portal}/bizMaterialCategory/v1/exportExcelData`,
        method: 'post',
        data: this.queryParam,
        headers: {
          'Content-type': 'application/json',
        },
        responseType: 'blob', //将文件流转成blob对象
        noErrorMsg: true,
      }
      this.exporting = true
      this.$http
        .request(query)
        .then((res) => {
          if (res.status == 200) {
            this.$message.success('导出成功')
            this.exporting = false
          } else {
            this.$message.error('导出失败')
            this.exporting = false
          }
        })
        .catch(() => {
          this.exporting = false
        })
    },
    //字典翻译
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      this.$http.post('${portal}/bizMaterialCategory/v1/getEnterpriseCategoryTree').then(res=>{
        this.tableLoading = false
        function recursivefilter(arr) {
          for (let item of arr) {
            if (item.hasChildren) {
              item['edit'] = false
              delete item.hasChildren
            }
            if (item.children.length > 0) {
              item['edit'] = true
              recursivefilter(item.children)
            }
          }
          return arr
        }
        recursivefilter(res.data.value)
        this.tableData = res.data.value
        this.tableData.forEach((item) => {
          this.$set(item, 'edit', true)
        })
      })
      // basicData.bizMaterialCategoryQuery(this.queryParam).then(res => {
      //   this.tableLoading = false
      //   this.tableData = res.rows
      // })
    },
    //添加
    handleAdd() {
      this.$router.push('materialeditAddDialog')
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'materialeditAddDialog',
        query: {editRows: rows, pageBean: this.queryParam},
      })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'materialDetail',
        query: {detailRows: rows, pageBean: this.queryParam},
      })
    },
    //添加下级
    LowerBtn(rows) {
      this.$router.push({
        name: 'materialeditAddDialog',
        query: {code: rows, pageBean: this.queryParam},
      })
    },
    // 删除
    handleDelete() {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning',
      })
        .then(() => {
          basicData
            .bizMaterialCategoryDeletCategory(this.handleDeleteId)
            .then((res) => {
              if (res.state) {
                this.$notify({
                  type: 'success',
                  message: '删除成功',
                  duration: 2000,
                })
                this.loadData()
              }
            })
            .catch((res) => {
              this.$notify({
                type: 'error',
                message: res.message,
                duration: 2000,
                offset: 80,
              })
            })
        })
        .catch(() => {})
    },
    //重置
    clearSearchForm() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.loadData()
    },
    // 顶部搜索
    handleSearch() {
      this.loadData()
    },
    handleSelectionChange(val) {
      if (val.length !== 0) {
        this.handleDeleteActive = false
        this.handleDeleteId = val.map((item) => {
          return item.id
        })
      } else {
        this.handleDeleteActive = true
      }
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData(this.queryParam)
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData(this.queryParam)
    },
  },
  computed: {
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px',
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.cloumn-btn {
  border: 1px solid #2761ff;
  padding: 3px 10px;
  border-radius: 5px;
  &:nth-child(1) {
    margin-right: 5px;
  }
  &:nth-child(2) {
    margin-right: 5px;
  }
  &:nth-child(1):hover {
    background-color: #2761ff;
    color: #fff;
  }
  &:nth-child(2):hover {
    background-color: #2761ff;
    color: #fff;
  }

  // &:nth-child(2):hover {
  //   background-color: #f56c6c;
  //   color: #fff;
  //   border-color: #f56c6c;
  // }
  /deep/ i,
  span {
    font-size: 16px;
  }
}
/deep/.el-switch,
/deep/.el-switch__label {
  height: 50px;
  line-height: 50px;
}
/deep/ .el-tree-node__content {
  padding: 5px 0;
}

/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
::v-deep .el-switch__core {
  width: 44px;
  height: 24px;
  border-radius: 100px;
  border: none;
}
::v-deep .el-switch__core::after {
  width: 20px;
  height: 20px;
  top: 2px;
}
::v-deep .el-switch.is-checked .el-switch__core::after {
  margin-left: -21px;
}
/*关闭时文字位置设置*/
::v-deep .el-switch__label--right {
  position: absolute;
  z-index: 1;
  right: 6px;
  margin-left: 0px;
  color: rgba(255, 255, 255, 0.9019607843137255);
  span {
    font-size: 12px;
  }
}
/* 激活时另一个文字消失 */
::v-deep .el-switch__label.is-active {
  display: none;
}
/*开启时文字位置设置*/
::v-deep .el-switch__label--left {
  position: absolute;
  z-index: 1;
  left: 5px;
  margin-right: 0px;
  color: rgba(255, 255, 255, 0.9019607843137255);
  span {
    font-size: 12px;
  }
}
</style>
