<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="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.branch_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <!--      <template slot="menu" slot-scope="scope">-->
      <!--        <el-button type="text" size="small" icon="el-icon-setting">-->
      <!--          <el-dropdown @command="tip">-->
      <!--            <span class="el-dropdown-link">-->
      <!--              操作<i class="el-icon-arrow-down el-icon&#45;&#45;right"> </i>-->
      <!--            </span>-->
      <!--            <el-dropdown-menu slot="dropdown">-->
      <!--              <el-dropdown-item divided @click.native="detail(scope.row)" icon="el-icon-office-building">详情-->
      <!--              </el-dropdown-item>-->
      <!--              <el-dropdown-item divided @click.native="edit(scope.row)" icon="el-icon-school">编辑</el-dropdown-item>-->
      <!--              <el-dropdown-item divided @click.native="manage(scope.row)" icon="el-icon-s-order">管理</el-dropdown-item>-->
      <!--            </el-dropdown-menu>-->
      <!--          </el-dropdown>-->
      <!--        </el-button>-->
      <!--      </template>-->
      <template slot="activities" slot-scope="{row}">
        <el-button size="mini" @click="show(row,1)">查看党内活动</el-button>
      </template>
      <template slot="news" slot-scope="{row}">
        <el-button size="mini" @click="show(row,2)">查看党内咨询</el-button>
      </template>
      <template slot="meetings" slot-scope="{row}">
        <el-button size="mini" @click="show(row,3)">查看三会一课</el-button>
      </template>
    </avue-crud>

    <el-drawer :visible.sync="showActivities" size="85%" direction="rtl" :modal-append-to-body="false" destroy-on-close append-to-body center >
      <activity ref="activity" :branch-id="branchId"></activity>
    </el-drawer>
    <el-drawer :visible.sync="showNews" size="85%" direction="rtl" :modal-append-to-body="false" destroy-on-close append-to-body center >
      <newslist ref="newslist" :branch-id="branchId"></newslist>
    </el-drawer>
    <el-drawer :visible.sync="showMeetings" size="85%" direction="rtl" :modal-append-to-body="false" destroy-on-close append-to-body center >
      <meetinglist ref="meetinglist" :branch-id="branchId"></meetinglist>
    </el-drawer>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/party/branch";
  import {mapGetters} from "vuex";
  import activity from "@/views/party/activity";
  import newslist from "@/views/party/news";
  import meetinglist from "@/views/party/meeting";

  export default {
    components: {
      activity,
      newslist,
      meetinglist
    },
    data() {
      return {
        branchId: '',
        showActivities: false,
        showNews: false,
        showMeetings: false,
        form: {
          resourceType: 1
        },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          dialogWidth: 600,
          height: 'auto',
          align: "center",
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          menuWidth:350,
          column: [
            {
              label: "党支部名称",
              prop: "branchName",
              labelWidth: 100,
              span: 24,
              row: true,
              search: true,
              searchLabelWidth: 100,
              rules: [{
                required: true,
                message: "请输入党支部名称",
                trigger: "blur"
              }]
            },
            {
              label: "党内活动",
              prop: "activities",
              slot: true,
              labelWidth: 100,
              display: false
            },
            {
              label: "党内资讯",
              prop: "news",
              slot: true,
              labelWidth: 100,
              display: false

            },
            {
              label: "三会一课",
              prop: "meetings",
              slot: true,
              labelWidth: 100,
              display: false

            },

            {
              label: "党支部介绍",
              prop: "branchIntroduce",
              type: "textarea",
              labelWidth: 100,
              row: true,
              hide: true,
              rules: [{
                required: false,
                message: "请输入党支部介绍",
                trigger: "blur"
              }]
            },

            {
              label: "关联类型",
              prop: "resourceType",
              hide: true,
              type: "radio",
              dicData: [
                {
                  label: "社区",
                  value: 1
                },
                {
                  label: "小区",
                  value: 2
                }
              ],
              span: 24,
              row: true,
              rules: [{
                required: false,
                message: "请输入负责人类型方式",
                trigger: "blur"
              }]
            },

            {
              label: "关联对象",
              prop: "resourceIds",
              type: "select",
              search: true,
              span:24,
              // dicUrl: "/api/cyzh-community/residential/list?agencyId={{key}}",
              dicUrl: "/api/cyzh-community/residential/list?size=99999",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请选择所属小区",
                trigger: "blur"
              }]
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.branch_add, false),
          viewBtn: this.vaildData(this.permission.branch_view, false),
          delBtn: this.vaildData(this.permission.branch_delete, false),
          editBtn: this.vaildData(this.permission.branch_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    mounted() {
      let tenantType = localStorage.getItem("tenantType");
      let resouceTypeColumn = this.findObject(this.option.column, "resourceType");

      if (tenantType == 1) {//园区
        this.resourceTypeDefault = 3;
        resouceTypeColumn.dicData = [
          {
            label: "园区",
            value: 3
          }, {
            label: "企业",
            value: 4
          }
        ];
        let resourceIds = this.findObject(this.option.column, "resourceIds");
        resourceIds.dicUrl = `/api/cyzh-community/agency/list?size=500&name={{key}}`
      } else if (tenantType == 2) {//社区
        this.resourceTypeDefault = 1;
        resouceTypeColumn.dicData = [
          {
            label: "社区",
            value: 1
          }, {
            label: "小区",
            value: 2
          }
        ];
      }
    },
    watch: {
      'form.resourceType': {
        handler(val) {
          let resourceIds = this.findObject(this.option.column, "resourceIds");
          if (val == 1) {
            resourceIds.dicUrl = `/api/cyzh-community/agency/list?size=500&name={{key}}`
          } else {
            resourceIds.dicUrl = "/api/cyzh-community/residential/list?size=99999";
          }
        }
      }
    },
    methods: {
      show(row, type) {
        this.branchId = row.id;
        this.showParty = true;
        switch (type) {
          case 1:
            this.showActivities = true;
            break;
          case 2:
            this.showNews = true;
            break;
          case 3:
            this.showMeetings = true;
            break;
          default:
            break;
        }
      },
      rowSave(row, done, loading) {
        row.resourceIds = row.resourceIds.toString()
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        row.residentialIds = row.residentialIds.toString();
        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>
