<template>
  <div class="app-container" style="padding: 0px !important;">
    <div class="market-top">
      <!-- <el-form
        :model="searchArray"
        ref="searchArrayRef"
        size="small"
        :inline="true"
        style="margin-top:20px;"
      >
        <el-form-item label="关联人姓名" prop="relateName">
          <el-input
            v-model="searchArray.relateName"
            placeholder="请输入关联人姓名"
            clearable
            style="width:100%"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="searchFn">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetFn">重置</el-button>
        </el-form-item>
      </el-form> -->
      <el-button v-if="isShow" size="mini" type="primary" class="minibtn" @click="addFn()">新增</el-button>
    </div>
    <el-table v-loading="loading" :data="tableData" :height="dyHeight" :key="tableKey">
      <template>
        <el-table-column label="序号" prop="xh" width="80">
          <template slot-scope="scope">
            <span>{{ scope.$index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="关联人姓名" prop="relateName" min-width="100" show-overflow-tooltip />
        <el-table-column label="联系方式" prop="phoneNum" min-width="100" show-overflow-tooltip />
        <el-table-column label="职务" prop="position" min-width="100" show-overflow-tooltip />
        <el-table-column label="乡镇（街道）" prop="town" min-width="100" show-overflow-tooltip />
        <el-table-column label="行政村名称" prop="village" min-width="100" show-overflow-tooltip />
        <el-table-column label="网点名称" prop="outlet" min-width="100" show-overflow-tooltip />
        <el-table-column label="网格负责人" prop="gridCharger" min-width="100" show-overflow-tooltip />
        <el-table-column label="网格管理员" prop="gridManager" min-width="100" show-overflow-tooltip />
        <el-table-column label="操作" align="center" width="160" v-if="isShow">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="editFn(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" :disabled="!scope.row.id" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="pagination_end">
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        @current-change="currentChangeFn"
        :total="total"
        @size-change="sizeChangeFn"
      ></el-pagination>
    </div>
    
    <import-modal 
      ref="importModal" 
      :dialogTitle="dialogTitle"
      :perInfo="perInfo"
      @onRefreshTable="onRefreshTable"
    />
    <el-dialog title="提示" :visible.sync="showTips" width="30%">
      <span>确认删除该条的数据吗？</span>
      <div slot="footer">
        <el-button @click="handleCancelDelete">取消</el-button>
        <el-button type="primary" @click="handleConfirmDelete">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  teamlist,
  teamdel
} from "@/api/grid/createdjs.js";
import _ from "lodash";
import dayjs from "dayjs";
import { mapGetters } from "vuex";
import ImportModal from './components/import-modal'
export default {
  name: "createdjs",
  components: { ImportModal },
  computed: {
    ...mapGetters(["roles"]),
    isShow() {
      const isOk = ['headAdmin', 'headPrivate', 'headPublic', 'headOps', 'branchAdmin']
      const arr = isOk.filter(item => this.roles.findIndex(v => v === item) > -1)
      return arr.length > 0
    },
  },
  data() {
    return {
      searchArray: {
        relateName: ""
      },
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      total: 0,
      loading: false,
      dyHeight: {},
      tableKey: false,
      dialogTitle: '',
      perInfo: {},
      showTips: false,
      id: ''
    };
  },
  created() {
    window.addEventListener("resize", () => {
      this.dyHeight = Number(window.innerHeight) - 330;
    });
    this.dyHeight = Number(window.innerHeight) - 330;
    this.resetFn();
  },
  filters: {
  },
  methods: {
    showEdit(row) {
      if (row.name || row.phoneNum || row.idNum || row.roleType) {
        return true
      } else {
        return false
      }
    },
    resetFn() {
      this.searchArray = {
        relateName: ""
      };
      this.searchFn();
    },
    searchFn() {
      this.pageNum = 1;
      this.pageSize = 10;
      this.total = 0;
      this.queryListFn();
    },
    queryListFn() {
      this.loading = true;
      let p = {
        ...this.searchArray,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      teamlist(p).then(response => {
        this.loading = false;
        if (response.code == 200) {
          this.tableData = response.rows || [];
          this.total = response.total || 0;
        } else {
          this.$message.error(response.msg || "操作失败");
        }
        this.tableKey = !this.tableKey;
      });
    },
    currentChangeFn(val) {
      this.pageNum = val;
      this.queryListFn();
    },
    sizeChangeFn(val) {
      this.pageSize = val;
      this.queryListFn();
    },
    onRefreshTable() {
      this.pageNum = 1
      this.queryListFn();
    },
    addFn(row) {
      this.dialogTitle = "新增";
      this.perInfo = {}
      this.$refs.importModal.onOpen()
    },
    editFn(row) {
      this.dialogTitle = "编辑";
      this.perInfo = row
      this.$refs.importModal.onOpen()
    },
    delFn(row) {
      if (!row.id) return
      this.id = row.id;
      this.showTips = true;
    },
    handleCancelDelete() {
      this.id = '';
      this.showTips = false;
    },

    handleConfirmDelete() {
      teamdel(this.id).then(response => {
        if (response.code === 200) {
          this.$message.success('删除成功')
          this.showTips = false
          this.queryListFn();
        }
      });
    },
  }
};
</script>

<style lang="scss" scoped>
.market-top {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  .add-btn {
    height: 32px;
    margin-top: 20px;
  }
}
.minibtn {
  height: 28px;
  margin-top: 24px;
  margin-bottom: 20px;
}
.pagination_end {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
}
.header-radio {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #ebebeb;

  .el-radio-button {
    flex: 1;

    ::v-deep .el-radio-button__inner {
      width: 100%;
      border: none;
      font-weight: 400;
      letter-spacing: 0.44px;
      line-height: 25px;
      font-size: 16px;
      color: #666666;
      padding: 11px 0 12px 0;
    }

    ::v-deep .el-radio-button__orig-radio:checked + .el-radio-button__inner {
      background-color: #4886f8;
      font-weight: 400;
      color: #ffffff;
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }

    &:nth-child(2) {
      &::before,
      &::after {
        content: "";
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        height: 21px;
        width: 1px;
        background: #ebebeb;
        z-index: 1;
      }

      &::after {
        right: 1px;
      }
    }

    &.is-active {
      &::before,
      &::after {
        content: none;
      }
    }
  }
}
</style>