<template>
  <div
    class="classification_list"
    v-loading.fullscreen.lock="fullscreenLoading"
  >
    <div class="classification_list_headerSelect">
      <div class="classification-list-btn">
        <el-form ref="form" :model="form" inline label-width="90px">
          <el-form-item class="header_top_form" label="分类名称：" prop="name">
            <el-input
              v-model.trim="form.name"
              size="small"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item class="header_top_form" label="状态：" prop="status">
            <el-select
              v-model="form.status"
              clearable
              size="small"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div style="margin-right: 23px">
          <el-button
            size="small"
            style="width: 88px"
            type="primary"
            @click="search"
            >查询</el-button
          >
          <el-button size="small" style="width: 88px" @click="resetForm('form')"
            >重置</el-button
          >
        </div>
      </div>
      <div style="text-align: right">
        <el-button
          size="small"
          style="width: 104px; margin-right: 23px"
          @click="addList"
          icon="iconfont icon-a-zu13"
          >新增</el-button
        >
      </div>
    </div>
    <div>
      <el-table
        ref="editTable"
        :data="tableData"
        stripe
        :header-cell-style="{ background: '#F7F7F7' }"
        :height="screenHeight"
      >
        <el-table-column
          label="分类名称"
          prop="name"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="链接地址"
          prop="linkUrl"
          align="center"
          min-width="180"
        ></el-table-column>
        <el-table-column
          label="禁用/启用"
          prop="status"
          align="center"
        >
          <template slot-scope="scope">
            <div>
                <el-switch
                  v-model="scope.row.status"
                  active-color="#17A8FF"
                  inactive-color="#AFAFAF"
                  @change="switchChange($event, scope.row)"
                >
                </el-switch>
            </div> </template
        ></el-table-column>
        <el-table-column
          label="创建人"
          prop="createUserName"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="140px" align="center">
          <template slot-scope="scope">
            <div>
              <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.pageNum"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="form.pageSize"
      background
      layout="total,prev, pager, next,jumper"
      :total="form.total"
    >
    </el-pagination>
    <edit-dialog ref="editDialogRefs" />
  </div>
</template>
<script>
import editDialog from './edit'
import * as api from '@/api/api'
export default {
  name: 'classification',
  components: {
    editDialog
  },
  data() {
    return {
      fullscreenLoading: false,
      form: {
        name: '',
        status: '',
        pageNum: 1,
        pageSize: 10,
        start: '',
        total: 0
      },
      tableData: [],
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 }
      ],
      screenHeight: document.body.clientHeight - 320,
      tableDataheight: 0
    }
  },
  mounted() {
    this.screenHeight = document.body.clientHeight - 320
    window.onresize = () => {
      return (() => {
        this.screenHeight = document.body.clientHeight - 320
      })()
    }
    this.tableDataList()
  },
  methods: {
    /**
     * @method 重置
     * **/
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.search()
    },
    /**
     * @method 搜索
     * **/
    search() {
      if (sessionStorage.getItem('classifica')) {
        this.form.pageNum = Number(sessionStorage.getItem('classifica'))
        sessionStorage.removeItem('classifica')
      } else {
        this.form.pageNum = 1
      }

      this.tableDataList()
    },
    /**
     * @method 列表数据
     * **/
    tableDataList() {
      this.tableData = []
      this.fullscreenLoading = true
      let code = {
        name: this.form.name,
        page: this.form.pageNum,
        pageSize: this.form.pageSize,
        start: this.form.start,
        status: this.form.status
      }
      api
        .GET('/doc/type/list', code)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list
            this.tableData.forEach(item => {
              item.status == 1 ? (item.status = true) : (item.status = false)
            })
            this.form.pageNum = res.data.currPage
            this.form.pageSize = res.data.pageSize
            this.form.total = res.data.totalCount
            this.fullscreenLoading = false
            this.$nextTick(() => {
              this.$refs.editTable.bodyWrapper.scrollTop = this.tableDataheight
              this.tableDataheight = 0
            })
          }
        })
        .catch(() => {
          this.fullscreenLoading = false
        })
    },
    /**
     * @method 分页条数
     * **/
    handleSizeChange(val) {
      this.form.pageNum = 1
      this.form.pageSize = val
      this.tableDataList()
    },
    /**
     * @method 分页
     * **/
    handleCurrentChange(val) {
      this.form.pageNum = val
      this.tableDataList()
    },
    // 编辑
    edit(val) {
      this.tableDataheight = this.$refs.editTable.bodyWrapper.scrollTop
      sessionStorage.setItem('classifica', this.form.pageNum)
      let code = {
        title: '分类编辑',
        show: false,
        typeId: val.typeId
      }
      this.$refs.editDialogRefs.openClose(code)
    },
    /**
     * @method 新增
     * **/
    addList() {
      let code = {
        title: '分类新增',
        show: true
      }
      this.$refs.editDialogRefs.openClose(code)
    },
    /**
     * @method 切换状态
     * **/
    switchChange(val, item) {
      this.tableDataheight = this.$refs.editTable.bodyWrapper.scrollTop
      sessionStorage.setItem('classifica', this.form.pageNum)
      val ? (val = 1) : (val = 0)
      let code = {
        id: item.typeId,
        status: val
      }
      api
        .PUT('/doc/type/updateSwitch', code)
        .then(data => {
          if (data.code == 0) {
            this.search()
            this.$message.success(data.message)
          }
        })
        .catch(() => {
          this.search()
        })
    }
  }
}
</script>
<style lang="less">
.classification_list {
  .classification_list_headerSelect {
    .classification-list-btn {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
    }

    margin-bottom: 20px;
    .el-form-item {
      margin-bottom: 10px;
      margin-right: 60px;
      .el-input__inner {
        background: #f5f5f5;
        opacity: 1;
        border-radius: 17px;
      }
    }
    .icon-a-zu13 {
      font-size: 11px;
      margin-right: 5px;
    }
  }
}
</style>