<template>
  <section class="AppResidentTags">
    <ai-list>
      <ai-title slot="title" title="标签管理" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="dialog=true">添加
            </el-button>
            <el-button icon="iconfont iconDelete" :disabled="!search.ids" @click="handleDelete(search.ids)">删除
            </el-button>
          </template>
          <template #right>
            <el-input size="small" placeholder="标签信息/添加人" v-model="search.labelName" 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" align="center" label="操作" fixed="right" width="160px">
            <el-row type="flex" justify="center" align="middle" slot-scope="{row}">
              <el-button type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            </el-row>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
    <ai-dialog :title="dialogTitle" :visible.sync="dialog" width="600px" @closed="form={}" @onConfirm="submit">
      <el-form ref="DialogForm" :model="form" :rules="rules" size="small" label-width="60px">
        <el-form-item required label="标签" prop="labelName">
          <el-input v-model.trim="form.labelName" placeholder="请输入" clearable maxlength="32" show-word-limit/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

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

export default {
  name: "AppResidentTags",
  label: "标签管理(居民档案使用)",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    rules() {
      return {
        labelName: [{required: true, message: "请输入标签"}],
      }
    },
    dialogTitle() {
      return `${this.form.id ? "编辑" : "添加"}标签`
    }
  },
  data() {
    return {
      page: {current: 1, size: 10, total: 0},
      dialog: false,
      tableData: [],
      search: {name: "", ids: ""},
      form: {},
      colConfigs: [
        {type: "selection"},
        {label: "标签信息", prop: "labelName"},
        {label: "创建时间", prop: "createTime", align: '120px'},
        {label: "创建人", prop: "createUserName", align: 'center'},
        {slot: "options"}
      ]
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/app/appresidentlabelinfo/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    //添加账号
    submit() {
      this.$refs.DialogForm.validate(v => {
        if (v) {
          let {form: {logoUrl: logo}} = this, logoUrl = logo?.[0]?.url
          this.instance.post("/app/appresidentlabelinfo/addOrUpdate", {...this.form, logoUrl}).then(res => {
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.dialog = false
              this.getTableData()
            }
          })
        }
      })
    },
    handleEdit(row) {
      this.dialog = true
      this.form = JSON.parse(JSON.stringify(row))
      this.form.logoUrl = [{url: row.logoUrl}]
    },
    handleDelete(ids) {
      this.$confirm("是否要删除该标签?").then(() => {
        this.instance.post("/app/appresidentlabelinfo/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.AppResidentTags {
  height: 100%;
}
</style>
