<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="设备号：">
        <el-input v-model="form.dsn" placeholder="请输入设备号" clearable />
      </el-form-item>
      <el-form-item label="设备型号：">
        <el-input v-model="form.clientType" placeholder="请输入设备型号" clearable />
      </el-form-item>
      <el-form-item label="设备标识：">
        <el-input v-model="form.deviceMac" placeholder="请输入设备标识" clearable />
      </el-form-item>
      <el-form-item label="设备类型：">
        <el-select v-model="form.deviceType" placeholder="请选择设备类型" clearable>
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker v-model="createTime" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss" clearable />
      </el-form-item>
      <el-form-item label="激活时间：">
        <el-date-picker v-model="updateTime" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss" clearable />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button icon="el-icon-plus" type="primary" plain @click="dialogVisible = true"
          v-hasPermi="['device:yxw:add']">新增</el-button>
        <el-button icon="el-icon-download" type="primary" @click="getDownLoadNo">导出设备号</el-button>
        <el-button icon="el-icon-download" type="primary" @click="getDownLoadData">导出数据</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column prop="id" label="设备号" align="center" show-overflow-tooltip />
      <el-table-column prop="deviceMac" label="设备标识" align="center" />
      <el-table-column prop="clientType" label="设备型号" align="center" />
      <el-table-column prop="type" label="设备类型" align="center" />
      <el-table-column prop="createTimeStr" label="创建时间" align="center" />
      <el-table-column prop="updateTimeStr" label="激活时间" align="center" />
      <el-table-column prop="statusStr" label="当前状态" align="center" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageIndex" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog title="新增" :visible.sync="dialogVisible" width="500px">
      <el-form :model="dialogForm" ref="form" label-width="100px">
        <el-form-item prop="num" label="设备数量：" :rules="[{ required: true, message: '请输入设备数量', trigger: 'blur' }]">
          <el-input v-model="dialogForm.num" placeholder="请输入设备数量" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getYxwPage, getYxwAdd } from "@/api/device/yxw";
import { devTypeMixin } from '@/mixin/index'
export default {
  mixins: [devTypeMixin],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 当前状态列表
      statusOptions: [
        {
          value: 1,
          label: "未使用",
        },
        {
          value: 4,
          label: "已使用",
        },
      ],
      // 表单
      form: {
        pageIndex: 1,
        pageSize: 10,
      },
      // 创建时间
      createTime: [],
      // 更新时间
      updateTime: [],
      // 总数
      total: 0,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      // 新增设备数量
      dialogForm: {
        num: ""
      }
    };
  },
  watch: {
    createTime(val) {
      this.form.startCreateTime = val[0]
      this.form.endCreateTime = val[1]
    },
    updateTime(val) {
      this.form.startUpdateTime = val[0]
      this.form.endUpdateTime = val[1]
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 搜索
    getSearch() {
      this.form.pageIndex = 1;
      this.getList();
    },

    // 重置
    getRefresh() {
      this.form = {
        pageIndex: 1,
        pageSize: 10,
      };
      this.createTime = this.updateTime = [];
      this.getList();
    },

    // 分页
    getList() {
      this.loading = true;
      getYxwPage(this.form).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        }
      });
    },

    // 新建设备号
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          getYxwAdd(this.dialogForm).then((res) => {
            if (res.code === 0) {
              this.$message.success("新增成功！");
              this.dialogVisible = false;
              this.getList();
            }
          });
        } else {
          return false
        }
      })

    },

    // 导出设备号
    getDownLoadNo() {
      this.download(
        `/device/yunxiaowei/exportDeviceNo`,
        this.form,
        `云小微设备号.xlsx`
      );
    },

    // 导出数据
    getDownLoadData() {
      this.download(
        `/device/yunxiaowei/exportDeviceData`,
        this.form,
        `云小微设备数据.xlsx`
      );
    }
  },
};
</script>