<template>
  <div class="container-accountability infoManagerPage">
    <el-form
      ref="formData"
      :model="formData"
      :inline="true"
      class="search-bm-boder"
      label-width="72px"
    >
      <el-form-item label="单位名称:">
        <el-select placeholder="请选择" v-model="formData.orgCode" clearable class="inputCompatible">
          <el-option
            v-for="item in unitData"
            :key="item.orgCode"
            :label="item.orgName"
            :value="item.orgCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="信息标题:">
        <el-input v-model="formData.title" clearable placeholder="请输入" class="inputCompatible"></el-input>
      </el-form-item>
      <el-form-item label="上报年度:">
        <!--                <el-select v-model="formData.year" placeholder="请选择" clearable class="inputCompatible">-->
        <!--                    <el-option :key="item.index" :label="item" :value="item" v-for="item in dateyear"></el-option>-->
        <!--                </el-select>-->
        <el-date-picker
          type="year"
          v-model="formData.year"
          value-format="yyyy"
          class="inputCompatible"
          placeholder="请选择"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <div class="search-button">
          <span class="search-line"></span>
          <div class="search-select" @click="searchShowMoreBool = !searchShowMoreBool">
            更多条件
            <i class="el-icon-arrow-up" v-show="searchShowMoreBool"></i>
            <i class="el-icon-arrow-down" v-show="!searchShowMoreBool"></i>
          </div>
          <el-button type="primary" class="iconfont iconchaxun" size="small" @click="queryFun">查询</el-button>
          <el-button type="primary" size="small" class="iconfont iconzhongzhi" @click="resetFun">重置</el-button>
        </div>
      </el-form-item>
      <el-row v-show="searchShowMoreBool">
        <el-form-item label="状态:">
          <el-select v-model="formData.status" clearable placeholder="请选择" class="inputCompatible">
            <el-option
              v-for="(item,index) in typeData"
              :key="index"
              :label="item.name"
              :value="item.status"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row class="addBtnBg">
      <el-button
        type="primary"
        size="small"
        class="iconfont iconpiliangcaiyong"
        @click="goBatchUse"
      >批量采用</el-button>
      <el-button
        type="primary"
        size="small"
        class="iconfont iconpiliangweicaiyong"
        @click="goBatchNoUse"
      >批量不采用</el-button>
    </el-row>
    <el-row>
      <el-col :span="24">
        <!-- @row-click="tableRowClick" -->
        <el-table
          :data="tableList"
          v-loading="loading"
          border
          stripe
          highlight-current-row
          @selection-change="selectionChange"
        >
          <el-table-column type="selection" align="center" width="55"></el-table-column>
          <el-table-column align="center" label="序号" width="60">
            <template slot-scope="scope">
              <span>{{scope.$index+1}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="orgName"
            label="单位名称"
            align="center"
            sortable
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="title"
            label="信息标题"
            align="center"
            sortable
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="year"
            label="上报年度"
            align="center"
            sortable
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="publishtime"
            label="填报时间"
            align="center"
            sortable
            :show-overflow-tooltip="true"
            width="200"
          ></el-table-column>
          <el-table-column
            prop="status_value"
            label="状态"
            align="center"
            sortable
            :show-overflow-tooltip="true"
            width="200"
          ></el-table-column>
          <el-table-column label="操作" align="center" width="80">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="light" content="查看" placement="top">
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconchakan2 operate_show_btn"
                  @click.stop="goSee(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="light"
                content="采用"
                placement="top"
                v-if="buttonNameList.indexOf('采用') > -1 && (scope.row.status_value == '未采用' || scope.row.status_value == '已提交')"
              >
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconcaiyong1 operate_withdraw_btn"
                  :disabled="scope.row.isdisabled"
                  @click="goUse(scope.row,scope.$index)"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="light"
                content="不采用"
                placement="top"
                v-if="buttonNameList.indexOf('不采用') > -1 && scope.row.status_value == '已采用'"
              >
                <el-button
                  type="primary"
                  size="small"
                  class="iconfont iconweicaiyong operate_withdraw_btn"
                  :disabled="scope.row.isdisabled"
                  @click="goNoUse(scope.row,scope.$index)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <div class="pag-right clearFix">
          <el-pagination
            class="right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="page.page"
            :page-sizes="[10,20,30,40]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            v-if="paginationShow"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import * as management from "@/api/headResponsibility/infoManager";
import { authorityControl } from "@/api/login/login";
export default {
  data() {
    return {
      dateyear: [],
      searchShowMoreBool: false,
      formData: {
        title: "",
        status: "", //状态
        year: "",
        orgCode: ""
      },
      unitData: [],
      typeData: [],
      paginationShow: true,
      tableList: [],
      loading: false,
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      selList: [],
      buttonNameList: [],
    };
  },
  created() {
    this.getButtonList(
      sessionStorage.getItem("resourceCode"),
      sessionStorage.getItem("userCode")
    );
    var date = new Date();
    this.year = date.getFullYear();
    for (var i = 0; i < 9; i++) {
      this.dateyear.push(this.year);
      this.year = this.year - 1;
    }
    this.dateyear.unshift(this.dateyear[0] + 1);
    this.getBmList();
    this.getStatusType();
    this.getList("start");
  },
  methods: {
    getButtonList(menuCode, userCode) {
      let codeData = {
        resourceCode: menuCode,
        userId: userCode
      };
      authorityControl(codeData).then(res => {
        res.data.forEach(item => {
          this.buttonNameList.push(item.resourceName);
        });
      });
    },
    getBmList() {
      let orgCode = JSON.parse(sessionStorage.getItem("userInfo")).orgId;
      management.getOrg(orgCode).then(res => {
        if (res.infoCode == 200) {
          this.unitData = res.data;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
            duration: 2000
          });
        }
      });
    },
    selectionChange(val) {
      //列表复选框改变事件
      this.selList = [];
      if (val.length == 0) {
        this.selList = [];
      } else {
        val.forEach(item => {
          this.selList.push(item.id);
        });
      }
    },
    getList(type) {
      if (type != "start") {
        //非初始化调用：查询、分页
        sessionStorage.setItem("searchData", JSON.stringify(this.formData));
        sessionStorage.setItem("pageData", JSON.stringify(this.page));
      } else {
        //初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
        if (JSON.parse(sessionStorage.getItem("searchData"))) {
          //详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
          this.formData = JSON.parse(sessionStorage.getItem("searchData"));
          this.page = JSON.parse(sessionStorage.getItem("pageData"));
        } else {
          //第一次进去此菜单时，保存上方默认的data
          sessionStorage.setItem("searchData", JSON.stringify(this.formData));
          sessionStorage.setItem("pageData", JSON.stringify(this.page));
        }
      }
      this.loading = true;
      management.infoManagerList(this.formData, this.page).then(res => {
        if (res.infoCode == 200) {
          this.tableList = res.data.list;
          this.page.total = res.data.total;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
            duration: 2000
          });
        }
        this.loading = false;
      });
    },
    queryFun() {
      //查询
      this.paginationShow = false;
      this.handleCurrentChange(1);
      this.$nextTick(() => {
        this.paginationShow = true;
      });
    },
    resetFun() {
      //重置
      this.formData = {
        title: "",
        status: "", //状态
        year: "",
        orgCode: ""
      };
      this.queryFun();
    },
    tableRowClick(row, column, event) {
      if (event.label != "操作") {
        // this.goSee(row)
      }
    },
    goBatchUse() {
      //批量采用
      if (this.selList.length == 0) {
        this.$message({
          type: "warning",
          message: "请勾选操作项",
          duration: 2000
        });
      } else {
        let needData = {
          idsList: this.selList,
          status: "40"
        };
        this.batchCommon(needData);
      }
    },
    goBatchNoUse() {
      //批量不采用
      if (this.selList.length == 0) {
        this.$message({
          type: "warning",
          message: "请勾选操作项",
          duration: 2000
        });
      } else {
        let needData = {
          idsList: this.selList,
          status: "30"
        };
        this.batchCommon(needData);
      }
    },
    batchCommon(data) {
      management.batchIsUsing(data).then(res => {
        if (res.infoCode == 200) {
          this.$message({
            type: "success",
            message: data.status == "40" ? "批量采用成功" : "批量不采用成功",
            duration: 2000
          });
          this.getList();
        } else {
          this.$message({
            type: "error",
            message: res.msg,
            duration: 2000
          });
        }
      });
    },
    goSee(row) {
      //查看
      this.$router.push({
        path: "/accountability/infoSubmitProvinceSee",
        query: {
          id: row.id,
          state: "show"
        }
      });
    },
    goUse(row, ind) {
      //采用
      let needData = {
        id: row.id,
        status: "40"
      };
      this.useOrNoCommon(needData, ind);
    },
    goNoUse(row, ind) {
      //不采用
      let needData = {
        id: row.id,
        status: "30"
      };
      this.useOrNoCommon(needData, ind);
    },
    useOrNoCommon(data, ind) {
      this.tableList[ind].isdisabled = true;
      management.reportInfo(data).then(res => {
        if (res.infoCode == 200) {
          this.$message({
            type: "success",
            message: data.status == "40" ? "采用成功" : "不采用成功",
            duration: 2000
          });
          this.getList();
        } else {
          this.$message({
            type: "error",
            message: res.msg,
            duration: 2000
          });
          this.tableList[ind].isdisabled = false;
        }
      });
    },
    handleSizeChange(val) {
      //条数改变
      this.page.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      //页数改变
      this.page.page = val;
      this.getList();
    },
    getStatusType() {
      //获取检验类别
      management.statusData().then(res => {
        if (res.infoCode == 200) {
          let needArr = [];
          res.data.map(item => {
            if (item.status != 10) {
              //10代表未提交
              needArr.push(item);
            }
          });
          this.typeData = needArr;
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 2000
          });
        }
      });
    }
    // 时间格式化函数
    // dateFormat(time) {
    //     if (time) {
    //         var day = time.split("T");
    //         //获取日期部分的年月日
    //         var days = day[0].split("-");
    //         return days[0] + "-" + days[1] + "-" + days[2];
    //     } else {
    //         return "空";
    //     }
    // }
  }
};
</script>
<style lang="scss">
.infoManagerPage {
  .el-select {
    width: 100%;
  }
}
</style>
