<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.id ? `编辑（${curObj.estate_name}）模块主题` : '新增模块主题'"
      :fullscreen="false"
      width="700px"
      custom-class="xl-dialog"
      center
    >
      <div class="scoped-ed">
        <div class="se-op" v-for="(ed, index) in edList" :key="index">
          <span class="t">{{ed.mtName}}</span>
          <span class="s" @click="delHandle(ed)">删除</span>
        </div>
      </div>
      <!-- <table-list
        :list-loading="listLoading"
        :data="edList"
        :columns="listConfig"
        :currentPage="currentPage"
        :pageSize="-1"
      /> -->
      <base-form ref="ruleForm" class="lib-edit" :data="formData" :is-inline="false" label-width="110px">
        <div slot="footer" style="padding-top: 20px;">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">新增</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import baseTable from '_m/baseTable.js'
export default {
  components: { },
  props: {
    isShow: Boolean,
    curObj: Object
  },
  mixins: [...mixins, baseTable],
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      noCreated: true,
      edList: [],
      unModuleTypeArr: [],
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getEdList()
      }
    },
  },
  mounted() {
    this.listConfig = {
      rows: [
        { label: '已有模块主题', prop: 'mtName' },
        { label: '操作', width: 400, type: 'handle2', operations:
          [
            { label: '删除', func: this.delHandle, btnType: 'danger' },
          ]
        }
      ]
    }
  },
  methods: {
    getEdList () {
      this.listLoading = true
      this.$api.house.admestatemoduleid({estate_id: this.curObj.id}).then(res => {
        const mtObj = arrToObj(this.$dictData.module_type)
        this.edList = res.map((item, index) => {
          return { id: item.id, mtName: mtObj[item.module_type]}
        })
        let unMTArr = []
        let allMTArr = this.$dictData.module_type || []
        allMTArr.forEach(one => {
          let inFlag = true
          res.forEach((item, index) => {
            if (one.val === item.module_type) {
              inFlag = false
            }
          })
          if (inFlag) unMTArr.push(one)
        })
        this.unModuleTypeArr = [...unMTArr]
        this.getDef()
        this.listLoading = false
      })
    },
    delHandle(row) {
      this.$msg(`您确定要删除该模块主题吗？`, 'confirm', () => {
        this.$api.house.admestatemoduledel({
          id: row.id,
        }).then(data => {
          this.$msgs(`已删除！`)
          this.getEdList()
        })
      }, null, true)
    },
    getDef() {
      const params = {}
      this.formData = [
        { label: '新增模块主题', key: 'module_type', rules: 1, type: 'select', options: this.unModuleTypeArr },
        { label: '排序', key: 'sort' },
      ]
      this.setDefaultValue(params)
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            newForm.estate_id = this.curObj.id
            let apiStr = 'admestatemoduleadd'
            this.$api.house[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.getEdList()
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
.scoped-ed {
  .se-op {
    display: inline-block;
    vertical-align: middle;
    border: 1px solid #dcdcdc;
    margin-bottom: 10px;
    margin-right: 10px;
    .t {
      display: inline-block;
      vertical-align: middle;
      height: 30px;
      line-height: 30px;
      width: 150px;
      padding: 0 20px;
      font-weight: bold;
    }
    .s {
      display: inline-block;
      vertical-align: middle;
      color: #fff;
      background: #ff875c;
      height: 30px;
      line-height: 30px;
      padding: 0 10px;
      cursor: pointer;
    }
  }
}
</style>
