<template>
  <el-container class="content-height-fit">
    <inner-sidebar>
      <ProjectSectionTree @node-click="handleTreeClick" />
    </inner-sidebar>
    <el-container class="resize-fit-container">
      <div class="app-container">
        <div class="filter-container blue-top-border">
          <el-select
            v-model="listQuery.sectionAreaId"
            placeholder="工区"
            class="filter-item el-selects"
            clearable
            @clear="delValue('sectionAreaId')"
            @change="getList"
          >
            <el-option
              v-for="item in areas"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
          <el-input
            v-model="listQuery.deviceNo"
            placeholder="设备编号"
            class="filter-item"
            clearable
            @change="getList"
          />
          <el-input
            v-model="listQuery.deviceName"
            placeholder="设备名称"
            class="filter-item"
            clearable
            @change="getList"
          />
          <el-select
            v-model="listQuery.deviceType"
            placeholder="设备类型"
            class="filter-item el-selects"
            clearable
            @clear="delValue('deviceType')"
            @change="getList"
          >
            <el-option
              v-for="item in deviceTypes"
              :key="item.key"
              :value="item.key"
              :label="item.name"
            />
          </el-select>
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleFilter"
          >查询</el-button>
          <el-button
            v-if="$requirePermission('BSET.DVMG.C')"
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleCreate"
          >新增</el-button>
        </div>
        <div class="content-container">
          <el-table
            v-loading="listLoading"
            row-key="id"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%"
            height="calc(100vh - 276px)"
          >
            <div slot="empty">
              <span v-if="listQuery.sectionId">没有数据</span>
              <span v-else>请选择标段</span>
            </div>
            <el-table-column label="序号" type="index" width="50" align="center" />
            <!-- <el-table-column label="工区名称" align="center">
              <template slot-scope="{ row }">
                {{ getAreaName(row.sectionAreaId) }}
              </template>
            </el-table-column> -->
            <el-table-column label="设备编号" prop="deviceNo" :show-overflow-tooltip="true" align="center" />
            <el-table-column label="设备名称" prop="deviceName" :show-overflow-tooltip="true" align="center" />
            <el-table-column label="类型名称" prop="categoryName" :show-overflow-tooltip="true" align="center" />
            <el-table-column label="规格型号" prop="spec" :show-overflow-tooltip="true" align="center" />
            <el-table-column label="制造厂家" prop="madeFactory" :show-overflow-tooltip="true" align="center" />
            <!-- <el-table-column label="司机姓名" prop="driverName" width="80" align="center" />
            <el-table-column label="联系电话" prop="telephone" width="120" align="center" /> -->
            <el-table-column label="累计过孔数" prop="viaCount" width="100" align="center" />
            <el-table-column label="累计工作时间" prop="allTime" width="110" align="center" />
            <el-table-column label="是否在线" width="80" align="center">
              <template slot-scope="{ row }">
                <el-tag size="mini" :type="row.isOnline?'success':'danger'">{{ row.isOnLine ? "在线":"离线" }}</el-tag>
                <!-- {{ row.isOnLine ? "是":"否" }} -->
              </template>
            </el-table-column>
            <el-table-column label="设备类型" prop="deviceTypeName" width="80" align="center" />
            <el-table-column
              label="操作"
              align="center"
              width="110"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="{ row }">
                <el-button
                  v-if="$requirePermission('BSET.DVMG.U')"
                  type="primary"
                  size="mini"
                  @click="handleUpdate(row)"
                >修改</el-button>
                <el-button
                  v-if="$requirePermission('BSET.DVMG.D')"
                  size="mini"
                  type="danger"
                  @click="handleDelete(row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            :total="total"
            :skip-count.sync="listQuery.skipCount"
            :max-result-count.sync="listQuery.maxResultCount"
            @pagination="getList"
          />
        </div>
        <el-dialog
          :title="textMap[dialogStatus]"
          :close-on-click-modal="false"
          :visible.sync="dialogFormVisible"
        >
          <el-form
            ref="dataForm"
            v-loading="formLoading"
            :rules="rules"
            :model="entity"
            label-position="right"
            label-width="120px"
          >
            <el-row :gutter="16">
              <el-col :span="12">
                <el-form-item label="工区" prop="sectionAreaId">
                  <el-select v-model="entity.sectionAreaId" class="w100p">
                    <el-option
                      v-for="item in areas"
                      :key="item.id"
                      :value="item.id"
                      :label="item.name"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="设备编号" prop="deviceNo">
                  <el-input v-model="entity.deviceNo" :show-overflow-tooltip="true" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="16">
              <el-col :span="12">
                <el-form-item label="设备名称" prop="deviceName">
                  <el-input v-model="entity.deviceName" :show-overflow-tooltip="true" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="设备类型" prop="deviceType">
                  <el-select v-model="entity.deviceType" class="w100p">
                    <el-option
                      v-for="item in deviceTypes"
                      :key="item.key"
                      :value="item.key"
                      :label="item.name"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="16">
              <el-col :span="12">
                <el-form-item label="规格型号">
                  <el-input v-model="entity.spec" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="制造厂家" align="left">
                  <el-input v-model="entity.madeFactory" align="left" controls-position="right" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="16">
              <el-col :span="12">
                <el-form-item label="司机姓名" align="left">
                  <el-input v-model="entity.driverName" align="left" controls-position="right" style="width: 100%;" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" align="left">
                  <el-input v-model="entity.telephone" align="left" controls-position="right" style="width: 100%;" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="entity.deviceType === 1" :gutter="16">
              <el-col :span="12">
                <el-form-item label="架桥机类型" prop="category">
                  <el-select v-model="entity.category" class="w100p">
                    <el-option
                      v-for="item in jqjTypes"
                      :key="item.code"
                      :value="item.code"
                      :label="item.name"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="entity.deviceType === 2" :gutter="16">
              <el-col :span="12">
                <el-form-item label="运梁车类型" prop="category">
                  <el-select v-model="entity.category" class="w100p">
                    <el-option
                      v-for="item in ylcTypes"
                      :key="item.code"
                      :value="item.code"
                      :label="item.name"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button
              :disabled="formLoading"
              @click="dialogFormVisible = false"
            >取消</el-button>
            <el-button
              :disabled="formLoading"
              type="primary"
              @click="dialogStatus === 'create' ? createData() : updateData()"
            >确认</el-button>
          </div>
        </el-dialog>
      </div>
    </el-container>
  </el-container>
</template>

<script>
import * as interfaceApi from '@/api/device-manage.js'
import * as areaApi from '@/api/backbone/section-area'
import * as dictEntryApi from '@/api/dict-entry.js'
import ProjectSectionTree from '@/views/projects/components/ProjectSectionTree'

import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import InnerSidebar from '@/components/InnerSidebar'

export default {
  name: 'DeviceMng',
  directives: { waves },
  components: { Pagination, ProjectSectionTree, InnerSidebar },
  data() {
    return {
      list: [],
      deviceTypes: [
        { key: 1, name: '架桥机' },
        { key: 2, name: '运梁车' }
      ],
      entity: {
        id: undefined,
        projectId: undefined,
        sectionId: undefined,
        sectionAreaId: undefined,
        deviceNo: '',
        deviceName: '',
        category: undefined,
        categoryName: '',
        spec: '',
        madeFactory: '',
        driverName: '',
        telephone: '',
        deviceType: undefined
      },
      total: 0,
      listLoading: false,
      listQuery: {
        projectId: undefined,
        sectionId: undefined,
        sectionAreaId: undefined,
        deviceNo: '',
        deviceName: '',
        deviceType: undefined,
        skipCount: 0,
        maxResultCount: 20
      },
      dialogFormVisible: false,
      dialogStatus: '',
      formLoading: false,
      textMap: {
        update: '修改',
        create: '创建'
      },
      sections: [],
      rules: {
        deviceNo: [
          { required: true, message: '请输入设备编号', trigger: 'blur' },
          { max: 32, message: '长度必须少于32个字符', trigger: 'blur' }
        ],
        deviceName: [
          { required: true, message: '请输入设备名称', trigger: 'blur' },
          { max: 64, message: '长度必须少于64个字符', trigger: 'blur' }
        ],
        deviceType: [
          { required: true, message: '请选择设备类型', trigger: 'blur' }
        ],
        sectionAreaId: [
          { required: true, message: '请选择工区', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请选择架桥机类型', trigger: 'blur' }
        ]
      },
      areas: [],
      jqjTypes: [],
      ylcTypes: []
    }
  },
  watch: {
  },
  mounted() {
    this.getList()
    this.getJQJTypes()
    this.getYLCTypes()
  },
  methods: {
    getJQJTypes() {
      dictEntryApi.getJQJTypes().then(res => {
        res.forEach(t => {
          t.code = t.code - 0
        })
        this.jqjTypes = res
      })
    },
    getYLCTypes() {
      dictEntryApi.getYLCTypes().then(res => {
        res.forEach(t => {
          t.code = t.code - 0
        })
        this.ylcTypes = res
      })
    },
    // 树形事件
    handleTreeClick(data, node) {
      this.listQuery.projectId = undefined
      this.listQuery.sectionId = undefined
      if (data.isProject) {
        this.listQuery.projectId = data.id
        this.listQuery.sectionId = undefined
      }
      if (data.isSection) {
        this.listQuery.projectId = data.projectId
        this.listQuery.sectionId = data.id
        this.getArea()
      }
      this.getList()
    },
    getArea() {
      this.areas = []
      areaApi.lookupBySection(this.listQuery.sectionId).then(res => {
        res.forEach(t => {
          this.areas.push({
            id: t.id,
            name: t.name
          })
        })
      })
    },
    getAreaName(areaId) {
      const area = this.areas.find(t => t.id === areaId)
      if (area) {
        return area.name
      } else {
        return ''
      }
    },
    // 清空下拉
    delValue(parameter) {
      this.listQuery[parameter] = undefined
    },
    // 获取数据列表
    getList() {
      if (!this.listQuery.projectId) {
        this.list = []
        return
      }
      this.listLoading = true
      interfaceApi
        .getList(this.listQuery)
        .then(res => {
          this.list = res.items
          this.total = res.totalCount
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 查询
    handleFilter() {
      this.listQuery.skipCount = 0
      this.getList()
    },
    // 创建清空
    resetStation() {
      this.entity = {
        id: undefined,
        projectId: this.listQuery.projectId,
        sectionId: this.listQuery.sectionId,
        sectionAreaId: this.listQuery.sectionAreaId,
        deviceNo: '',
        deviceName: '',
        category: undefined,
        categoryName: '',
        spec: '',
        madeFactory: '',
        driverName: '',
        telephone: '',
        deviceType: undefined
      }
    },
    // 创建点击事件
    handleCreate() {
      if (!this.listQuery.sectionId) {
        this.verificationTips('请选择标段')
        return
      }
      this.resetStation()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.formLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 提示
    verificationTips(message) {
      this.$notify({
        title: '提示',
        message: message,
        type: 'error',
        duration: 2000
      })
    },
    // 创建
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (this.entity.deviceType === 1) {
            this.entity.categoryName = this.jqjTypes.find(t => t.code === this.entity.category).name
          }
          if (this.entity.deviceType === 2) {
            this.entity.categoryName = this.ylcTypes.find(t => t.code === this.entity.category).name
          }
          this.formLoading = true
          interfaceApi
            .create(this.entity)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
            .finally(() => {
              this.formLoading = false
            })
        }
      })
    },
    // 修改点击事件
    handleUpdate(row) {
      this.entity = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.formLoading = false
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (this.entity.deviceType === 1) {
            this.entity.categoryName = this.jqjTypes.find(t => t.code === this.entity.category).name
          }
          if (this.entity.deviceType === 2) {
            this.entity.categoryName = this.ylcTypes.find(t => t.code === this.entity.category).name
          }
          const submitData = Object.assign({}, this.entity)
          this.formLoading = true
          interfaceApi
            .update(submitData.id, submitData)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
            })
            .finally(() => {
              this.formLoading = false
            })
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        interfaceApi
          .remove(row.id)
          .then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
          .finally(() => {
            this.listLoading = false
          })
      })
    }
  }
}
</script>

<style  scoped>
.number >>> input{
  text-align: left !important;
}
.el-selects >>> .el-input--suffix{
  width: 150px !important;
}
</style>
