<template>
  <div class="div_main">
    <div v-show="true">
      <div class="div_search">
        <el-form
          :inline="true"
          :model="searchData"
          ref="ref_searchform"
          :label-width="'80px'"
        >
          <div>
            <el-form-item label="服务类别" prop="serviceCategory">
              <el-select
                v-model="searchData.serviceCategory"
                placeholder="请选择"
                size="small"
                clearable
                class="item_width_2"
              >
                <el-option
                  v-for="item in categoryOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="服务名称" prop="serviceName">
              <el-input
                v-model="searchData.serviceName"
                class="item_width_1"
                size="small"
                clearable
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>

            <el-form-item label="服务内容" prop="serviceContent">
              <el-input
                v-model="searchData.serviceContent"
                class="item_width_2"
                size="small"
                clearable
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>

            <el-form-item label="政策依据" prop="policyGround">
              <el-input
                v-model="searchData.policyGround"
                class="item_width_2"
                size="small"
                clearable
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>

            <el-button
              style="margin-left: 30px"
              size="small"
              class="diy-button--search"
              @click="handleSearch"
              >查询</el-button
            >
            <el-button
              style="margin-left: 10px"
              size="small"
              class="diy-button--reset"
              @click="resetSearch"
              >重置</el-button
            >
          </div>
        </el-form>
      </div>

      <div class="div_table">
        <div class="div_btn">
          <el-button class="diy-button--add" size="small" @click="handleAdd"
            >新增</el-button
          >

          <!-- <el-button
            @click="handleExport"
            class="diy-button--reset"
            size="small"
            >导出</el-button
          > -->
        </div>

        <el-table
          :data="tableData"
          border
          :header-cell-style="{ background: '#2195FE', color: '#FFFFFF' }"
          class="table"
          style="width: 100%"
          :height="maxTableHeight"
        >
          <el-table-column
            label="序号"
            fixed="left"
            type="index"
            align="center"
            width="50"
          />

          <el-table-column
            prop="serviceCategory"
            align="center"
            label="服务类别"
            :show-overflow-tooltip="true"
          >
          </el-table-column>

          <el-table-column
            prop="serviceName"
            label="服务名称"
            align="center"
            :show-overflow-tooltip="true"
          >
          </el-table-column>

          <el-table-column
            prop="serviceContent"
            align="center"
            label="服务内容"
            :show-overflow-tooltip="true"
          >
          </el-table-column>

          <el-table-column
            prop="policyGround"
            align="center"
            :show-overflow-tooltip="true"
            label="政策依据"
          >
          </el-table-column>

          <el-table-column
            prop="policyLevel"
            align="center"
            label="政策级别"
            :show-overflow-tooltip="true"
          >
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <el-button
                @click="handleWatch(scope.row)"
                type="text"
                size="small"
                class=".div-table-button--detail"
                >查看</el-button
              >
              <el-button
                @click="handleDispose(scope.row)"
                type="text"
                size="small"
                class="div-table-button--edit"
                >编辑</el-button
              >
              <el-button
                @click="handleDelete(scope.row)"
                type="text"
                size="small"
                class="div-table-button--delete"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="pageNo"
            :page-sizes="[20, 50, 100, 200]"
            :page-size="parseInt(pageSize)"
            layout="sizes, prev, pager, next, total"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>

    <!-- 修改弹出框 -->
    <el-dialog
      :visible.sync="formShow"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="formTitle"
      width="850px"
      top="5vh"
      class="dialog-h"
      @closed="handleClose"
    >
      <edit-form
        ref="eleEditForm"
        :formId="formId"
        :formType="formType"
        @close="handleClose"
        @afterEdit="handleEditSuccess"
      ></edit-form>
    </el-dialog>
  </div>
</template>

<script>
import { requestPost } from "@/js/dai/request";
import nextTick from "dai-js/tools/nextTick";
import { mapGetters } from "vuex";
import axios from "axios";
import editForm from "./cpts/edit";

function iniSearchData() {
  return {
    serviceCategory: "",
    serviceName: "",
    serviceContent: "",
    policyGround: "",
  };
}

export default {
  components: { editForm },

  data() {
    return {
      tableData: [],

      searchData: iniSearchData(),
      categoryOptions: [],

      pageNo: 1,
      pageSize: window.localStorage.getItem("pageSize") || 20,
      total: 1,

      formId: "",
      formShow: false,
      formTitle: "服务项目",
      formType: "", // 列表list 新增add  修改edit 详情info
    };
  },
  computed: {
    maxTableHeight() {
      return this.$store.state.inIframe
        ? this.clientHeight - 410 + this.iframeHeigh
        : this.clientHeight - 410;
    },
    ...mapGetters(["clientHeight", "iframeHeight"]),
  },
  watch: {
    "searchData.endTime": function (val) {
      if (val && val != "") {
        let arrayTemp = val.split(" ");
        this.searchData.endTime = arrayTemp[0] + " 23:59:59";
      }
    },
  },
  mounted() {
    console.log(this.$store.state);
    this.user = this.$store.state.user;

    this.agencyId = this.user.agencyId;

    this.getTableData();
  },
  methods: {
    handleSearch(val) {
      console.log(this.searchData);
      this.pageNo = 1;
      this.getTableData();
    },

    async handleExport() {
      const url = "/gov/project/icEvent/export";
      const { pageSize, pageNo, searchData } = this;
      axios({
        url: window.SITE_CONFIG["apiURL"] + url,
        method: "post",
        data: {
          pageSize,
          pageNo,
          ...searchData,
        },
        responseType: "blob",
      })
        .then((res) => {
          let fileName = window.decodeURI(
            res.headers["content-disposition"].split(";")[1].split("=")[1]
          );
          console.log("filename", fileName);
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          var url = window.URL.createObjectURL(blob);
          var aLink = document.createElement("a");
          aLink.style.display = "none";
          aLink.href = url;
          aLink.setAttribute("download", fileName);
          document.body.appendChild(aLink);
          aLink.click();
          document.body.removeChild(aLink); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        })
        .catch((err) => {
          console.log("获取导出情失败", err);
          return this.$message.error("网络错误");
        });
    },

    handleAdd() {
      this.formType = "add";
      this.formTitle = "新增";
      this.formShow = true;
    },

    handleWatch(row) {
      this.formType = "watch";
      this.formId = row.serviceProjectId;
      this.formTitle = "查看";
      this.formShow = true;
    },

    handleEdit(row) {
      this.formType = "edit";
      this.formId = row.serviceProjectId;
      this.formTitle = "查看";
      this.formShow = true;
    },

    handleClose() {
      this.formShow = false;
    },

    handleEditSuccess() {
      this.handleClose();
      this.getTableData();
    },

    async handleDel(rowData, rowIndex) {
      console.log(rowData, rowIndex);
      const url = "/heart/icServiceProject/serviceProjectEnabled";
      const { tableData } = this;

      const { data, code, msg } = await requestPost(url, [
        tableData[rowIndex].serviceProjectId,
      ]);

      if (code === 0) {
        this.$message.success("删除成功！");
        this.getTableData();
      } else {
        this.$message.error("操作失败！");
      }
    },

    async getCategoryOpitons() {
      const url = "/sys/dict/data/dictlist";

      const { data, code, msg } = await requestPost(url, {
        dictType: "ic_service_type",
      });

      if (code === 0) {
        this.categoryOptions = data || [];
      } else {
        this.$message.error(msg);
      }
    },

    async getTableData() {
      const url = "/heart/icServiceProject/serviceProjectList";

      const { pageSize, pageNo, searchData } = this;
      const { data, code, msg } = await requestPost(url, {
        pageSize,
        pageNo,
        ...searchData,
      });

      if (code === 0) {
        this.total = data.total || 0;
        this.tableData = data.list
          ? data.list.map((item) => {
              return item;
            })
          : [];
      } else {
        this.$message.error(msg);
      }
    },

    handleSizeChange(val) {
      this.pageSize = val;
      window.localStorage.setItem("pageSize", val);
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getTableData();
    },

    resetSearch() {
      this.searchData = iniSearchData();
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/scss/buttonstyle.scss";
@import "@/assets/scss/modules/management/list-main.scss";
@import "@/assets/scss/modules/shequzhili/event-info.scss";
</style>
