<template>
  <el-dialog
      class="select-area"
      :width="width"
      :title="title"
      :visible.sync="openOrClose"
      >
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入区域"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission"></crudOperation>
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="index" label="序号" width="120" align="center"/>
      <el-table-column
        v-for="item in selectAreaTableOptions"
        :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','selectArea:edit', 'selectArea:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <udOperation :data="scope.row" :permission="permission" /> -->
          <el-button type="primary" v-if="!buttonShow(scope.row)" @click="selectData(scope.row)">选择</el-button>
          <el-button type="warning" v-if="buttonShow(scope.row)"  @click="cancleData(scope.row)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />  
    <span slot="footer" class="dialog-footer">
      <el-button type="primary"  @click="saveViewData()" >保 存</el-button>
      <el-button @click="openOrClose = false">关 闭</el-button>
    </span>
  </div>
  </el-dialog>
</template>

<script>
import permission from '@/directive/permission'
import crudArea from '@/api/system/person'
import { getOptions } from '@/api/system/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'
import { selectAreaTableOptions } from '../tableConfig'
import { getToken } from '@/utils/auth'
import { initData } from '@/api/data'

const defaultForm = {
  id: null,
  code: '',
  name: '',
  areaId: '',
  departmentId: '',
  userIds:[],
}

export default {
  name: 'selectArea',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: '选择区域',
      url: 'positioning/area/getPageListJson',
      listOrder: [],
      crudMethod: { ...crudArea },
      optShow: { add: false, edit: false, del: false, reset: true },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
  },
  props: {
    title: {
      type: String,
      default: '',
    },
    width: {
      type: String,
      default:'60',
    },
    dataType: {
      type: String,
      default: '',
    },
    checkFlag: {
      type: String,
      default: '1',
    },
    dataM: {
      type: Array,
      default: () => [],
    },
    openOrClose: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      mapData: [],
      buildFloor: {},
      selectAreaTableOptions,
      viewLoading: false,
      permission: {
        add: ['admin', 'selectArea : add'],
        edit: ['admin', 'selectArea : edit'],
        del: ['admin', 'selectArea : del'],
      },
      rules: {
        name: [{ required: true, message: '请输入检查点名称', trigger: 'blur' }],
        labelCode: [{ required: true, message: '请选择巡检标签', trigger: 'blur' }],
        areaId: [{ required: true, message: '请选择所属区域', trigger: 'blur' }],
      },
      token: getToken() ?? '',
      dataList:[],
    }
  },
  created() {
    // this.getOptions()
  },
  methods: {
    // // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {
      },
    [CRUD.HOOK.beforeToAdd]() {
      },
    [CRUD.HOOK.beforeToEdit]() {
     
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) this.optionsObj[key] = res.data.content
        } catch (error) {
          console.log(error)
        }
      })
    },
    buttonShow(row){

      if (this.dataList) {
        for (let i = 0; i < this.dataList.length; i++) {
          const u = this.dataList[i];
          if (row.id == u.id) {
            return true
          }
        }
      }
      return false
    },
    selectData(row){
      // console.log(row)
      if(this.checkFlag == '1'){
        this.dataList.splice(0,1,row)
        this.$emit('getData',this.dataList)
        console.log(this.dataList)
        this.openOrClose = false
      }
      if(this.checkFlag == '2'){
        this.dataList.push(row)
      }
    },
    cancleData(row){
      if(this.checkFlag == '1'){
        this.dataList.pop()
      }
      if(this.checkFlag == '2'){
        if(this.dataList) {
          for (let i = 0; i < this.dataList.length; i++) {
            const u = this.dataList[i];
            if (u.id == row.id) {
              this.dataList.splice(i,1)
            }
          }
        }
      }
    },
    saveViewData(){
      if(this.checkFlag == '2'){
        this.$emit('getData',this.dataList)
        // console.log(this.dataList)
      }
      this.openOrClose = false
    },
  }
}
</script>

<style lang="scss" scoped>
</style>
