<template>
  <ai-list class="list">
    <template slot="title">
      <ai-title title="网格员管理" :isShowBottomBorder="true"></ai-title>
    </template>
    <template slot="content">
      <ai-search-bar bottomBorder>
        <template slot="left">
          <el-date-picker
              v-model="searchObj.selectionDate"
              type="date"
              @change="(page.current = 1), getList()"
              value-format="yyyy-MM-dd"
              size="small"
              placeholder="选用时间">
          </el-date-picker>
        </template>
        <template slot="right">
          <el-input
              v-model="searchObj.name"
              size="small"
              placeholder="责任网格"
              @keyup.enter.native="(page.current = 1), getList()"
              clearable
              @clear="(searchObj.name = '', page.current = 1), getList()"
              suffix-icon="iconfont iconSearch"/>
        </template>
      </ai-search-bar>
      <ai-search-bar style="padding: 16px 0 0">
        <template slot="left">
          <el-button
              icon="iconfont iconAdd"
              type="primary"
              size="small"
              @click="add('')"
          >添加
          </el-button
          >
          <el-button
              icon="iconfont iconDelete"
              @click="deleteById(ids.join(','))"
              :disabled="!Boolean(ids.length)"
          >删除
          </el-button
          >
        </template>
      </ai-search-bar>
      <ai-table :tableData="tableData"
                :col-configs="colConfigs"
                :total="page.total"
                ref="aitableex"
                :current.sync="page.current"
                :size.sync="page.size"
                @selection-change="(v) => (ids = v.map((e) => e.id))"
                @getList="getList()">
        <el-table-column label="操作" slot="options" align="center" fixed="right" width="200">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="setTag(row)">标签</el-button>
              <el-button type="text" @click="toFamily(row.id)">责任家庭</el-button>
              <el-button type="text" @click="add(row.id)">查看</el-button>
              <el-button type="text" @click="deleteById(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog title="设置标签" :visible.sync="dialog" @closed="form={}" @onConfirm="submit"
                 width="400px">
        <el-form :model="form" size="small" ref="DialogForm" :rules="rules" label-width="0">
          <el-form-item prop="label">
            <el-input v-model="form.label" maxlength="10" show-word-limit clearable placeholder="请输入标签信息,最多10个字"/>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-list>
</template>

<script>
export default {
  name: "list",
  label: "网格员管理",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
  },
  data() {
    return {
      searchObj: {
        name: "",
        selectionDate: "",
      },
      page: {
        current: 1,
        size: 10,
        total: 0,
      },
      goAdd: false,
      tableData: [],
      fileList: [],
      ids: [],
      detail: {},
      dialog: false,
      form: {},
      rules: {}
    };
  },
  created() {
    this.dict.load("sex", "girdMemberType", "politicsStatus", "education");
    this.getList();
  },
  computed: {
    colConfigs() {
      return [
        {type: "selection"},
        {prop: "wxUserId", label: "网格员姓名", openType: 'userName'},
        {prop: "girdInfoListStr", align: "center", label: "责任网格"},
        {prop: "phone", align: "center", label: "联系电话"},
        {prop: "label", align: "center", label: "标签信息"},
        {prop: "selectionDate", align: "center", label: "选用时间"},
      ];
    },
  },
  methods: {
    getList() {
      this.instance
      .post("/app/appgirdmemberinfo/list", null, {
        params: {
          ...this.searchObj,
          ...this.page,
        },
      })
      .then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.records;
          this.page.total = res.data.total;
        }
      });
    },
    deleteById(ids) {
      ids &&
      this.$confirm("是否要删除该网格员？", {
        type: "error",
      })
      .then(() => {
        this.instance
        .post("/app/appgirdmemberinfo/delete", null, {
          params: {ids},
        })
        .then((res) => {
          if (res?.code == 0) {
            this.$message.success("删除成功!");
            this.getList();
          }
        });
      })
      .catch(() => {
      });
    },
    add(id) {
      this.$emit('change', {
        type: 'Add',
        params: {
          id: id || ''
        }
      })
    },
    toFamily(id) {
      this.$emit('change', {
        type: 'Family',
        params: {
          id
        }
      })
    },
    handleSelectionChange(val) {
      this.ids = [];
      val.map((e) => {
        this.ids.push(e.id);
      });
    },
    resetSearch() {
      Object.keys(this.searchObj).map((e) => {
        this.searchObj[e] = "";
      });
      this.getList();
    },
    setTag(row) {
      this.form = this.$copy(row)
      this.dialog = true
    },
    submit() {
      this.$refs.DialogForm.validate(v => {
        if (v) {
          let loading = this.$loading({text: "提交中...", background: 'rgba(0,0,0,.8)'})
          let {id, label} = this.form
          this.instance.post("/app/appgirdmemberinfo/updateGirdMemberLabelById", null, {
            params: {id, label}
          }).then(res => {
            loading.close()
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.dialog = false
              this.getList()
            }
          }).catch(() => loading.close())
        }
      })
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
