<template>
  <div class="content-main">
    <el-form
      :model="formInfo"
      label-width="75px"
      ref="formInfo"
      :inline="true"
      class="search-bm-boder"
    >
      <el-form-item label="系统模块:" prop="gnmk">
        <el-input placeholder="请输入" v-model="formInfo.gnmk" class="inputCompatible" clearable></el-input>
      </el-form-item>
      <el-form-item label="文档标题:" prop="wdbt">
        <el-input placeholder="请输入" v-model="formInfo.wdbt" class="inputCompatible" clearable></el-input>
      </el-form-item>
      <el-form-item label="推荐读者:" prop="tjdz">
        <el-select class="inputCompatible" v-model="formInfo.tjdz" placeholder="请选择" clearable>
          <el-option
            v-for="item in tjdzOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="search-button">
        <span class="search-line"></span>
        <el-button type="primary" @click="goSearch" size="small" class="iconfont iconchaxun">查询</el-button>
        <el-button
          type="primary"
          @click="resetInfo('formInfo')"
          size="small"
          class="iconfont iconzhongzhi"
        >重置</el-button>
      </div>
    </el-form>
    <el-row class="addBtnBg">
      <el-button type="primary" size="small" class="iconfont iconxinzeng" @click="goAdd">新增</el-button>
    </el-row>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      border
      stripe
      highlight-current-row
      @row-click="tableRowClick"
      style="width: 100%"
    >
      <el-table-column label="序号" type="index" align="center" width="60"></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="gnmk"
        label="系统模块"
        sortable
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="wdbt"
        label="文档标题"
        sortable
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="yhcj"
        label="用户场景"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="scope.row.yhcj == 1">管理端</span>
          <span v-else-if="scope.row.yhcj == 2">企业端</span>
          <span v-else-if="scope.row.yhcj == ''"></span>
          <!-- scope.row.yhcj的值是 '1,2' 的情况时 -->
          <span v-else>管理端，企业端</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="tjdz"
        label="推荐读者"
        sortable
      ></el-table-column>
      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="bzrq"
        label="编制日期"
        sortable
      ></el-table-column>

      <el-table-column
        align="center"
        :show-overflow-tooltip="true"
        prop="status"
        label="状态"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">草稿</span>
          <span v-if="scope.row.status == 1">已启用</span>
          <span v-if="scope.row.status == 2">已禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="116">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" content="查看" placement="top">
            <el-button
              type="primary"
              @click="goLook(scope.row)"
              size="small"
              class="operate_show_btn iconfont iconchakan2"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="light"
            content="编辑"
            placement="top"
            v-if="buttonNameList.indexOf('编辑') > -1 && scope.row.status == 0 || scope.row.status == 2"
          >
            <el-button
              type="primary"
              size="small"
              class="operate_edit_btn el-icon-edit-outline"
              @click="goEditor(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="light"
            content="启用"
            placement="top"
            v-if="buttonNameList.indexOf('启用') > -1 && scope.row.status == 2"
          >
            <el-button
              :disabled="scope.row.isdisabled"
              type="primary"
              size="small"
              class="iconfont iconqiyong1 operate_withdraw_btn"
              @click="goIsEnable(scope.row,1,scope.$index)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="light"
            content="禁用"
            placement="top"
            v-if="buttonNameList.indexOf('禁用') > -1 && scope.row.status == 1"
          >
            <el-button
              :disabled="scope.row.isdisabled"
              type="primary"
              size="small"
              class="iconfont iconjinyong operate_withdraw_btn"
              @click="goIsEnable(scope.row,2,scope.$index)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="light"
            content="删除"
            placement="top"
            v-if="buttonNameList.indexOf('删除') > -1 && scope.row.status == 0"
          >
            <el-button
              type="primary"
              size="small"
              class="iconfont iconshanchu operate_delete_btn"
              @click.stop="goDelete(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <div class="pag-right clearFix">
      <el-pagination
        class="right"
        :current-page.sync="limit.page"
        :page-size="limit.size"
        :page-sizes="[10,20,30,40]"
        :total="limit.total"
        @current-change="handleCurrentChangeTask"
        @size-change="handleSizeChangeTask"
        layout="total, sizes, prev, pager, next, jumper"
        v-if="paginationShow"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import {
  helpList,
  helpDelete,
  helpIsEnable
} from "@/api/publicfunction/helpDocument";
import { authorityControl } from "@/api/login/login";
export default {
  data() {
    return {
      listLoading: false,
      paginationShow: true,
      tableData: [],
      buttonNameList: [],
      formInfo: {
        wdbt: "",
        tjdz: "",
        gnmk: "",
        status: [],
        yhcj: []
      },
      limit: {
        page: 1,
        size: 10,
        total: 0
      },
      tjdzOptions: [
        { value: "粮食局", label: "粮食局" },
        { value: "批发市场", label: "批发市场" },
        { value: "储备粮公司", label: "储备粮公司" },
        { value: "区粮办", label: "区粮办" },
        { value: "承储库", label: "承储库" },
        { value: "购销公司(利达集团)", label: "购销公司(利达集团)" }
      ]
    };
  },
  created() {
    this.getButtonList(
      sessionStorage.getItem("resourceCode"),
      sessionStorage.getItem("userCode")
    );
    this.getList();
  },
  methods: {
    tableRowClick(row, column, event) {
      //   if (event.label != "操作") {
      //     this.goLook(row);
      //   }
    },
    getButtonList(menuCode, userCode) {
      let codeData = {
        resourceCode: menuCode,
        userId: userCode
      };
      authorityControl(codeData).then(res => {
        res.data.forEach(item => {
          this.buttonNameList.push(item.resourceName);
        });
      });
    },
    getList() {
      this.listLoading = true;

      helpList(this.formInfo, this.limit).then(res => {
        if (res && res.items) {
          this.tableData = res.items;
          this.limit.total = res.total;
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 2000
          });
        }
        this.listLoading = false;
      });
    },
    //查询
    goSearch() {
      this.paginationShow = false;
      this.handleCurrentChangeTask(1);
      this.$nextTick(function() {
        this.paginationShow = true;
      });
    },
    resetInfo(formName) {
      this.$refs[formName].resetFields();
      this.getList();
    },
    goAdd() {
      this.$router.push({
        path: "/publicfunction/helpManagerAdd"
      });
    },
    goLook(row) {
      this.$router.push({
        path: "/publicfunction/helpManagerAdd",
        query: { wdId: row.wdid, state: "show" }
      });
    },
    goEditor(row) {
      this.$router.push({
        path: "/publicfunction/helpManagerAdd",
        query: { wdId: row.wdid, state: "upDate" }
      });
    },
    goIsEnable(row, status, ind) {
      this.tableData[ind].isdisabled = true;
      helpIsEnable(row.wdid, status).then(res => {
        if (res.code == 200) {
          this.$message({
            message: status == 1 ? "启用成功" : "禁用成功",
            type: "success",
            duration: 2000
          });
          this.getList();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 2000
          });
          this.tableData[ind].isdisabled = false;
        }
      });
    },
    goDelete(row) {
      this.$confirm("您确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        helpDelete(row.wdid).then(res => {
          if (res.code == 200) {
            this.$message({
              message: "删除成功",
              type: "success",
              duration: 2000
            });
            this.getList();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
              duration: 2000
            });
          }
        });
      });
    },
    handleCurrentChangeTask(val) {
      this.limit.page = val;
      this.getList();
    },
    handleSizeChangeTask(val) {
      this.limit.size = val;
      this.getList();
    }
  }
};
</script>
<style>
</style>
