<template>
  <div style="height:100%;max-height:100%;overflow-y:auto;display:flex;flex-direction:column;justify-content:space-between;">
    <div class="box-card" style="background-color: #ECF8FF; height: 40px; font-size: small; line-height: 30px; display: flex; justify-content: space-between; align-items: center;">
      <div style="display:flex;" class="checkbox_left">
        <a-checkbox :checked="checked" @change="onChecked">本级</a-checkbox>
        <a-input-search
          v-model="input5"
          placeholder="请输入对象名称"
          enter-button
          @search="searchDcryResult"
          allow-clear
          style="width: 200px; margin-left: 30px;"/>
      </div>
      <a-button
        v-if="dczId && dczId.length==12"
        @click="editHandler"
        type="primary"
        icon="edit"
        size="small"
        ghost
        style="float: right; margin-right: 10px; height: 30px;">添加对象</a-button>
    </div>
    <a-table
      :data-source="tableData"
      :columns="columns"
      @change="handleCurrentChange"
      :loading="loading"
      rowKey="index"
      :pagination="{
        current: pageIndex,
        total,
        defaultPageSize: pageSize,
        showTotal: (total) => `共${total}条`,
      }"
      style="width: 100%;"
      :style="{ height: tableHeight + 'px' }"
      ref="dcryTable"
    >
      <span slot="action" slot-scope="text">
        <a @click="deleteHandler(text)">删除</a>
      </span>
    </a-table>
  </div>
</template>

<script>
import { deleteDcdxByObjid, getDcdxList1 } from '../../../api/duChaPlan';
export default {
  props: ['dczId', 'activeContent'],
  data() {
    return {
      checked: false,
      isAll: '1',
      pageIndex: 1,
      pageSize: 12,
      total: 0,
      input5: '',
      tableData: [],
      loading: false,
      tableHeight: window.innerHeight - 230,
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 70,
          ellipsis: true,
        },
        {
          title: '对象名称',
          dataIndex: 'nm',
          align: 'center',
          width: 200,
          ellipsis: true,
        },
        {
          title: '行政区划',
          dataIndex: 'adName',
          align: 'center',
          width: 200,
          ellipsis: true,
        },
        {
          title: '督查类别',
          dataIndex: 'typeName',
          align: 'center',
          width: 150,
          ellipsis: true,
        },
        {
          title: '位置',
          dataIndex: 'orgName',
          align: 'center',
          width: 100,
          ellipsis: true,
        },
        {
          title: '所在分组',
          dataIndex: 'groupName',
          align: 'center',
          width: 280,
          ellipsis: true,
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
        },
      ],
    };
  },
  created() {},
  mounted() {
    this.input5 = '';
  },
  methods: {
    onChecked(e) {
      this.checked = e.target.checked;
    },
    searchDcryResult(val) {
      this.input5 = val;
      this.pageIndex = 1;
      this.getDcdxList1();
    },
    editHandler() {
      this.$emit('showEditDialog_dx');
    },
    getDcdxList1() {
      if (!this.dczId) {
        this.loading = false;
        return;
      }
      this.loading = true;
      getDcdxList1(
        this.dczId,
        '111',
        this.pageSize,
        this.pageIndex,
        this.isAll,
        this.input5,
        this.input5,
        this.input5,
      ).then((response) => {
        var res = response.data;
        this.tableData = res.list;
        this.total = res.total;
        this.loading = false;
        this.tableData.forEach((element, index) => {
          element['index'] = this.pageSize * (this.pageIndex - 1) + index + 1;
        });
      });
    },
    handleCurrentChange(item) {
      this.pageIndex = item.current;
      this.getDcdxList1();
    },
    deleteHandler(item) {
      this.$confirm({
        title: '提示',
        content: '是否确认删除?',
        okType: 'info',
        onOk: () => {
          deleteDcdxByObjid(item.objId, '111', '1').then((res) => {
            if (res.success) {
              this.getDcdxList1(); // 删除成功之后  刷新表格数据
              this.$message.success('删除成功');
            } else {
              if (res.message == '请先删除登记表') {
                this.$message.warning('请联系管理员，删除登记表！');
              } else {
                this.$message.error('请删除失败');
              }
            }
          });
        },
        onCancel: () => {
          this.$message.info('已取消删除');
        },
      });
    },
  },
  watch: {
    checked(n, o) {
      if (n == true) {
        this.isAll = '0';
      } else {
        this.isAll = '1';
      }
      this.pageIndex = 1;
      this.getDcdxList1();
    },
    dczId(n, o) {
      this.getDcdxList1();
    },
    // 为了删除区域点回对象列表后更新列表
    activeContent(val) {
      if (val == 'second') {
        this.getDcdxList1();
      }
    },
  },
};
</script>

<style lang='less' scoped>
.checkbox_left{
  margin-left: 20px;
}
/deep/ .ant-checkbox-wrapper {
  white-space: nowrap;
}
/deep/ .ant-table-pagination {
  float: left;
}
/deep/ .ant-table-thead > tr > th, /deep/ .ant-table-tbody > tr > td {
  padding: 0;
  height: 50px;
}
</style>
