<template>
  <div v-if="pageLoading" class="resi-container">
    <el-card ref="searchCard" class="search-card">
      <div class="">
        <el-form ref="searchForm" :inline="true" :model="searchForm" size="small" label-width="100px" class="demo-form-inline">
          <div>
            <!-- <el-form-item label="所属党组织" prop="partyOrgId">
              <el-cascader
                v-model="partyOrgRange"
                :options="optionsG"
                :props="partyProps"
                clearable
                @change="handlePartyChange"></el-cascader>
            </el-form-item> -->
            <el-form-item label="姓名" prop="name">
              <el-input v-model="searchForm.name" placeholder="请输入" class="input-width" clearable></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="searchForm.mobile" placeholder="请输入" class="input-width" clearable></el-input>
            </el-form-item>
            <el-form-item label="身份证" prop="idCard">
              <el-input v-model="searchForm.idCard" placeholder="请输入" class="input-width" clearable></el-input>
            </el-form-item>
            <el-form-item label="审核状态" prop="status">
              <el-select v-model="searchForm.status" filterable :disabled="disabled" placeholder="请选择" class="input-width" clearable>
                <el-option
                  v-for="item in zwList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>

            </el-form-item>
          </div>
          <el-form-item label="申请时间"
                        prop="startTime">
            <el-date-picker v-model="timeRange"
                            size="small"
                            type="daterange"
                            value-format="yyyy-MM-dd"
                            @change="handleTimeChange"
                            range-separator="至"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间">
            </el-date-picker>

          </el-form-item>
          
          <el-form-item>
            <el-button class="diy-button--search" size="small" @click="handleSearch">查询</el-button>
            <el-button class="diy-button--reset" size="small" @click="resetForm('searchForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="resi-card-table">
      
      <div class="resi-row-btn">
        <el-button class="diy-button--add" size="small" @click="handleAdd">批量审核</el-button>
        
      </div>
      
      <el-table
        :data="tableData"
        row-key="id"
        v-loading="tableLoading"
        border
        :height="tableHeight"
        style="width: 100%"
        class="resi-table"
        @select="handleSelection"
      >
        <el-table-column type="selection" align="center" width="50">
        </el-table-column>
        <el-table-column label="序号" type="index" align="center" width="50">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          align="center"
          width="100"
        />
        <el-table-column
          prop="gender"
          label="性别"
          align="center"
          width="100"
        />
        <el-table-column
          prop="idCard"
          label="身份证号"
          align="center"
          min-width="180"
        />
        <el-table-column
          prop="mobile"
          label="手机号"
          align="center"
          min-width="160"
        />
        <el-table-column
          prop="gridName"
          label="所在网格"
          align="center"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          prop="title"
          label="标题"
          align="center"
          show-overflow-tooltip
          min-width="180"
        />
        <el-table-column
          prop="ruleName"
          label="类别"
          align="center"
          min-width="160"
        />
        <el-table-column
          prop="createdTime"
          label="申请时间"
          align="center"
          width="160"
        />
        <el-table-column
          prop="status"
          label="审核状态"
          align="center"
          show-overflow-tooltip
          width="100"
        >
          <template slot-scope="scope">
           <span v-if="scope.row.status == 0">待审核</span>
           <span v-else-if="scope.row.status == 2">已通过</span>
           <span v-else-if="scope.row.status == 1">已驳回</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
              @click="handleLook(scope.row)"
              type="text"
              size="small"
              class="btn-color-look"
              >查看</el-button
            >
            <el-button
              v-if="scope.row.status == 0"
                @click="handleEdit(scope.row, 'edit')"
                type="text"
                size="small"
                class="btn-color-edit"
                >审核</el-button
              >
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-sizes="[20, 50, 100, 200]"
          :page-size="pageSize"
          layout="sizes, prev, pager, next, total"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog title="审核信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        
        <el-form-item label="审核状态" prop="status" :label-width="'100px'">
          <el-select v-model="form.status" placeholder="请选择" class="input-width">
            <el-option
                v-for="item in zwLists"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" :label-width="'100px'">
          <el-input v-model="form.remark" autocomplete="off" class="input-width"></el-input>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handlerCancle">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="详情" :visible.sync="dialogVisible" width="800px">
      <el-form :model="detailInfo" inline>
        
        <div>
          <el-form-item label="所属网格:" :label-width="'100px'">
            <div class="input-width1">{{detailInfo.gridName}}</div>
          </el-form-item>
          <el-form-item label="姓名:" :label-width="'100px'">
            <div class="input-width1">{{detailInfo.name}}</div>
          </el-form-item>
          
        </div>
        <div>
          <!-- <el-form-item label="性别" prop="remark" :label-width="'100px'">
            <el-input v-model="form.remark" autocomplete="off" class="input-width"></el-input>
          </el-form-item> -->
          <el-form-item label="手机号:" :label-width="'100px'">
           <div class="input-width1">{{detailInfo.mobile}}</div>
          </el-form-item>
          <el-form-item label="身份证:" :label-width="'100px'">
            <div class="input-width1">{{detailInfo.idCard}}</div>
          </el-form-item>
        </div>
        <div>
          
          <el-form-item label="积分类别:" :label-width="'100px'">
            <div class="input-width1">{{detailInfo.ruleName}}</div>
          </el-form-item>
          <el-form-item label="积分分值:" prop="status" :label-width="'100px'">
            <div class="input-width1">{{detailInfo.pointValue}}</div>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="标题:" prop="status" :label-width="'100px'">
            <div class="input-width1">{{detailInfo.title}}</div>
          </el-form-item>
          <el-form-item label="内容:" :label-width="'100px'">
            <div class="input-width1">{{detailInfo.statement}}</div>
          </el-form-item>
          
        </div>
        <el-form-item label="附件:" :label-width="'100px'">
            <div class="input-width1" style="display: flex;">
              <el-image
                v-for="item in detailInfo.annexList" 
                :key="item"
                style="width: 100px; height: 100px; margin-right: 10px;"
                :src="item" 
                :preview-src-list="detailInfo.annexList">
              </el-image>
            </div>
          </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handlerCancle">关闭</el-button>
        <!-- <el-button type="primary" @click="handleSubmit">确 定</el-button> -->
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      dialogVisible: false,
      dialogFormVisible: false,
      tableLoading: false,
      disabled: false,
      pageLoading: false,
      currentPage: 1,
      pageSize: 20,
      total: null,
      tableData: [],
      searchH: 0,
      addType: 'add',
      optionsG: [],
      zwLists: [
        {
          label: '通过',
          value: '2'
        },
        {
          label: '驳回',
          value: '1'
        }
      ],
      zwList: [
        {
          label: '待审核',
          value: '0'
        },
        {
          label: '已通过',
          value: '2'
        },
        {
          label: '已驳回',
          value: '1'
        }
      ],
      selectionList: [],
      tempList: [],
      partyOrgRange: [],
      timeRange: [],
      customerId: '',
      searchForm: {
        // partyOrgId: '',
        mobile: '',
        idCard: '',
        name: '',
        status: '',
        startTime: '',
        endTime: '',
        categoryCode: 'party_building'
      },
      form: {
        ids: [],
        status: '2',
        remark: ''
      },
      detailInfo: {},
      partyProps: {
        label: 'partyOrgName',
        value: 'id',
        checkStrictly: true 
      }
    }
  },
   computed: {
    ...mapGetters(['clientHeight', 'iframeHeight']),
    tableHeight() {
      const h = this.clientHeight - this.searchH - 280 + this.iframeHeigh
      const _h = this.clientHeight - 280 - this.searchH
      return this.$store.state.inIframe ? h : _h
    }
  },
  async created() {
    this.customerId = localStorage.getItem('customerId')
    this.getTableData()
    this.getGridList('query')
    this.pageLoading = true
    
  },
  
  mounted() {
    
    this.$nextTick(() => {
      this.searchH = this.$refs.searchCard.$el.offsetHeight
      console.log('tableHeight', this.tableHeight)
    })
    
  },
  methods: {
    handleSelection(val) {
      this.selectionList = [...val]
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      if (this.selectionList.length > 0 && val !== 1) {
        this.tempList = [...this.tempList, ...this.selectionList]
      }
      this.getTableData()
    },
    handlePartyChange(val) {
      console.log('val------pp', val)
      this.searchForm.partyOrgId = val[val.length - 1]
    },
    handleFilterSpan(row, item) {
      let _val = ''
      if (item.options && item.options.length > 0) {
        item.options.forEach((n) => {
          if (n.value === row[item.columnName]) _val = n.label
        })
      }
      return _val || row[item.columnName]
    },
    handleTimeChange (val) {
      if (val.length > 0) {
        this.searchForm.startTime = val[0]
        this.searchForm.endTime = val[1]
      } else {
        this.searchForm.startTime = ''
        this.searchForm.endTime = ''
      }

    },
    
    handleDelimg(item, index) {
      this.form.imageList.splice(index, 1)
    },
    handleSearch(val) {
      console.log('searchhh--', val)
      this.currentPage = 1
      this.getTableData()
    },
    resetForm(formName) {
      this.timeRange = []
      this.searchForm.startTime = ''
      this.searchForm.endTime = ''
      this.$refs[formName].resetFields()
      this.handleSearch()
    },
    handleAdd() {
      if (this.selectionList.length == 0) return this.$message.warning('请选择数据')
      this.dialogFormVisible = true
    },

    handlerCancle() {
      this.detailInfo = {}
      this.selectionList = []
      this.tempList = []
      this.disabled = false
      this.form = {
        ids: [],
        status: '2',
        remark: ''
      }
      this.dialogFormVisible = false
      this.dialogVisible = false
    },
    async handleLook(row) {
      this.disabled = true
      await this.getDetail(row.id)
    },
    async handleEdit(row, addType) {
      this.form.ids = [row.id]
      // await this.getDetail(row.id)
      this.dialogFormVisible = true
    },
    
    handleDel(row) {
      let params = {
        ids: [row.id]
      }
      console.log('row1', row)
      this.$http
        .post('/resi/partymember/icPartyMember/delete', [row.id])
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.$message.success('删除成功')
            this.getTableData()
          }
        })
        .catch((err) => {
          return this.$message.error('网络错误')
        })
    },
    async handleSubmit() {
      
      if (this.form.ids.length == 0) {
        if (this.tempList.length > 0) this.form.ids = Array.from(new Set(this.tempList.map(item => item.id)))
        else this.form.ids = Array.from(new Set(this.selectionList.map(item => item.id)))
        
      }
      this.$http
        .post('/point/apply/examine/submit', this.form)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.$message.success('操作成功')
            this.handlerCancle()
            this.getTableData()
          }
        })
        .catch((err) => {
          return this.$message.error('网络错误')
        })
    },
    async getDetail(id) {
      let params = {
        id,
      }
      this.$http
        .post(`/point/apply/detail/${id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.detailInfo = { ...res.data }
            this.dialogVisible = true
          }
        })
        .catch((err) => {
          console.log('row4', err)
          return this.$message.error('网络错误')
        })
    },
    async getGridList(type, agencyId) {
      const { user } = await this.$store.state
      console.log('user---ppp', user)
      // addorupdate  query
      await this.$http
        .get('/resi/partymember/icPartyOrg/getSearchTreelist', { params: {agencyId: agencyId || user.agencyId} })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            console.log('获取查询详情成功', res.data)
            
            this.optionsG = this.deepArrTOnull(res.data)
          }
        })
        .catch(() => {
          return this.$message.error('网络错误')
        })
    },
    deepArrTOnull(arr) {
      let a = []
      a = arr.map(item => {
        return {
          ...item,
          children: (item.children.length > 0 && this.deepArrTOnull(item.children) )|| null
        }
      })
      return a
    },
    async getTableData() {
      this.tableLoading = true
      const { user } = await this.$store.state
      let params = {
        ...this.searchForm,
        agencyId: user.agencyId,
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }
      await this.$http
        .post('/point/apply/listall', params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.tableData = res.data.list
            this.total = res.data.total
          }
        })
        .catch(() => {
          return this.$message.error('网络错误')
        })
      this.tableLoading = false
    }
    
  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/scss/buttonstyle.scss";

.resi-container .resi-card-table {
  ::v-deep .el-table th {
    color: #fff;
    background-color: rgba(33, 149, 254, 1);
    // border-right: 1px solid rgba(33, 149, 254, 1);
  }
}
.resi-table {
  ::v-deep .el-button--text {
    text-decoration: underline;
  }
  ::v-deep .btn-color-del {
    margin-left: 10px;
    color: rgba(213, 16, 16, 1);
  }
  ::v-deep .btn-color-edit {
    color: rgba(0, 167, 169, 1);
  }
}
.input-width {
  width: 200px;

}
.form-wr {
  .input-width {
    width: 260px;

  }
  .input-width-textarea {
    width: 500px;
  }
  .imsg-list {
    display: flex;
    align-items: center;
    .imgs-item {
      position: relative;
      margin-right: 10px;
      .el-icon-delete {
        position: absolute;
        top: 0;
        right: 0;
        font-size: 18px;
        color: red;
        z-index: 3;
        cursor: pointer;
      }
    }
  }
}
.div-content {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>

<style lang="scss" scoped>

.resi-row-btn {
  display: flex;
  margin-bottom: 13px;
  ::v-deep .el-button {
    // margin-left: 10px;
    border: 0;
  }
  ::v-deep .el-select {
    margin-right: 10px;
  }
  .el-button--success {
    background: rgba(34, 193, 195, 1);
  }
  .el-button {
    margin-left: 10px;
    border: 0;
  }
  .el-button--success {
    background: rgba(34, 193, 195, 1);
  }
  .el-button--warning {
    background: rgba(254, 179, 73, 1);
  }
  .el-button--danger {
    background: rgba(254, 98, 82, 1);
  }
}
.avatar-uploader {
  ::v-deep
  .el-upload {
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
}
  
  

.resi-btns {
  margin-top: 20px;
  text-align: center;
}
.input-width1 {
  width: 250px;
}
</style>
