<template>
  <el-dialog
      class="select-view"
      :width="width"
      :title="title"
      :visible.sync="openOrClose"
  >
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <div v-if="dataType == 'person'">
          <el-input
            v-model="query.name"
            clearable
            size="small"
            placeholder="输入人员姓名"
            style="width: 160px"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
          <el-select
            v-model="query.departmentName"
            filterable
            clearable
            size="small"
            placeholder="请输入部门"
            class="filter-item"
            style="width: 160px"
            @change="crud.toQuery"
          >
            <el-option
              v-for="item in optionsObj.Department"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select
            v-model="query.positionName"
            filterable
            clearable
            size="small"
            placeholder="请输入岗位"
            class="filter-item"
            style="width: 160px"
            @change="crud.toQuery"
          >
            <el-option
              v-for="item in optionsObj.Position"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <div v-if="dataType !== 'person'">
          <el-input
            v-model="query.name"
            clearable
            size="small"
            placeholder="输入名称"
            style="width: 160px"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
        </div>
         <!-- <el-select
            v-model="query.positionName"
            filterable
            clearable
            size="small"
            placeholder="选择情况——未完成"
            class="filter-item"
            style="width: 160px"
            @change="crud.toQuery"
          >
            <el-option
              v-for="item in optionsObj.Position"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select> -->
        <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="55" align="center"/>
      <el-table-column
        v-for="item in tbOption"
        :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'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
            <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 crudCheckPoint from '@/api/patrolManage/patrolLabel'
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 {  selectUserTableOptions,
          selectDepartmentTableOptions,
          selectAreaTableOptions,
          selectPatrolLableTableOptions,
          selectPatrolObjectTableOptions,
          selectCheckPointTableOptions,
          selectDangerOriginTableOptions } from '../tableConfig'
import { getToken } from '@/utils/auth'
import { initData } from '@/api/data'
// var resUrl

const defaultForm = {
  id: null,
  code: '',
  name: '',
  areaId: '',
  departmentId: '',
  dataList:[],
  dataL:{},
  tbOption:null,
  resUrl:'',
}

export default {
  name: 'selectPatrolLable',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: '选择巡检标签',
      url: 'patrol/label/getList',
      listOrder: [],
      crudMethod: { ...crudCheckPoint },
      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',
    },
    openOrClose: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      mapData: [],
      buildFloor: {},
      selectUserTableOptions,
      selectDepartmentTableOptions,
      selectAreaTableOptions,
      selectPatrolLableTableOptions,
      selectPatrolObjectTableOptions,
      selectCheckPointTableOptions,
      selectDangerOriginTableOptions,
      viewLoading: false,
      permission: {
        add: ['admin', 'selectUser : add'],
        edit: ['admin', 'selectUser : edit'],
        del: ['admin', 'selectUser : del'],
      },
      rules: {
        name: [{ required: true, message: '请输入检查点名称', trigger: 'blur' }],
        labelCode: [{ required: true, message: '请选择巡检标签', trigger: 'blur' }],
        areaId: [{ required: true, message: '请选择所属区域', trigger: 'blur' }],
      },
      optionsObj: {
        Department: [],
        Position: [],
      },
      dataTypeTableOptions:[
        {selcetType:"person", tableOptions:selectUserTableOptions, url:'system/person/getPageListJson'},
        {selcetType:"area", tableOptions:selectAreaTableOptions, url:'system/area/getPageListJson'},
        {selcetType:"dept", tableOptions:selectDepartmentTableOptions, url:'system/dept/getPageListJson'},
        {selcetType:"patrolLabel", tableOptions:selectPatrolLableTableOptions, url:'patrol/label/getList'},
        {selcetType:"patrolObject", tableOptions:selectPatrolObjectTableOptions, url:'patrol/object/getList'},
        {selcetType:"checkPoint", tableOptions:selectCheckPointTableOptions, url:'patrol/checkPoint/getList'},
        {selcetType:"dangerOrigin", tableOptions:selectDangerOriginTableOptions, url:'system/dept/getPageListJson'},
      ],
      token: getToken() ?? '',
      dataList:[],
      tbOption:{},
      resUrl:'',
      tableData:[],
    }
  },
  created() {
    this.getOptions()
    this.selectOption()
    // setTimeout(()=>{
    //   this.getTableData()
    // },200)
  },
  methods: {
    // // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {

    },
    [CRUD.HOOK.beforeToAdd]() {

    },
    [CRUD.HOOK.beforeToEdit]() {
     
    },
    //获取表格数据
    async getTableData(){
      try {
        const res = await initData(this.resUrl)
        this.tableData = res.data.content
        console.log(this.tableData.length);
      } catch (err) {
        console.log(err)
      }
    },
    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)
        }
      })
    },
    selectOption(){
      for (let i = 0; i < this.dataTypeTableOptions.length; i++) {
        if(this.dataType == this.dataTypeTableOptions[i].selcetType){
          this.tbOption = this.dataTypeTableOptions[i].tableOptions
          this.resUrl = this.dataTypeTableOptions[i].url
          console.log(this.resUrl)
          // console.log(this.tbOption)
        }
      }
    },
    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){
      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
    },
    closeDialog(){
      this.$emit("closeDia")
    },
  }
}
</script>

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