<template>
  <div class="app-container">
    <el-button
      type="primary"
      icon="el-icon-plus"
      @click="create"
    >添加设备</el-button>
    <!-- <el-select
      filterable
      clearable
      v-model="page.data.sn"
      placeholder="请选择设备编号"
      class="margin-left input"
    >
      <el-option
        v-for="item in machine_list"
        :key="item.sn"
        :label="item.sn"
        :value="item.sn"
      >
      </el-option>
    </el-select>
    <el-select
      filterable
      clearable
      v-model="page.data.sn"
      placeholder="请选择设备地点"
      class="margin-left input"
    >
      <el-option
        v-for="item in machine_list"
        :key="item.sn"
        :label="item.name"
        :value="item.sn"
      >
      </el-option>
    </el-select> -->
    <el-input
      v-model="page.data.sn"
      placeholder="请输入设备编号"
      class="margin-left input"
    />
    <el-input
      v-model="page.data.address"
      placeholder="请输入设备地点"
      class="margin-left input"
    />
    <el-input
      v-model="page.data.companyName"
      placeholder="请输入公司名称"
      class="margin-left input"
    />
    <el-button
      type="primary"
      icon="el-icon-search"
      class="margin-left"
      @click="fetchData"
    >搜索</el-button>
    <!--主表格-->
    <el-table
      ref="table_form"
      v-loading="listLoading"
      class="table"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" align="center" width="80px">
        <template slot-scope="scope">
          {{ indexMethod(scope.$index) }}
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center">
        <template slot-scope="scope">
          {{ scope.row.sn | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="设备地点" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.address | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.head | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人电话" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.headPhone | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.companyName | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否开启场所码" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.enableStatus === true"
            type="success"
          >开启</el-tag>
          <el-tag v-else type="info">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="每秒请求数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.requestNumber || '无限次' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调用上限次数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.requestNumberLimit || '无限次' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已调用次数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.requestedTimes }}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余调用次数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.requestNumberLimit?scope.row.validRequestTimes:'无限次' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | matchNull }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作区" width="250">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="deleteUser(scope.row.id)"
          >删除</el-button>
          <el-button
            type="primary"
            size="small"
            @click="update(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <pagination
      :total="total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="fetchData"
    />
    <!--对话框-->
    <el-dialog
      :title="dialogTitle[dialogType]"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        label-position="left"
      >
        <div class="flex">
          <el-form-item style="flex: 0.5" label="设备编号" prop="sn">
            <el-input
              v-model.trim="form.sn"
              maxlength="50"
              show-word-limit
              placeholder="请填写设备编号"
            />
          </el-form-item>
          <el-form-item label="设备地点" prop="address" style="flex: 0.5">
            <el-input
              v-model.trim="form.address"
              maxlength="50"
              show-word-limit
              placeholder="请填写设备地点"
            />
          </el-form-item>
        </div>
        <div class="flex">
          <el-form-item style="flex: 0.5" label="负责人姓名" prop="head">
            <el-input v-model.trim="form.head" placeholder="请填写负责人姓名" />
          </el-form-item>
          <el-form-item label="负责人电话" prop="headPhone" style="flex: 0.5">
            <el-input
              v-model.trim="form.headPhone"
              maxlength="11"
              show-word-limit
              placeholder="请填写负责人电话"
            />
          </el-form-item>
        </div>
        <el-form-item label="公司名称" prop="companyName">
          <el-input
            v-model.trim="form.companyName"
            placeholder="请填写公司名称"
          />
        </el-form-item>
        <div class="flex">
          <el-form-item
            label="是否开启场所码"
            prop="enableStatus"
            style="flex: 0.5"
          >
            <el-radio-group v-model="form.enableStatus">
              <el-radio :label="false">停用</el-radio>
              <el-radio :label="true">启用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="每秒请求数（次）"
            prop="requestNumber"
            style="flex: 0.5"
          >
            <el-input-number
              v-model="form.requestNumber"
              :min="0"
            />
          </el-form-item>
        </div>
        <el-tooltip
          class="item"
          effect="dark"
          content="0默认无限次（次）"
          placement="top-start"
        >
          <el-form-item
            label="调用上限"
            prop="requestNumberLimit"
            style="flex: 0.5"
          >
            <el-input-number
              v-model="form.requestNumberLimit"
              :min="0"
            />
          </el-form-item>
        </el-tooltip>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false"> 取消 </el-button>
        <el-button
          type="primary"
          @click="dialogType === 'add' ? add() : doUpdate()"
        >
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  healthCodeDel,
  healthCodeSave,
  getFaceDeviceList,
  healthCodeGetPageList
} from '@/api/ordinary_user'
import Pagination from '@/components/Pagination'

export default {
  name: '',
  components: { Pagination },
  filters: {
    matchNull(str) {
      return str || '暂无数据'
    }
  },
  data() {
    return {
      // 主表格&主表格加载状态
      list: null,
      listLoading: true,
      // 分页参数
      page: {
        data: {
          sn: null,
          companyName: null,
          address: null
        },
        pageNum: 1,
        pageSize: 10
      },
      total: 0,

      // 设备列表_搜索
      machine_list: [],
      // 添加账号参数
      form: {
        sn: null,
        address: null,
        head: null,
        headPhone: null,
        companyName: null,
        enableStatus: false,
        requestNumber: 0,
        requestNumberLimit: 0
      },
      form_copy: {},
      // 验证规则
      rules: {
        sn: [{ required: true, message: '请填写sn', trigger: 'blur' }],
        address: [{ required: true, message: '请填写地址', trigger: 'blur' }],
        head: [
          { required: true, message: '请填写负责人姓名', trigger: 'blur' }
        ],
        headPhone: [
          { required: true, message: '请填写负责人电话', trigger: 'blur' }
        ],
        companyName: [
          { required: true, message: '请填写公司名称', trigger: 'blur' }
        ]
      },
      // 是否显示对话框
      dialogVisible: false,
      // 对话框标题
      dialogTitle: {
        add: '添加设备',
        update: '编辑设备'
      },
      dialogType: 'add'
    }
  },
  computed: {},
  watch: {},
  created() {
    this.form_copy = JSON.parse(JSON.stringify(this.form))
    this.fetchData()
    // this.getFaceDeviceList();
  },
  mounted() {},
  methods: {
    // 获取列表数据
    fetchData() {
      this.listLoading = true
      healthCodeGetPageList(this.page).then((res) => {
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      })
    },
    // 获取设备列表-搜索
    // getFaceDeviceList() {
    //   getFaceDeviceList().then((res) => {
    //     this.machine_list = res.data;
    //   });
    // },
    // 打开添加设备对话框
    create() {
      this.form = JSON.parse(JSON.stringify(this.form_copy))
      this.dialogType = 'add'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    // 确定添加
    add() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          healthCodeSave(this.form).then((res) => {
            this.dialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
          })
        }
      })
    },
    // 删除此设备
    deleteUser(id) {
      this.$confirm('确定要删除此设备吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        healthCodeDel(id).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    // 打开编辑对话框
    update(row) {
      this.form = Object.assign(this.form, row)
      this.dialogType = 'update'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    // 确定编辑
    doUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          healthCodeSave(this.form).then((res) => {
            this.dialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '编辑成功!'
            })
          })
        }
      })
    },
    // 返回列表序号
    indexMethod(index) {
      return (this.page.pageNum - 1) * this.page.pageSize + index + 1
    }
  }
}
</script>

<style scoped lang="scss">
.table {
  margin-top: 15px;
}
.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.margin-left {
  margin: 10px 0 0 10px;
}
.input {
  width: 200px;
}
</style>
