<template>
  <el-dialog
    append-to-body
    :title="title"
    top="20px"
    :visible.sync="dialogFormVisible"
    width="60%"
    @close="close"
  >
    <el-form>
      <el-form-item :label="__('权限组名称')">
        <el-input
          v-model="form.rights_group_name"
          disabled
          :placeholder="__('权限组编号')"
          :style="{ width: '60%' }"
        />
      </el-form-item>
      <el-divider />
      <el-scrollbar style="height: 600px">
        <el-checkbox-group v-model="checkedAll">
          <div
            v-for="(rights, index) in rightsBaseTree"
            :key="'cate' + rights.rights_id"
            :class="index === 0 ? 'top-line' : null"
          >
            <el-row class="table-layout" style="background: #f2f6fc">
              <el-checkbox
                :label="rights.rights_id"
                @change="handleCheckAllChange(rights)"
              >
                {{ rights.rights_name }}
              </el-checkbox>
            </el-row>
            <el-row class="table-layout">
              <el-col
                v-for="right in rights.children"
                :key="right.rights_id"
                :span="8"
                style="padding: 4px 0"
              >
                <el-checkbox
                  :label="right.rights_id"
                  @change="handleCheckAllChange(right)"
                >
                  {{ right.rights_name }}
                </el-checkbox>
              </el-col>
            </el-row>
          </div>
        </el-checkbox-group>
      </el-scrollbar>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doEdit } from '@/api/store/employee/rights/group'
  import { getRightsBaseTree } from '@/api/store/employee/rights/base'

  export default {
    name: 'StoreEmployeeRightsGroupEdit',
    data() {
      return {
        form: {},
        rules: {},
        title: this.__('编辑'),
        dialogFormVisible: false,
        indeterminate: {},
        checkedAll: [],
        checkList: {},
        rightsBaseTree: [],
        rights_group_rights_ids: [],
      }
    },
    created() {
      this.getRightsBaseTree()
    },
    methods: {
      __,
      showEdit(row) {
        this.form = Object.assign({}, row)
        if (row.rights_group_rights_ids) {
          const rights_group_rights_ids = row.rights_group_rights_ids.split(',')
          rights_group_rights_ids.map(Number)
          this.checkedAll = rights_group_rights_ids.map(Number)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.checkedAll = []
        this.dialogFormVisible = false
      },
      async save() {
        if (this.checkedAll) {
          let obj = {
            rights_group_id: this.form.rights_group_id,
            rights_group_rights_ids: this.checkedAll.toString(),
          }
          const { msg, status } = await doEdit(obj)
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          this.$emit('fetch-data')
          this.close()
        }
      },
      async getRightsBaseTree() {
        const { data } = await getRightsBaseTree()
        if (data) {
          this.rightsBaseTree = data
          data.forEach((item, index) => {
            this.checkList[index] = []
          })
        }
      },
      handleCheckAllChange(node) {
        const arr = this.checkedAll
        if (node.children.length > 0 && node.rights_parent_id === 8) {
          if (arr.includes(node.rights_id)) {
            this.checkAllChildren(node.children)
          } else {
            this.cancelAllChildren(node.children)
          }
        } else {
          if (
            arr.includes(node.rights_id) &&
            !arr.includes(node.rights_parent_id)
          ) {
            arr.push(node.rights_parent_id)
          }
        }
      },
      checkAllChildren(childrens) {
        const arr = this.checkedAll
        childrens.map((item) => {
          if (!arr.includes(item.rights_id)) arr.push(item.rights_id)
        })
      },
      cancelAllChildren(childrens) {
        const arr = this.checkedAll
        childrens.map((obj) => {
          arr.splice(
            arr.findIndex((item) => item === obj.rights_id),
            1
          )
        })
      },
    },
  }
</script>
