<template>
  <div class="container-accountability">
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline search-form"
      label-width="80px"
    >
      <el-row>
        <el-form-item label="考核年度:">
          <el-date-picker
            v-model="formInline.khnd"
            type="year"
            placeholder="请选择"
            format="yyyy"
            value-format="yyyy"
            style="width: 160px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="search-button">
          <span class="search-line"></span>
          <el-button
            type="primary"
            @click="doSearch"
            size="small"
            class="iconfont iconchaxun"
            >查询</el-button
          >
          <el-button
            type="primary"
            @click="doReset"
            size="small"
            class="iconfont iconzhongzhi"
            >重置</el-button
          >
        </el-form-item>
      </el-row>
    </el-form>
    <el-row class="addBtnBg">
      <el-button
        type="primary"
        size="small"
        class="iconfont iconxinzeng"
        @click="addLedger('add')"
        >新增</el-button
      >
    </el-row>
    <el-row>
      <el-table
        :data="tableData"
        v-loading="listLoading"
        border
        stripe
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="序号"
          width="50"
          type="index"
        ></el-table-column>
        <el-table-column
          align="center"
          min-width="100"
          prop="khnd"
          label="考核年度"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="pfbmc"
          label="评分表名称"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="fbsj"
          label="发布时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          prop="fbdx"
          label="发布对象"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          min-width="100"
          prop="zt"
          label="状态"
          show-overflow-tooltip
        ></el-table-column>

        <el-table-column align="center" label="操作" width="160">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                @click="handleClick(scope.row.id, 0)"
                type="primary"
                size="small"
                class="iconfont operate_show_btn iconchakan2"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="light"
              content="查看发布详情"
              placement="top"
            >
              <el-button
                @click="handleFb(scope.row.id, scope.row.pfbmc, 0)"
                type="primary"
                size="small"
                class="iconfont operate_show_btn iconchakan2"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="light"
              content="编辑"
              placement="top"
              v-if="buttonNameList.indexOf('编辑') > -1"
            >
              <el-button
                @click="handleClick(scope.row.id, 1)"
                v-show="scope.row.zt == '草稿' || scope.row.zt == '驳回'"
                type="primary"
                size="small"
                class="operate_edit_btn iconfont iconxiugai1"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="light"
              content="删除"
              placement="top"
              v-if="buttonNameList.indexOf('删除') > -1"
            >
              <el-button
                @click="handleDelete(scope.row.id)"
                v-show="scope.row.zt == '草稿'"
                type="primary"
                size="small"
                class="operate_delete_btn iconfont iconshanchu"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="light"
              content="接收"
              placement="top"
              v-if="buttonNameList.indexOf('接收') > -1"
            >
              <el-button
                @click="handleJs(scope.row.id)"
                v-show="scope.row.zt == '待接收'"
                type="primary"
                size="small"
                class="operate_edit_btn iconfont iconjieshou"
              ></el-button>
            </el-tooltip>
            <!-- <el-tooltip
              class="item"
              effect="light"
              content="驳回"
              placement="top"
              v-if="buttonNameList.indexOf('驳回') > -1"
            >
              <el-button
                @click="handleJs(scope.row.id,'1')"
                v-show="scope.row.zt == '待接收'"
                type="primary"
                size="small"
                class="operate_edit_btn iconfont iconbohui"
              ></el-button>
            </el-tooltip> -->
            <el-tooltip
              class="item"
              effect="light"
              content="发布"
              placement="top"
              v-if="buttonNameList.indexOf('发布') > -1"
            >
              <el-button
                @click="handleFb(scope.row.id, scope.row.pfbmc, 1)"
                v-show="scope.row.zt == '待发布'"
                type="primary"
                size="small"
                class="operate_edit_btn iconfont iconfabu"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageForm.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageForm.total"
        :key="key"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
// import {
//   getLedgerList,
//   update,
//   deleteItem,
//   pageExport,
//   getArea,
// } from "@/api/administrativeRecords/annualLedger/index";
import { authorityControl } from "@/api/login/login";
import {
  getKhpgPage,
  updateKhpgg,
  deleteKhpg,
} from "@/api/evaluationMan/index";

export default {
  data() {
    return {
      qyOptions: [], // 区域
      listLoading: true,
      formInline: {
        khnd: "",
      },
      pageForm: {
        page: 1,
        size: 10,
        total: 0,
      },
      tableData: [],
      buttonNameList: [],
      key: 0,
    };
  },
  methods: {
    doReset() {
      this.formInline = {
        khnd: "",
      };

      this.doSearch();
    },
    doSearch() {
      this.pageForm.page = 1;
      this.listLoading = true;
      this.getList();
    },
    getButtonList() {
      let codeData = {
        resourceCode: sessionStorage.getItem("resourceCode"),
        userId: sessionStorage.getItem("userCode"),
      };
      authorityControl(codeData).then((res) => {
        res.data.forEach((item) => {
          this.buttonNameList.push(item.resourceName);
        });
        this.getList("start");
      });
    },
    getList(type) {
      if (type != "start") {
        //非初始化调用：查询、分页
        sessionStorage.setItem("searchData", JSON.stringify(this.formInline));
        sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
      } else {
        //初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
        if (JSON.parse(sessionStorage.getItem("searchData"))) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          this.formInline = JSON.parse(sessionStorage.getItem("searchData"));
          this.pageForm = JSON.parse(sessionStorage.getItem("pageData"));
        } else {
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(this.formInline));
          sessionStorage.setItem("pageData", JSON.stringify(this.pageForm));
        }
      }
      // console.log(this.$route.meta.pageType,"msg---------");
      let data = {
        ...this.formInline,

        pageType: 0,
      };
      getKhpgPage(data, {
        pageNum: this.pageForm.page,
        pageSize: this.pageForm.size,
      })
        .then((res) => {
          this.listLoading = false;
          if (res.infoCode === 200) {
            this.tableData = res.data.items;
            this.pageForm.total = res.data.total;
            this.pageForm.page = res.data.pageNum;
            return;
          }
          this.$message.error("获取数据失败");
        })
        .catch((err) => {
          this.listLoading = false;
          this.$message.error("获取数据失败");
        });
    },
    // 新增
    addLedger(type) {
      this.$router.push({
        name: "assessmentFormManDetail",
        query: {
          type: type,
        },
      });
    },
    // type : detail查看，update编辑，reject退回
    handleClick(id, type) {
      this.$router.push({
        name: "assessmentFormManDetail",
        query: { type: type, id: id },
      });
    },
    handleFb(id, pfbmc, type) {
      this.$router.push({
        name: "assessmentFormManPush",
        query: { id: id, pfbmc: pfbmc, type: type },
      });
    },
    //接收
    handleJs(id) {
      this.$router.push({
        name: "assessmentFormManDetail",
        query: { type: "js", id: id },
      });
    },
    //删除
    handleDelete(id) {
      this.$confirm("此操作将删除该记录, 是否继续?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteKhpg(id)
            .then((res) => {
              if (res.infoCode == 200) {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    handleSizeChange(val) {
      this.pageForm.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageForm.page = val;
      this.getList();
    },
  },
  created() {
    this.getButtonList();
  },
};
</script>

<style lang="scss" scoped>
.iconfont {
  font-size: 13px;
}
</style>
