<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人姓名" prop="deviceUser" label-width="85px">
        <el-input
          v-model="queryParams.deviceUser"
          placeholder="请输入负责人姓名"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createUser" label-width="85px">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建人姓名"
          clearable v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable style="width: 240px">
          <el-option v-for="item in deviceOptions" :key="item.deviceType" :label="item.deviceName"
                     :value="item.deviceType"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          :editable="false"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceInformation" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备类型" align="center" prop="deviceTypeName" />
      <el-table-column label="设备型号" align="center" prop="deviceModel" />
      <el-table-column label="设备负责人" align="center" prop="deviceUser" />
      <el-table-column label="负责人联系方式" width="120" align="center">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.devicePhone">{{ hideSensitiveInfo(scope.row.devicePhone) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createUser" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前绑定工程" align="center" prop="proName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body :close-on-click-modal="false">
      <el-form :label-position="labelPosition" ref="form" :model="form" :rules="rules">
        <el-row type="flex" class="row-bg" justify="space-around">
          <el-col :span="11">
            <el-form-item label="设备名称" prop="deviceName">
              <el-input v-model="form.deviceName" placeholder="请输入设备名称" maxlength="30"  v-no-whitespace/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="设备类型" prop="deviceType">
              <el-select v-model="form.deviceType" placeholder="请选择设备类型" @change="changeTypeName"
                         :placeholder="isEditing ? '请选择设备类型' : ''" :disabled="!isEditing"
                         :style="{width: '100%'}"
              >
<!--                <el-option v-for="item in dict.type.sys_device_type" :key="item.value" :label="item.label"-->
<!--                           :value="item.value"-->
<!--                ></el-option>-->
                <el-option v-for="item in deviceOptions" :key="item.deviceType" :label="item.deviceName"
                           :value="item.deviceType"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" class="row-bg" justify="space-around">
          <el-col :span="11">
            <el-form-item label="设备型号" prop="deviceModel">
              <el-input v-model="form.deviceModel" placeholder="请输入设备型号" v-no-whitespace
                        :placeholder="isEditing ? '请输入设备型号' : ''" :disabled="!isEditing"
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="设备负责人" prop="deviceUser" label-width="150">
              <el-input v-model="form.deviceUser" placeholder="请输入设备负责人" maxlength="20" v-no-whitespace />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" class="row-bg" justify="space-around" v-if="['1', '2', '3', '5', '6'].includes(form.deviceType)">
          <el-col :span="11">
            <el-form-item label="设备puid" prop="puid">
              <el-input v-model="form.puid" placeholder="请输入设备puid" maxlength="20"
                        :placeholder="'请输入设备puid'" v-no-whitespace
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="设备ballIndex" prop="ballIndex" label-width="150">
              <el-select v-model="form.ballIndex" placeholder="请选择设备ballIndex">
                <el-option value="0">0</el-option>
                <el-option value="1">1</el-option>
                <el-option value="2">2</el-option>
                <el-option value="3">3</el-option>
                <el-option value="4">4</el-option>
                <el-option value="5">5</el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" class="row-bg" justify="space-around">
          <el-col :span="11">
            <el-form-item label="负责人联系方式" prop="devicePhone" label-width="170">
              <el-input v-model="form.devicePhone" placeholder="请输入负责人联系方式" maxlength="11"  v-no-whitespace/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="视频编码" prop="typeCode" label-width="170">
              <el-input v-model="form.typeCode" placeholder="请输入视频编码" maxlength="32"  v-no-whitespace/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--隐藏字段-->
        <el-row type="flex" class="row-bg" justify="space-around">
          <el-col :span="11">
            <el-form-item label="在线状态" prop="deviceStatus" label-width="150">
               <el-switch v-model="form.deviceStatus" :active-value='1' :inactive-value='0'></el-switch>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item label="新增人" prop="createUser" v-show="showDeviceName">
              <el-input v-model="form.createUser" placeholder="" :placeholder="isEditing ? '请输入新增人' : ''"
                        :disabled="!isEditing" v-no-whitespace
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" class="row-bg" justify="space-around">
          <el-col :span="11">
            <el-form-item label="新增时间" prop="createTime" v-show="showDeviceName">
              <el-input v-model="form.createTime" placeholder="" :placeholder="isEditing ? '请输入新增时间' : ''"
                        :disabled="!isEditing" v-no-whitespace
              />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="当前绑定工程" prop="proName" v-show="showDeviceName" label-width="150">
              <el-input v-model="form.proName" placeholder="" :placeholder="isEditing ? '请输入当前绑定工程' : ''"
                        :disabled="!isEditing" v-no-whitespace
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-preventReClick="5000">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  selectDeviceType
} from '@/api/tool/select'
import {
  addDeviceInformation, delDeviceInformation,
  listDeviceInformation,
  updateDeviceInformation
} from '@/api/terminalEquipment/deviceInformation'
import { hideSensitiveInfo } from '../../utils/bonus'
import numberUtils from '@/utils/validate'
import dict from '@/utils/dict'

export default {
  name: 'Post',
  dicts: ['sys_normal_disable','sys_device_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      deviceCodes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      deviceInformation: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      labelPosition: 'top',
      // 角色选项
      deviceOptions: [],
      // 控制显示或隐藏的变量
      showDeviceName: false,
      // 默认为新增模式，可编辑
      isEditing: false,
      // 地图是否显示弹出层
      innerVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        createUser: undefined,
        deviceUser: undefined,
        deviceType: undefined
      },
      // 表单参数
      form: {
        deviceName: undefined,
        deviceType: undefined,
        deviceModel: undefined,
        puid: undefined,
        ballIndex: '0',
        deviceStatus:'0',
        typeCode:'',
        deviceUser: undefined,
        devicePhone: undefined,
        createUser: undefined,
        createTime: undefined,
        proName: undefined,
        deviceTypeName: undefined
      },
      // 表单校验
      rules: {
        deviceName: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        deviceType: [
          { required: true, message: '设备类型不能为空', trigger: 'blur' }
        ],
        deviceModel: [
          { required: true, message: '设备型号不能为空', trigger: 'blur' }
        ],
        puid:[
          { required: true, message: '设备puid不能为空', trigger: 'blur' }
        ],
        ballIndex:[
          { required: true, message: '设备ballIndex不能为空', trigger: 'blur' }
        ],
        deviceUser: [
          { required: true, message: '设备负责人不能为空', trigger: 'blur' }
        ],
        devicePhone: [
          {
            required: true,
            validator: numberUtils.checkPhone,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDeviceType()
  },
  methods: {
    dict,
    hideSensitiveInfo,
    // 改变类型名称
    changeTypeName(e) {
      this.form.deviceTypeName = this.deviceOptions.find(item => item.deviceType === e).deviceName
      this.form.ballIndex = '0'
    },
    //获取工程地址选项
    getDeviceType() {
      const params = {}
      selectDeviceType(params).then(response => {
        console.log('response:', response.data)
        this.deviceOptions = response.data
      })
    },

    /** 查询设备列表 */
    getList() {
      this.loading = true
      listDeviceInformation(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.deviceInformation = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: undefined,
        deviceType: undefined,
        deviceName: undefined,
        devicePhone: undefined,
        deviceModel: undefined,
        ballIndex:0,
        deviceStatus:0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.deviceCodes = selection.map(item => item.deviceCode)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getDeviceType()
      this.isEditing = true
      this.showDeviceName = false
      this.open = true
      this.title = '添加设备信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('row:', row)
      this.reset()
      this.getDeviceType()
      this.isEditing = false
      this.showDeviceName = true
      row.createTime = this.parseTime(row.createTime)
      this.form = row
      this.open = true
      this.title = '修改设备信息'
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.deviceId != undefined) {
            this.form.createTime = new Date(this.form.createTime.replace(' ', 'T'))
            updateDeviceInformation(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            }).catch(() => {
              this.form.createTime = this.parseTime(this.form.createTime)
            })
          } else {
            addDeviceInformation(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const addressIds = row.deviceId || this.ids
      const deviceCodes = row.deviceCode || this.deviceCodes
      this.$modal.confirm('是否确认删除设备编号为"' + deviceCodes + '"的数据项？').then(function() {
        return delDeviceInformation(addressIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.derive('project/deviceInformation/export', {
        ...this.queryParams
      }, `设备信息_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
