<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.agency_delete" @click="handleDelete">删
          除
        </el-button>
        <cy-excel-import :flag="4" file-name="社区信息导入模板" @success="onLoad(page)"></cy-excel-import>
      </template>
      <template slot="mapSelectForm" slot-scope="scope">
        <div>
          <input class="el-input--small el-input__inner" placeholder="选择地址" @click="mapVisible = true">
          <el-dialog :append-to-body="true" :close-on-click-modal="false" :modal-append-to-body="false" :visible.sync="mapVisible"
                     title="编辑地址" width="80%">
            <editPolygonMap v-if="mapVisible" :editForm.sync="scope.row" :visible.sync="mapVisible"></editPolygonMap>
          </el-dialog>
        </div>
      </template>

    </avue-crud>
  </basic-container>
</template>

<script>
  import {
    getList,
    getDetail,
    add,
    update,
    remove
  } from "@/api/community/agency.js";
  import {
    mapGetters
  } from "vuex";
  import {
    validatePhone
  } from "@/util/validator"
  import CyExcelImport from "../../components/excel/cy-excel-import"
  import editPolygonMap from "../../components/residential/editPolygonMap.vue"
  export default {
    components: {CyExcelImport,editPolygonMap},
    data() {
      return {
        mapVisible:false,
        areaData: [],
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 60,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [{
            label: "名称",
            prop: "name",
            search: true,
            width: 160,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入名称",
              trigger: "blur"
            }]
          },
            {
              label:'',
              prop:'mapSelect',

              formslot: true,
              hide: true,
            },
            {
              label: "经度",
              prop: "longitude",
              width: 120,
              hide: true,
              rules: [{
                required: false,
                message: "请输入经度",
                trigger: "blur"
              }]
            },
            {
              label: "纬度",
              prop: "latitude",
              width: 120,
              hide: true,
              rules: [{
                required: false,
                message: "请输入纬度",
                trigger: "blur"
              }]
            },
            {
              label: "所属省",
              prop: "regionProvince",
              type: "select",
              width: 150,
              dicUrl: "/api/blade-system/region/lazy-tree?parentCode=000000",
              cascaderItem:["regionCity"],
              props:{
                label:"title",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入省行政编码",
                trigger: "blur"
              }]
            },
            {
              label: "所属市",
              prop: "regionCity",
              type: "select",
              cascaderItem: ["regionArea"],
              dicUrl: `/api/blade-system/region/lazy-tree?parentCode={{key}}`,

              props:{
                label: "title",
                value: "id"
              },
              rules: [{
                required: false,
                message: "请输入市行政编码",
                trigger: "blur"
              }]
            },
            {
              label: "所属区",
              prop: "regionArea",
              type: "select",

              cascaderItem: ["regionStreet"],
              dicUrl:  `/api/blade-system/region/lazy-tree?parentCode={{key}}`,
              props: {
                label: "title",
                value: "id"
              },
              rules: [{
                required: false,
                message: "请输入区行政编码",
                trigger: "blur"
              }]
            },
            {
              label: "所属街道",
              prop: "regionStreet",
              type:"select",

              width: 150,
              dicUrl: `/api/blade-system/region/lazy-tree?parentCode={{key}}`,
              props: {
                label: "title",
                value: "id"
              },

              rules: [{
                required: true,
                message: "请输入街道id",
                trigger: "blur"
              }]
            },
            {
              label: "详细地址",
              search: true,
              prop: "address",
              // width: 300,
              overHidden: true,
              rules: [{
                required: false,
                message: "请输入详细地址",
                trigger: "blur"
              }]
            },
            {
              label: "负责人名称",
              prop: "personName",
              width: 150,
              labelWidth: 100,
              rules: [{
                required: false,
                message: "请输入负责人名称",
                trigger: "blur"
              }]
            },
            {
              label: "联系方式",
              prop: "personPhone",
              width: 120,
              rules: [{
                required: false,
                message: "请输入负责人联系方式",
                trigger: "blur"
              }, {
                validator: validatePhone,
                trigger: "blur"
              }],
            },
            {
              label: "背景图",
              prop: "pic",
              type: 'upload',
              hide: true,
              propsHttp: {
                res: "data",
                url: 'link'
              },
              listType: 'picture-img',
              span: 24,
              row: true,
              action: "/api/blade-resource/oss/endpoint/put-file",
              display: true,
              rules: [{
                required: false,
                message: "请输入封面图片",
                trigger: "blur"
              }]
            },
            {
              label: "小区数量",
              prop: "residentialCount",
              display: false,
              hide: true,
            },
            {
              label: "楼栋数量",
              prop: "buildingCount",
              hide: true,
              display: false,
            },
            {
              label: "单元数量",
              prop: "unitCount",
              hide: true,
              display: false,
            },
            {
              label: "楼层数量",
              prop: "floorCount",
              hide: true,
              display: false,
            },
            {
              label: "房间数量",
              prop: "roomCount",
              hide: true,
              display: false,
            },
            {
              label: "人员数量",
              prop: "personCount",
              hide: true,
              display: false,
            },

            {
              label: "备注",
              prop: "remark",
              hide: true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.agency_add, false),
          viewBtn: this.vaildData(this.permission.agency_view, false),
          delBtn: this.vaildData(this.permission.agency_delete, false),
          editBtn: this.vaildData(this.permission.agency_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
    },
    watch: {
      'form.mapSelect': {
        handler: function(value) {
          if (!value){
            return
          }else{
            this.form.longitude = this.form.mapSelect.longitude;
            this.form.latitude = this.form.mapSelect.latitude;
            this.form.address = this.form.mapSelect.formattedAddress;
          }
        },
      }
    },
    methods: {
      rowSave(row, done, loading) {
        row.residentialCount = 0;
        row.unitCount = 0;
        row.roomCount = 0;
        row.personCount = 0;
        row.carCount = 0;
        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>
