<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.basicinfo_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menu" slot-scope="scope">
        <el-button icon="el-icon-setting" size="mini" type="text">
          <el-dropdown>
            <span class="el-dropdown-link" style="font-size: xx-small; color: #4babfe">设置标签<i class="el-icon-arrow-down el-icon--right"></i></span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item type="text" @click.native="tagsFormOpen('elder', scope.row)">高龄老人</el-dropdown-item>
              <el-dropdown-item type="text" @click.native="tagsFormOpen('keyPerson', scope.row)">重点人员</el-dropdown-item>
              <el-dropdown-item type="text" @click.native="tagsFormOpen('disabler', scope.row)">残障人士</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-button>
      </template>
      <template slot="tags" slot-scope="scope" style="display: flex">
        <el-button style="margin: auto" class="el-icon-collection-tag" v-for="item in scope.row.tags.split('，')" @click.native="tagsDetail(item,scope.row)" size="mini" type="text">
          {{item}}
        </el-button>
      </template>
    </avue-crud>
<!--    显示标签详情组件-->
    <el-dialog title="残障人士【标签详情】" :visible.sync="disablerTagsdetail" :modal-append-to-body="false">
      <DisablerTagsDetail v-if="disablerTagsdetail" :basic-data="basicData"></DisablerTagsDetail>
    </el-dialog>
    <el-dialog title="高龄老人【标签详情】" :visible.sync="elderTagsdetail" :modal-append-to-body="false">
      <ElderTagsDetail v-if="elderTagsdetail" :basic-data="basicData"></ElderTagsDetail>
    </el-dialog>
    <el-dialog title="重点人员【标签详情】" :visible.sync="keyPersonTagsdetail" :modal-append-to-body="false">
      <KeypeopleTagsDetail v-if="keyPersonTagsdetail" :basic-data="basicData"></KeypeopleTagsDetail>
    </el-dialog>
<!--    标签添加信息组件-->
    <el-dialog title="高龄老人【添加标签信息】" :visible.sync="elderTags" :modal-append-to-body="false">
      <ElderTagsForm v-if="elderTags" :basic-data="basicData" :close-fn="() => {elderTags = false}"></ElderTagsForm>
    </el-dialog>
    <el-dialog title="残障人士【添加标签信息】" :visible.sync="disablerTags" :modal-append-to-body="false">
      <DisablerTagsForm v-if="disablerTags" :basic-data="basicData" :close-fn="() => {disablerTags = false}"></DisablerTagsForm>
    </el-dialog>
    <el-dialog title="重点人员【添加标签信息】" :visible.sync="keyPersonTags" :modal-append-to-body="false">
      <KeyPersonTagsForm v-if="keyPersonTags" :basic-data="basicData" :close-fn="() => {keyPersonTags = false}"></KeyPersonTagsForm>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/person/basicinfo";
  import {mapGetters} from "vuex";
  import ElderTagsForm from "@/components/person-tags/elderTags-form";
  import DisablerTagsForm from "@/components/person-tags/disablerTags-form";
  import KeyPersonTagsForm from "@/components/person-tags/keyPersonTags-form";
  import ElderTagsDetail from "@/components/person-tags/elderTags-detail";
  import DisablerTagsDetail from "@/components/person-tags/disablerTags-detail";
  import KeypeopleTagsDetail from "@/components/person-tags/keyPersonTags-detail";

  export default {
    components: {ElderTagsForm, DisablerTagsForm,KeyPersonTagsForm,DisablerTagsDetail,ElderTagsDetail,KeypeopleTagsDetail},
    data() {
      return {
        elderTags: false,
        disablerTags: false,
        keyPersonTags: false,
        elderTagsdetail:false,
        disablerTagsdetail:false,
        keyPersonTagsdetail:false,
        basicData: {},
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          labelWidth: 120,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "姓名",
              prop: "name",
              search:true,
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }]
            },
            {
              label: "身份证号码",
              prop: "idCard",
              hide: true,
              rules: [{
                required: false,
                message: "请输入身份证号码",
                trigger: "blur"
              }]
            },
            {
              label: "性别",
              prop: "sex",
              type: 'select',
              dataType:"number",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=sex",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择性别",
                trigger: "blur"
              }]
            },
            {
              label: "生日",
              prop: "birthday",
              type:'date',
              valueFormat:"yyyy-MM-dd",
              hide: true,
              rules: [{
                required: true,
                message: "请输入生日",
                trigger: "blur"
              }]
            },
            {
              label: "民族",
              prop: "nation",
              rules: [{
                required: true,
                message: "请输入民族",
                trigger: "blur"
              }]
            },
            {
              label: "籍贯",
              prop: "nativePlace",
              rules: [{
                required: false,
                message: "请输入籍贯",
                trigger: "blur"
              }]
            },
            {
              label: "国籍",
              prop: "nationality",
              rules: [{
                required: false,
                message: "请输入国籍",
                trigger: "blur"
              }]
            },
            {
              label: "户口所在地",
              prop: "registeredResidence",
              rules: [{
                required: false,
                message: "请输入户口所在地",
                trigger: "blur"
              }]
            },
            {
              label: "政治面貌",
              prop: "political",
              type: 'select',
              dataType:"string",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=political",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: false,
                message: "请输入政治面貌",
                trigger: "blur"
              }]
            },
            {
              label: "婚姻状况",
              prop: "maritalStatus",
              type: 'select',
              dataType:"string",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=marital-status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: false,
                message: "请选择婚姻状况",
                trigger: "blur"
              }]
            },
            {
              label: "教育水平",
              prop: "educationalLevel",
              type: 'select',
              dataType:"string",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=educational-level",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: false,
                message: "请选择教育水平",
                trigger: "blur"
              }]
            },
            {
              label: "照片",
              prop: "faceImg",
              type: 'upload',
              listType: 'picture-img',
              span: 8,
              hide: true,
              propsHttp: {
                url: "data"
              },
              action: "/api/upload/putfile",
              tip: "只能上传jpg/png图片，且不超过500kb",
              rules: [{
                required: false,
                message: "请输入入境证明url",
                trigger: "blur"
              }]
            },
            {
              label: "工作单位",
              prop: "workplace",
              rules: [{
                required: false,
                message: "请输入工作单位",
                trigger: "blur"
              }]
            },
            {
              label: "小程序openId",
              prop: "openId",
              hide: true,
              display: false,
              rules: [{
                required: false,
                message: "请输入小程序openId",
                trigger: "blur"
              }]
            },
            {
              label: "邮箱",
              prop: "email",
              rules: [{
                required: false,
                message: "请输入邮箱",
                trigger: "blur"
              }]
            },
            {
              label: "人员标签",
              prop: "tags",
              type:'select',
              width:150,
              display:false,
              multiple:true,
              slot:true,
              dataType:'string',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=person-tag",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: false,
                message: "请输入标签",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "remark",
              rules: [{
                required: false,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.basicinfo_add, false),
          viewBtn: this.vaildData(this.permission.basicinfo_view, false),
          delBtn: this.vaildData(this.permission.basicinfo_delete, false),
          editBtn: this.vaildData(this.permission.basicinfo_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      tagsFormOpen(type,row) {
        this.basicData = row;
        switch (type) {
          case 'elder':
            this.elderTags = true;
            break;
          case 'disabler':
            this.disablerTags = true;
            break;
          case 'keyPerson':
            this.keyPersonTags = true;
            break;
        }
      },
      tagsDetail(type,row){
        this.basicData = row;
        switch (type) {
          case '高龄老人':
            this.elderTagsdetail = true;
            break;
          case '残障人士':
            this.disablerTagsdetail = true;
            break;
          case '重点人员':
            this.keyPersonTagsdetail = true;
            break;
        }
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
