<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.parkinglot_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="mapSelectForm" slot-scope="scope">
        <div>
          <!--          <input class="el-input&#45;&#45;small el-input__inner" placeholder="选择地址" @click="mapVisible = true">-->
          <el-button @click="mapVisible = true"> 选择地址</el-button>
          <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" :region.sync="scope.row.parkAdd" :latitude.sync="scope.row.parkLatitude" :longitude.sync="scope.row.parkLongitude" :visible.sync="mapVisible"></editPolygonMap>
          </el-dialog>
        </div>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/estate/parkinglot";
  import {mapGetters} from "vuex";
  import editPolygonMap from "@/components/residential/editPolygonMap";
  import {validatePhone} from "../../util/validator";
  import {getList as getAgencyList} from "@/api/community/agency";
  import {getList as getResidentialList} from "@/api/community/residential";

  export default {
    components: {
      editPolygonMap
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        mapVisible:false,
        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: "索引ID",
              prop: "indexId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入索引ID",
                trigger: "blur"
              }]
            },
            {
              label: "停车场名称",
              prop: "parkName",
              search: true,
              searchLabelWidth: 100,
              rules: [{
                required: true,
                message: "请输入停车场名称",
                trigger: "blur"
              }]
            },
            {
              label: "车场编号",
              prop: "parkKey",
              rules: [{
                required: true,
                message: "请输入车场唯一编号",
                trigger: "blur"
              }],
              hide: true
            },
            {
              label: "所属社区",
              prop: "agencyId",
              search: true,
              searchFilterable: true,
              filterable: true,
              cascaderItem: ["residentialId"],
              type: "select",
              // remote: true,
              dicUrl: "/api/cyzh-community/agency/list?size=500&name={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属社区",
                trigger: "change"
              }],
              hide: true,
              viewDisplay: false,
            },
            {
              label: "所属社区",
              prop: "agencyName",
              editDisplay: false,
              addDisplay: false,
            },
            {
              label: "所属小区",
              prop: "residentialName",
              editDisplay: false,
              addDisplay: false
            },
            {
              label: "所属小区",
              prop: "residentialId",
              type: "select",
              dicUrl: "/api/cyzh-community/residential/list?size=500&agencyId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              filter: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属小区",
                trigger: "blur"
              }],
              hide: true,
              viewDisplay: false
            },
            {
              label:'',
              prop:'mapSelect',
              // row: true,
              // span: 20,
              formslot: true,
              hide: true,
            },
            {
              label: "车场纬度",
              prop: "parkLatitude",
              hide: true,
              rules: [{
                required: false,
                message: "请输入车场纬度",
                trigger: "blur"
              }]
            },
            {
              label: "车场经度",
              prop: "parkLongitude",
              hide: true,
              rules: [{
                required: false,
                message: "请输入车场经度",
                trigger: "blur"
              }]
            },
            {
              label: "车场地址",
              prop: "parkAdd",
              overHidden: true,
              rules: [{
                required: false,
                message: "请输入车场地址",
                trigger: "blur"
              }]
            },
            {
              label: "车场联系方式",
              prop: "parkTel",
              hide: true,
              rules: [{
                required: false,
                message: "请输入车场联系方式",
                trigger: "blur"
              },{
                validator: validatePhone,
                trigger: "blur"
              }]
            },
            {
              label: "车场联系人",
              prop: "parkLinkman",
              hide: true,
              rules: [{
                required: false,
                message: "请输入车场联系人",
                trigger: "blur"
              }]
            },
            // {
            //   label: "免费分钟数	",
            //   prop: "parkFreeTime",
            //   hide: true,
            //   rules: [{
            //     required: false,
            //     message: "请输入免费分钟数	",
            //     trigger: "blur"
            //   }]
            // },
            // {
            //   label: "免费超时分钟数	",
            //   prop: "parkFreeTimeout",
            //   hide: true,
            //   rules: [{
            //     required: false,
            //     message: "请输入免费超时分钟数	",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "车场收费说明	",
              prop: "chargesDesc",
              hide: true,
              rules: [{
                required: false,
                message: "请输入车场收费说明	",
                trigger: "blur"
              }]
            },
            // {
            //   label: "停车场是否启用预定功能",
            //   prop: "reserveStatus",
            //   hide: true,
            //   rules: [{
            //     required: false,
            //     message: "请输入停车场是否启用预定功能",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "创建时间	",
              prop: "createTime",
              type: 'date',
              valueFormat: 'yyyy-MM-dd',
              rules: [{
                required: false,
                message: "创建时间	",
                trigger: "blur"
              }],
              disabled: true,
              addDisplay: false,
              editDisplay: false,
            },
            // {
            //   label: "注册时间	",
            //   prop: "regTime",
            //   type: 'date',
            //   valueFormat: 'yyyy-MM-dd',
            //   rules: [{
            //     required: false,
            //     message: "请输入注册时间	",
            //     trigger: "blur"
            //   }]
            // },
            // {
            //   label: "停车场有效期止日	",
            //   prop: "validTime",
            //   hide: true,
            //   type: 'date',
            //   valueFormat: 'yyyy-MM-dd',
            //   rules: [{
            //     required: false,
            //     message: "请输入停车场有效期止日	",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "车位总数",
              prop: "spaceTotal",
              hide: true,
              rules: [{
                required: false,
                message: "请输入车位总数",
                trigger: "blur"
              }]
            },
            {
              label: "车牌地区简称	",
              prop: "cityShortName",
              hide: true,
              display: false,
              rules: [{
                required: false,
                message: "请输入车牌地区简称	",
                trigger: "blur"
              }]
            },
            {
              label: "空余车位",
              prop: "remainSpace",
              hide: true,
              rules: [{
                required: false,
                message: "请输入空余车位",
                trigger: "blur"
              }],
              disabled: true,
              editDisplay: false,
              addDisplay: false,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.parkinglot_add, false),
          viewBtn: this.vaildData(this.permission.parkinglot_view, false),
          delBtn: this.vaildData(this.permission.parkinglot_delete, false),
          editBtn: this.vaildData(this.permission.parkinglot_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    created() {
      const tenantType = localStorage.getItem("tenantType");
      if (tenantType == 0){
        let agencyIdProp = this.findObject(this.option.column,"agencyId");
        let agencyNameProp = this.findObject(this.option.column,"agencyName");
        let residentialIdProp = this.findObject(this.option.column,"residentialId");
        let residentialNameProp = this.findObject(this.option.column,"residentialName");
        agencyIdProp.label = "所属社区"
        agencyIdProp.rules[0].message = "请选择所属社区"
        agencyNameProp.label = "所属社区"
        residentialIdProp.label = "所属小区"
        residentialIdProp.rules[0].message = "请选择所属小区"
        residentialNameProp.label = "所属小区"
      }else if (tenantType == 1){
        let agencyIdProp = this.findObject(this.option.column,"agencyId");
        let agencyNameProp = this.findObject(this.option.column,"agencyName");
        let residentialIdProp = this.findObject(this.option.column,"residentialId");
        let residentialNameProp = this.findObject(this.option.column,"residentialName");
        agencyIdProp.label = "所属园区"
        agencyIdProp.rules[0].message = "请选择所属园区"
        agencyNameProp.label = "所属园区"
        residentialIdProp.label = "所属区域"
        residentialIdProp.rules[0].message = "请选择所属区域"
        residentialNameProp.label = "所属区域"
      }
    },
    watch: {
      "form.agencyId"() {
        getAgencyList(1, 10, {id: this.form.agencyId}).then(res => {
          this.form.agencyName = res.data.data.records[0].name;
        })
      },
      "form.residentialId"() {
        getResidentialList(1, 10, {id: this.form.residentialId}).then(res => {
          this.form.residentialName = res.data.data.records[0].name;
        })
      }
    },
    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>
