<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">设备编号</label>
        <el-input v-model="query.code" clearable placeholder="设备编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">设备名称</label>
        <el-input v-model="query.name" clearable placeholder="设备名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">设备安装位置</label>
        <el-select
          v-model="query.floorId"
          clearable
          size="small"
          placeholder="设备安装位置"
          class="filter-item"
          style="width: 185px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in floors"
            :key="item.floorName"
            :label="item.floorName"
            :value="item.floorId"
          />
        </el-select>
        <date-range-picker
          v-model="query.buildDate"
          start-placeholder="buildDateStart"
          end-placeholder="buildDateStart"
          class="date-item"
        />
        <date-range-picker
          v-model="query.lastGuarantee"
          start-placeholder="lastGuaranteeStart"
          end-placeholder="lastGuaranteeStart"
          class="date-item"
        />
        <rrOperation :crud="crud" />
        <!-- <deviceInfo /> -->
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="600px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="设备名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="设备编号" prop="code">
            <el-input v-model="form.code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="打印机ip" prop="printingIp">
            <el-input v-model="form.printingIp" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="打印机地址" prop="printingAddress">
            <el-input v-model="form.printingAddress" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="读卡器ip" prop="cardIp">
            <el-input v-model="form.cardIp" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="产品类型" prop="style">
            <el-input v-model="form.style" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="安装地址" prop="floor.floorId">
            <el-select
              v-model="form.floor.floorId"
              style="width: 370px"
              placeholder="请选择"
              clearable
              filterable
              @change="changeFloor"
            >
              <el-option
                v-for="item in floors"
                :key="item.id"
                :label="item.floorName"
                :value="item.floorId"
                :disabled="item.floorId == form.floor.floorId"
              />
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="设备状态" prop="status">-->
          <!--            <el-select v-model="form.status" filterable placeholder="请选择" style="width: 370px">-->
          <!--              <el-option-->
          <!--                v-for="item in dict.dept_status"-->
          <!--                :key="item.id"-->
          <!--                :label="item.label"-->
          <!--                :value="item.value"-->
          <!--              />-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item label="安装时间" prop="buildDate">
            <el-date-picker v-model="form.buildDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="保修期" prop="warranty">
            <el-input v-model="form.warranty" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="最新维修日期" prop="lastGuarantee">
            <el-date-picker v-model="form.lastGuarantee" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="运维人员" prop="operator">
            <el-input v-model="form.operator" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="联系方式" prop="linkType">
            <el-input v-model="form.linkType" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否是公共打印机">
            <el-radio-group v-model="form.isCommonPrinter" style="width: 193px">
              <el-radio
                v-for="item in dict.is_true"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @row-dblclick="showInfo" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="id" label="主键" /> -->
        <el-table-column prop="code" label="设备编号" />
        <el-table-column prop="name" label="设备名称" />
        <el-table-column prop="style" label="产品类型" />
        <el-table-column prop="floor.floorName" label="设备安装位置" />
        <el-table-column prop="status" label="设备状态">
          <template slot-scope="scope">
            {{ dict.label.device_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="buildDate" label="安装时间" />
        <el-table-column prop="warranty" label="保修期" />
        <el-table-column prop="lastGuarantee" label="最新维修日期" />
        <el-table-column prop="operator" label="运维人员" />
        <el-table-column prop="linkType" label="联系方式" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column :show-overflow-tooltip="true" prop="isCommonPrinter" label="是否是公共打印机">
          <template slot-scope="scope">
            <div v-if="scope.row.isCommonPrinter == 1">是</div>
            <div v-if="scope.row.isCommonPrinter == 0">否</div>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','dmDevice:edit','dmDevice:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmDevice from '@/api/dm/device/dmDevice'
import { isvalidPhone } from '@/utils/validate'
import { getAllFloor } from '@/api/dm/floor/dmFloor'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import dateRangePicker from '@/components/DateRangePicker'
// import deviceInfo from '@/views/dm/device/info'

const defaultForm = { id: null, name: null, style: null, floor: { floorId: null }, status: null, buildDate: null, warranty: null, lastGuarantee: null, operator: null, linkType: null, remark: null, isCommonPrinter: '0', printingIp: null, cardIp: null, printingAddress: null }
export default {
  name: 'DmDevice', //, deviceInfo
  components: { pagination, crudOperation, rrOperation, udOperation, dateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['device_status', 'is_true'],
  cruds() {
    return CRUD({ title: '设备', url: 'api/dmDevice', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDmDevice }})
  },
  data() {
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入手机号号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      floors: [],
      floor: '', // 安装地址
      permission: {
        add: ['admin', 'dmDevice:add'],
        edit: ['admin', 'dmDevice:edit'],
        del: ['admin', 'dmDevice:del']
      },
      rules: {
        id: [
          { required: true, message: '主键不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '设备编号不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        style: [
          { required: true, message: '产品类型不能为空', trigger: 'blur' }
        ],
        'floor.floorId': [
          { required: true, message: '设备安装位置不能为空', trigger: 'blur' }
        ],
        operator: [
          { required: true, message: '运维人员不能为空', trigger: 'blur' }
        ],
        linkType: [
          { required: true, trigger: 'blur', validator: validPhone }
        ],
        lastGuarantee: [
          { required: true, message: '最新维修日期不能为空', trigger: 'blur' }
        ],
        warranty: [
          { required: true, message: '保修期不能为空', trigger: 'blur' }
        ],
        buildDate: [
          { required: true, message: '安装时间不能为空', trigger: 'blur' }
        ],
        printingIp: [
          { required: true, message: '打印机ip不能为空', trigger: 'blur' }
        ],
        cardIp: [
          { required: true, message: '读卡器ip不能为空', trigger: 'blur' }
        ],
        printingAddress: [
          { required: true, message: '打印机安装地址不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'code', display_name: '设备编号' },
        { key: 'name', display_name: '设备名称' },
        { key: 'style', display_name: '产品类型' },
        { key: 'floorId', display_name: '设备安装位置' },
        { key: 'status', display_name: '设备状态' },
        { key: 'warranty', display_name: '保修期' },
        { key: 'operator', display_name: '运维人员' },
        { key: 'linkType', display_name: '联系方式' },
        { key: 'remark', display_name: '备注' }
      ]
    }
  },
  created() {
    console.info('params', this.user)
    this.getFloors()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToAdd]() {
      this.getFloors()
    },
    getFloors() {
      getAllFloor().then(res => {
        this.floors = res
      }).catch(() => { })
    },
    deleteTag(value) {
      if (this.floor === value) {
        this.floor = ''
      }
    },
    changeFloor(value) {
      this.floor = value
    },
    showInfo(row) {
      console.info('data ==>', row)
      this.crud.doOpenDeviceInfo(row)
    }
  }
}
</script>

<style scoped>

</style>
