<template>
    <d2-container>
        <div class="d2-table-view-full" style="overflow: hidden">
            <div class="d2-table-view-header">
                <el-form label-width="100px" labelPosition="right" size="small" :inline="true" :model="filter" @keyup.enter.native="handleList" class="demo-form-inline">
                    <el-form-item label="设备名称">
                        <el-input style="width: 192px" v-model="filter.deviceNm" placeholder="设备名称" clearable/>
                    </el-form-item>
                    <el-form-item label="设备类型">
                        <custom-select :value="filter.deviceTyp" v-model="filter.deviceTyp" typeName="车场设备类型" style="width: 100%"></custom-select>
                    </el-form-item>
                    <!--<el-form-item label="设备类型" prop="deviceTyp">-->
                        <!--<el-select v-model="filter.deviceTyp" placeholder="请选择">-->
                            <!--<el-option-->
                                    <!--v-for="item in options"-->
                                    <!--:key="item.areaId"-->
                                    <!--:label="item.areaNm"-->
                                    <!--:value="item.areaId">-->
                            <!--</el-option>-->
                        <!--</el-select>-->
                    <!--</el-form-item>-->
                    <el-form-item>
                        <el-button   type="primary" @click="handleList('1')">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="d2-table-view-content">
                <div class="d2-table-view-handle">
                    <el-button @click="onCreateClick" size="small" icon="el-icon-plus">新增</el-button>
                    <el-button size="small" @click="onMutilDelClick">批量删除</el-button>
                </div>
                <d2-crud
                        title="区域设置"
                        ref="d2Crud"
                        :options="tableOptions"
                        :loading="loading"
                        :columns="columns"
                        :data="data"
                        :rowHandle="rowHandle"
                        @edit="handleRowEdit"
                        @delete="handleRowRemove"
                        selection-row
                        @selection-change="handleSelectionChange"
                >
                </d2-crud>
            </div>
            <div class="d2-table-view-footer">
                <el-pagination
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        :page-size="tableOptions.pageList[0]"
                        :page-sizes="tableOptions.pageList"
                        :current-page.sync="currentPage"
                        layout="total,sizes, prev, pager, next,jumper"
                        :total="pagination.total"
                ></el-pagination>
            </div>
        </div>
        <el-dialog
                :title="diaLogName"
                :visible.sync="operVisible"
                @close="clearForm"
                width="40%"
                :close-on-click-modal="false"
        >
            <el-form ref="form" :model="form" label-width="120px" labelPosition="right" :rules="rules">
                <el-row :gutter="15">
                    <el-col :span="24">
                        <el-form-item label="车场设备编号" prop="deviceId">
                            <el-input maxlength="20" disabled v-model.trim="form.deviceId" placeholder="系统自动生成"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="车场设备名称" prop="deviceNm">
                            <el-input maxlength="20" v-model.trim="form.deviceNm"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="设备型号" prop="deviceModel">
                            <!--<el-input maxlength="10" v-model.trim="form.deviceModel"/>-->
                            <custom-select :value="form.deviceModel" v-model="form.deviceModel" typeName="车场设备型号" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="通讯方式" prop="connectMode">
                            <!--<el-input maxlength="10" v-model.trim="form.connectMode"/>-->
                            <custom-select :value="form.connectMode" v-model="form.connectMode" typeName="车场通讯方式" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="设备IP地址" prop="deviceIp">
                            <el-input maxlength="15" v-model.trim="form.deviceIp"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="通讯端口" prop="devicePort">
                            <el-input maxlength="5" v-model.trim="form.devicePort"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="设备类型" prop="deviceTyp">
                            <!--<el-input maxlength="10" v-model.trim="form.deviceTyp"/>-->
                            <custom-select :value="form.deviceTyp" v-model="form.deviceTyp" typeName="车场设备类型" style="width: 100%"></custom-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="序列号" prop="mac">
                            <el-input v-model.trim="form.mac" maxlength="16"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                            <el-form-item label="所在区域" prop="areaId" >
                                <el-select v-model="form.areaId" placeholder="请选择" style="width: 100%">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.areaId"
                                            :label="item.areaNm"
                                            :value="item.areaId">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="canOpen">
                            <!--<el-input maxlength="2" v-model.trim="form.canOpen"/>-->
                            <el-checkbox v-model="form.canOpen" :checked="isChecked">是否开闸</el-checkbox>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                 <el-button :loading="staLoading" @click="operInstance(1)" v-if="isShow">继续添加</el-button>
        <zl-button :loading="staLoading" @click="operInstance(2)">完成</zl-button>
      </span>
        </el-dialog>
    </d2-container>
</template>
<script>
  import { tableOptions } from '@/assets/options'
  import {getAreaOptions} from '@/api/parking/costRules'
  import {getMangeEquipmentList,saveOrUpdateEquipment,getEquipmentById,onDeleteEquiment} from '@/api/parking/mangeEquipment'
  export default {
    name: 'system-staff',
    components: {

    },
    data () {
      let checkIP = (rule, value, callback) => {
        let reg = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/
        if (!reg.test(value)) {
          return callback(new Error('请输入正确的IP'))
        } else {
          callback ()
        }
      }
      let checkNumber = (rule, value, callback) => {
        if(!value&&value!==0){
          return callback(new Error('不能为空'))
        }
        let reg = /^[0-9]*$/
        if (!reg.test(value)) {
          return callback(new Error('请输入数字'))
        } else {
          callback ()
        }
      }
      return {
        options:[],
        isShow:true,
        isChecked:false,
        time:"",
        options:[],
        deviceId:[],
        deviceIds:"",
        isCreate:false,
        userId:"",
        checkList:[],
        // classVisible:false,
        staLoading: false,
        multipleSelection:[],
        func: {
          add: false,
          delete: false,
          update: false,
          select: false
        },
        falg: true,
        isExpend: true,
        tableOptions,
        loading: false,
        operVisible: false,
        diaLogName: '新增',
        lanuchs: [],
        form: {
          deviceId:'',
          deviceNm:"",
          deviceModel:"",
          connectMode:"",
          deviceIp:"",
          devicePort:"",
          deviceTyp:"",
          mac:"",
          areaId:"",
          canOpen:""
        },
        filter: {
          pageSize: 10,
          pageNum: 1,
          deviceNm:"",
          deviceTyp:""
        },
        pagination: {
          total: 0
        },
        currentPage: 1,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        columns: [
          {
            title: '设备编号',
            key: 'deviceId',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '设备名称',
            key: 'deviceNm',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '设备型号',
            key: 'deviceModel',
            minWidth: '120',
            formatter (row, column, cellValue, index) {
              if (cellValue === "10001") {
                return '摄像头'
              } else if (cellValue === "10002") {
                return '相机'
              }
            }
          },
          {
            title: '通讯方式',
            key: 'connectMode',
            minWidth: '90',
            formatter (row, column, cellValue, index) {
              if (cellValue === "10001") {
                return '网络'
              } else if (cellValue === "10002") {
                return '光纤'
              }
            }
          },
          {
            title: '设备IP地址',
            key: 'deviceIp',
            minWidth: '120',
            showOverflowTooltip: true
          },
          {
            title: '通讯端口',
            key: 'devicePort',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '设备类型',
            key: 'deviceTyp',
            minWidth: '90',
            formatter (row, column, cellValue, index) {
              if (cellValue === "10001") {
                return '相机'
              }
            }
          },
          {
            title: '区域名称',
            key: 'areaNm',
            minWidth: '90',
            showOverflowTooltip: true
          },
          {
            title: '是否开闸',
            key: 'canOpen',
            minWidth: '90',
            formatter (row, column, cellValue, index) {
              if (cellValue === 0) {
                return '否'
              } else if (cellValue === 1) {
                return '是'
              }
            }
          },
          {
            title: '序列号',
            key: 'mac',
            minWidth: '180',
            showOverflowTooltip: true
          },
        ],
        rowHandle: {
          columnHeader: '操作',
          minWidth: '180',
          align: 'center',
          custom: [
            {
              text: '编辑',
              type: 'primary',
              size: 'small',
              emit: 'edit'
            },
            {
              text: '删除',
              type: 'danger',
              size: 'small',
              emit: 'delete'
            }
          ]
        },
        data: [],
        rules: {
          deviceNm:[{required: true, message: '设备名称不能为空', trigger: 'blur'}],
          deviceIp:[{validator:checkIP,required: true, message: 'IP地址不能为空', trigger: 'blur'}],
          devicePort:[{validator: checkNumber,required: true, trigger: 'blur'}],
          deviceTyp:[{required: true, message: '请选择设备类型', trigger: 'blur'}],
          mac:[{validator: checkNumber,required: true, trigger: 'blur'}],
          areaId:[{required: true, message: '请选择区域', trigger: 'blur'}],
          deviceModel:[{required: true, message: '请选择设备型号', trigger: 'blur'}],
          connectMode:[{required: true, message: '请选择通讯方式', trigger: 'blur'}],
        }
      }
    },

    methods: {
      dateChange (val) {
        if(val == null){
          this.filter.bgTm = ''
          this.filter.endTm = ''
        }else{
          this.filter.bgTm = val[0]
          this.filter.endTm = val[1]
        }
      },
      onCreateClick(){
        this.operVisible=true
      },
      handleTreeNode () {
        this.handleList()
      },
      handleList (val) {
        if (val) {
          this.filter.pageNum = 1
          this.currentPage = 1
        }
        this.loading = true
        getMangeEquipmentList(this.filter)
          .then(res => {
            // console.log(res)
            this.loading = false
            this.data = res.page.list
            this.pagination.total = res.page.total
          })
          .catch(err => {
            this.loading = false
          })
      },
      handleSelectionChange(val) {
        this.isCreate=false
        this.multipleSelection = val;
      },
      handleCurrentChange (val) {
        this.filter.pageNum = val
        this.handleList()
      },
      handleSizeChange (val) {
        this.filter.pageNum = 1
        this.filter.pageSize = val
        this.handleList()
      },
      operInstance (val) {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if(this.form.canOpen){
              this.form.canOpen=1
            }else{
              this.form.canOpen=0
            }
            this.staLoading = true
            saveOrUpdateEquipment(this.form).then(res => {
              this.staLoading = false
              this.clearForm()
              if (val == 2) {
                this.operVisible = false
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.handleTreeNode()
              } else {
                this.clearForm()
              }

            }).catch(err => {
              this.staLoading = false
            })
          }
        })
      },
      handleRowEdit ({index, row}) {
        this.diaLogName = '修改'
        this.isShow=false
        getEquipmentById({deviceId:row.deviceId}).then(res => {
          this.form = res.parkDevice
if(res.parkDevice.canOpen==1){
  // this.form.canOpen==true
  this.$set(this.form,'canOpen',true)
}else{
  // this.form.canOpen==false
  this.$set(this.form,'canOpen',false)
}
        })
        this.operVisible = true
      },
      onMutilDelClick(){
        if(!this.multipleSelection.length){
          this.$message({
            message: '请选择记录',
            type: 'warning'
          })
        }else{
          this.$confirm(`确定要删除当前设备?`, '确认操作', {
            confirmButtonText: '确定',
            cancelButtonText: '放弃',
            type: 'warning'
          }).then(() => {
            this.deviceId=[]
            this.multipleSelection.forEach((item,index)=>{
              this.deviceId.push(item.deviceId)
            })
            this.deviceIds=this.deviceId.join(",")
            onDeleteEquiment({deviceId:this.deviceIds}).then(res=>{
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.handleTreeNode()
            })
          })
        }
      },
      handleRowRemove ({index, row}) {
        // console.log(row)
        this.$confirm(`确定要删除当前设备?`, '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '放弃',
          type: 'warning'
        }).then(() => {
          onDeleteEquiment({deviceId:row.deviceId}).then(res=>{
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.handleTreeNode()
          })
        })
      },
      clearForm () {
        if (this.$refs['form']!==undefined) {
          this.$refs['form'].resetFields()
        }
        this.form = {}
        this.checked=false
        this.diaLogName = '新增'
        this.isShow=true
      },
    },
    mounted () {
      this.clearForm()
      // getEquipStyleOptions().then(res=>{
      //   console.log(res.idNm)
      // })
      getAreaOptions().then((res)=>{
        this.options=res.list
      })
      this.filter.pageSize = this.tableOptions.pageList[0]
      this.tableOptions = this.$calcTableHeight(tableOptions, 102)
      this.handleTreeNode()
    }
  }
</script>
<style>
    /*.d2-table-view-full{*/
        /*height: 100%;*/
    /*}*/
    /*.timeBox{*/
        /*margin-top: 10px;*/
    /*}*/
    /*.timePay{*/
        /*margin-left: 8px;*/
    /*}*/
    /*.avatar-uploader .el-upload {*/
        /*border: 1px dashed #d9d9d9;*/
        /*border-radius: 6px;*/
        /*cursor: pointer;*/
        /*position: relative;*/
        /*overflow: hidden;*/
    /*}*/

    /*.avatar-uploader .el-upload:hover {*/
        /*border-color: #409eff;*/
    /*}*/

    /*.avatar-uploader-icon {*/
        /*font-size: 28px;*/
        /*color: #8c939d;*/
        /*width: 178px;*/
        /*height: 178px;*/
        /*line-height: 178px;*/
        /*text-align: center;*/
    /*}*/

    /*.avatar {*/
        /*width: 178px;*/
        /*height: 178px;*/
        /*display: block;*/
    /*}*/
    /*.timeLabel label{*/
        /*width: 110px !important;*/
    /*}*/
    /*.bankLabel label{*/
        /*font-size: 13px;*/
    /*}*/
    /*.el-form-item__label {*/
        /*text-align: left !important;*/
    /*}*/
    /*.d2-table-view{*/
        /*overflow: hidden;*/
    /*}*/
    /*.d2-table-view-content{*/
        /*height: 100%;*/
    /*}*/
    /*.el-table{*/
        /*height: 450px !important;*/
    /*}*/
</style>