<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.disabler_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/person/disabler";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        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,
          addBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "基本信息表id",
              prop: "parentId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入基本信息表id",
                trigger: "blur"
              }]
            },
            {
              label:'姓名',
              prop:'name',
              search:true,
              editDisplay:false
            },
            {
              label: "居住情况",
              prop: "livingConditions",
              type: 'select',
              multiple: true,
              dataType:'string',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=living-conditions",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择居住情况",
                trigger: "blur"
              }]
            },
            {
              label: "监护人",
              prop: "guardian",
              hide: true,
              rules: [{
                required: true,
                message: "请输入监护人",
                trigger: "blur"
              }]
            },
            {
              label: "监护人电话",
              prop: "guardianPhone",
              rules: [{
                required: true,
                message: "请输入监护人电话",
                trigger: "blur"
              }]
            },
            {
              label: "收入来源",
              prop: "incomeSource",
              type: 'select',
              multiple: true,
              dataType:'string',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=income-source",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择收入来源",
                trigger: "blur"
              }]
            },
            {
              label: "医疗保障",
              prop: "healthCare",
              type: 'select',
              multiple: true,
              dataType:'string',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=health-care",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择医疗保障",
                trigger: "blur"
              }]
            },
            {
              label: "康复需求",
              prop: "convalescence",
              rules: [{
                required: false,
                message: "请输入康复需求",
                trigger: "blur"
              }]
            },
            {
              label: "辅助器具",
              prop: "assistiveDevices",
              rules: [{
                required: false,
                message: "请输入辅助器具",
                trigger: "blur"
              }]
            },
            {
              label: "生活爱好",
              prop: "hobby",
              hide: true,
              rules: [{
                required: false,
                message: "请输入生活爱好",
                trigger: "blur"
              }]
            },
            {
              label: "生活嗜好",
              prop: "addiction",
              hide: true,
              rules: [{
                required: false,
                message: "请输入生活嗜好",
                trigger: "blur"
              }]
            },
            {
              label: "行为能力",
              prop: "ability",
              type: 'select',
              dataType: 'number',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=ability",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择行为能力",
                trigger: "blur"
              }]
            },
            {
              label: "社交能力",
              prop: "socialSkills",
              type: 'select',
              dataType: 'number',
              hide: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=social-skills",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择社交能力",
                trigger: "blur"
              }]
            },
            {
              label: "就业意向",
              prop: "employmentNeeds",
              hide: true,
              rules: [{
                required: false,
                message: "请输入就业意向",
                trigger: "blur"
              }]
            },
            {
              label: "探访计划",
              prop: "visitPlan",
              rules: [{
                required: false,
                message: "请输入探访计划",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "remark",
              hide: true,
              rules: [{
                required: false,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
            {
              label: "社区名称",
              prop: "agencyName",
              rules: [{
                required: false,
                message: "请输入社区名称",
                trigger: "blur"
              }]
            },
            {
              label: "社区Id",
              prop: "agencyId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入社区Id",
                trigger: "blur"
              }]
            },
            {
              label: "小区名称",
              prop: "residentialName",
              rules: [{
                required: false,
                message: "请输入小区名称",
                trigger: "blur"
              }]
            },
            {
              label: "小区Id",
              prop: "residentialId",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入小区Id",
                trigger: "blur"
              }]
            },
            {
              label: "楼栋名称",
              prop: "buildingName",
              rules: [{
                required: false,
                message: "请输入楼栋名称",
                trigger: "blur"
              }]
            },
            {
              label: "楼栋Id",
              prop: "buildingId",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入楼栋Id",
                trigger: "blur"
              }]
            },
            {
              label: "单元名称",
              prop: "unitName",
              rules: [{
                required: false,
                message: "请输入单元名称",
                trigger: "blur"
              }]
            },
            {
              label: "单元Id",
              prop: "unitId",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入单元Id",
                trigger: "blur"
              }]
            },
            {
              label: "楼层名称",
              prop: "floorName",
              rules: [{
                required: false,
                message: "请输入楼层名称",
                trigger: "blur"
              }]
            },
            {
              label: "楼层Id",
              prop: "floorId",
              display: false,
              hide: true,
              rules: [{
                required: false,
                message: "请输入楼层Id",
                trigger: "blur"
              }]
            },
            {
              label: "房间名称",
              prop: "roomName",
              rules: [{
                required: false,
                message: "请输入房间名称",
                trigger: "blur"
              }]
            },
            {
              label: "房间Id",
              prop: "roomId",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入房间Id",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.disabler_add, false),
          viewBtn: this.vaildData(this.permission.disabler_view, false),
          delBtn: this.vaildData(this.permission.disabler_delete, false),
          editBtn: this.vaildData(this.permission.disabler_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      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>
