<template>
  <section class="hbnList mar-t16">
    <ai-card hideTitle>
      <template #content>
        <ai-search-bar class="mar-t8">
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd"
                       @click="form.linksageDate=$moment().format('YYYY-MM-DD'),dialog=true">添加
            </el-button>
            <el-button icon="iconfont iconDelete" :disabled="!search.ids" @click="handleDelete(search.ids)">删除
            </el-button>
            <el-date-picker class="w-300" v-model="search.createTime" type="date" placeholder="日期" size="small"
                            clearable @change="page.current=1,getTableData()"/>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索党员/四邻信息" v-model="search.name" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict"
                  @selection-change="v=>search.ids=v.map(e=>e.id).toString()">
          <el-table-column slot="options" label="操作" fixed="right" align="center">
            <template slot-scope="{row}">
              <el-button type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-card>
    <ai-dialog title="四邻联动信息" :visible.sync="dialog" @closed="form={residentId:''},residents=[]" @onConfirm="submit"
               width="600px">
      <el-form :model="form" size="small" ref="DialogForm" :rules="rules" label-width="80px">
        <el-form-item label="党员" prop="partyId">
          <ai-select v-model="form.partyId" action="/app/appparty/listByFourParty" :instance="instance"
                     :prop="{label:'name'}" @change="form.residentId=null,getResidents()"/>
        </el-form-item>
        <el-form-item label="四邻信息" prop="residentId">
          <ai-select v-model="form.residentId" :selectList="residents" placeholder="请选择四邻信息"/>
        </el-form-item>
        <el-form-item label="日期" prop="linksageDate">
          <el-date-picker v-model="form.linksageDate" clearable placeholder="日期"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="form.description" rows="5" maxlength="200" show-word-limit/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "hbnList",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    colConfigs() {
      return [
        {type: 'selection'},
        {label: "党员", prop: "partyName"},
        {label: "四邻信息", prop: "residentName"},
        {label: "描述", prop: "description"},
        {label: "日期", prop: "linksageDate"},
        {slot: "options"}
      ]
    }
  },
  data() {
    return {
      search: {name: "", ids: ""},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      dialog: false,
      form: {residentId: "", linksageDate: ""},
      rules: {
        partyId: {required: true, message: "请选择党员"},
        residentId: {required: true, message: "请选择四邻信息"},
        linksageDate: {required: true, message: "请选择日期"},
        description: {required: true, message: "请输入描述"},
      },
      residents: []
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/app/apppartyfourlinkage/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    handleDelete(ids) {
      this.$confirm("是否要删除联动记录?").then(() => {
        this.instance.post("/app/apppartyfourlinkage/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
    submit() {
      this.$refs.DialogForm.validate(v => {
        if (v) {
          let loading = this.$loading({text: "提交中..."})
          this.instance.post("/app/apppartyfourlinkage/addOrUpdate", this.form).then(res => {
            loading.close()
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.dialog = false
              this.getTableData()
            }
          }).catch(() => loading.close())
        }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.getResidents().then(() => this.dialog = true)

    },
    getResidents() {
      let {partyId} = this.form
      return this.instance.post("/app/apppartyfourresident/listFourResident", null, {
        params: {partyId, size: 999}
      }).then(res => {
        if (res?.data) {
          this.residents = res.data.records.map(e => ({dictValue: e.id, dictName: e.name}))
          this.$forceUpdate()
        }
      })
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.hbnList {
  height: fit-content;

  .el-date-editor {
    width: 100%;
  }

  .w-300 {
    width: 300px;
  }
}
</style>
