<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select
          v-model="query.buildId"
          filterable
          size="small"
          placeholder="请选择建筑名称"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <!-- <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入区域名称搜索"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        /> -->
        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="600px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="建筑名称" prop="positioningBuildId">
          <el-select
            v-model="form.positioningBuildId"
            filterable
            clearable
            size="small"
            placeholder="请选择建筑名称"
            class="filter-item"
            style="width: 370px"
            disabled
          >
            <el-option
              v-for="item in optionsObj.Build"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="统计区域名称" prop="name">
          <el-input v-model="form.name" style="width: 370px" />
        </el-form-item>
        <el-form-item label="统计区域排序" prop="sort">
          <el-input-number
            v-model.number="form.sort"
            :min="0"
            :max="999"
            controls-position="right"
            style="width: 370px"
          />
        </el-form-item>
        <el-form-item label="上级区域" prop="parentId">
          <treeselect
            v-model="form.parentId"
            filterable
            :defaultExpandLevel=3
            :options="crud.data"
            style="width: 370px"
            placeholder="选择上级区域（顶级区域该项不选）"
          />
        </el-form-item>
        <el-form-item label="绑定区域" prop="pArea">
          <el-select
            v-model="form.pArea"
            filterable
            clearable
            size="small"
            placeholder="请选择绑定区域"
            class="filter-item"
            style="width: 370px"
          >
            <el-option
              v-for="item in optionsObj.Area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="剩余区域" prop="remainingEnable">
          <el-switch
            v-model="form.remainingEnable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="剩余区域名称" prop="remainingName">
          <el-input v-model="form.remainingName" style="width: 370px" />
        </el-form-item>
        <el-form-item label="自定义区域" prop="customEnable">
          <el-switch
            v-model="form.customEnable"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            :inactive-value="false"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="自定义区域名称" prop="customName">
          <el-input v-model="form.customName" style="width: 370px" />
        </el-form-item>
        <el-form-item style="margin-bottom: 0" label="备注" prop="remark">
          <el-input v-model="form.remark" style="width: 370px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU"
          >确认</el-button
        >
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      v-loading="crud.loading"
      :data="crud.data"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'nohasChildren' }"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        v-for="item in countAreaTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      >
      </el-table-column>
      <el-table-column
        v-if="checkPer(['admin', 'countarea:edit', 'countarea:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            :disabled-dle="scope.row.id === 1"
            msg="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudCountArea from '@/api/positioning/countarea'
import { getOptions } from '@/api/positioning/public'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import DateRangePicker from '@/components/DateRangePicker'

// vue-treeselect
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

// 表头配置
import { countAreaTableOptions } from '../tableConfig'

const defaultForm = {
  id: '',
  name: '',
  positioningBuildId: '',
  // isTop: '1',
  subCount: 0,
  parentId: null,
  sort: 999,
  // enabled: 'true'
}
export default {
  name: 'conutArea',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
    DateRangePicker,
    Treeselect,
  },
  cruds() {
    return CRUD({
      title: '统计区域',
      url: 'positioning/countarea/getTree',
      crudMethod: { ...crudCountArea },
    })
  },
  data() {
    return {
      countAreaTableOptions,
      rules: {
        positioningBuildId: [{ required: true, message: '请选择建筑名称', trigger: 'blur' }],
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        sort: [
          {
            required: true,
            message: '请输入序号',
            trigger: 'blur',
            type: 'number',
          },
        ],
      },
      permission: {
        add: ['admin', 'countarea:add'],
        edit: ['admin', 'countarea:edit'],
        del: ['admin', 'countarea:delete'],
      },
      optionsObj: { Build: [], Area: [] },
    }
  },
  created() {
    this.getBuildOptions()
  },
  watch: {
    query: {
      handler(q) {
        //console.log(q)
        if (q.buildId) {
          this.getAreaOptions(q.buildId)
        }
      },
    },
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {
      this.form.positioningBuildId = this.query.buildId
      if (this.form.id) {
        delete this.form.id
      }
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.parentId == '0') {
        form.parentId = null
      }
    },
    // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {
      if (this.form.parentId !== null && this.form.parentId === this.form.id) {
        this.$message({
          message: '上级区域不能是自身',
          type: 'warning',
        })
        return false
      }
      return true
    },
    async getBuildOptions() {
      try {
        let res = await getOptions('Build')
        if (res.code === 20000) {
          this.optionsObj.Build = res.data.content
          this.query.buildId = this.optionsObj.Build[0].value
          this.crud.toQuery()
          await this.getAreaOptions(this.query.buildId)
        }
      } catch (error) {
        console.log(error)
      }
    },
    async getAreaOptions(buildId) {
      //console.log(buildId)
      try {
        let res = await getOptions('Area', { buildId: buildId })
        if (res.code === 20000) this.optionsObj.Area = res.data.content
      } catch (error) {
        console.log(error)
      }
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
