<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-if="showSearch" label-width="68px">
      <el-form-item prop="deviceKey">
        <el-input
          v-model="queryParams.deviceKey"
          placeholder="请输入客户端标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item prop="creditNo">
        <el-input
          v-model="queryParams.creditNo"
          placeholder="请输入统一社会信用号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="['biz:device:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:device:edit']"
        >修改
        </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="['biz:device: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="['biz:device:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
        type="index"
        label="序号"
      >
      </el-table-column>
      <el-table-column label="客户端标识" align="center" prop="deviceKey"/>
      <el-table-column prop="type" label="客户端类型">
        <template slot-scope="scope">
          <span>{{ deviceTypeMap[scope.row.type] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="企业名称" align="center" prop="companyName"/>
      <el-table-column label="统一社会信用号" align="center" prop="creditNo"/>
      <el-table-column label="企业类型" align="center" prop="companyType">
        <template slot-scope="scope">
          <span>{{ companyTypeMap[scope.row.companyType] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="企业状态" align="center" prop="status"/>
      <el-table-column label="上线状态" align="center" prop="onLineStatus"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <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="['biz:device:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:device: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"
    />

    <!-- 添加或修改biz对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="12" class="el-col">
            <el-form-item label="客户端标识" prop="deviceKey">
              <el-input v-model="form.deviceKey" placeholder="请输入客户端标识" disabled/>
            </el-form-item>
          </el-col>

          <el-col :span="12" class="el-col">
            <el-form-item label="客户端类型" prop="type">

              <el-select v-model="form.type" placeholder="请选择客户端类型" clearable style="width: 100%">
                <el-option label="赋码系统" :value="1"></el-option>
                <el-option label="UDIMS平台" :value="2"></el-option>
                <el-option label="企业客户端" :value="3"></el-option>
                <el-option label="UDI自助平台" :value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


        </el-row>
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="12" class="el-col">
            <el-form-item label="统一社会信用号" prop="creditNo">
              <el-input v-model="form.creditNo" placeholder="请输入统一社会信用号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="12" class="el-col">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="form.tel" placeholder="请输入联系电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="12" class="el-col">
            <el-form-item label="公司地址" prop="addr">
              <el-input v-model="form.addr" placeholder="请输入地址"/>
            </el-form-item>
          </el-col>


          <el-col :span="12" class="el-col">
            <el-form-item label="企业类型" prop="type">
              <el-select v-model="form.companyType" placeholder="请选择企业类型" clearable style="width: 100%">
                <el-option label="生产企业" :value="1"></el-option>
                <el-option label="流通企业" :value="2"></el-option>
                <el-option label="医疗机构" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>


        </el-row>
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="12" class="el-col">
            <el-form-item label="接口服务地址" prop="apiUrl">
              <el-input v-model="form.apiUrl" placeholder="请输入接口服务地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="el-col">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDevice, getDevice, delDevice, addDevice, updateDevice} from "@/api/biz/device";

export default {
  name: "Device",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // biz表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceKey: undefined,
        type: undefined,
        ip: undefined,
        creditNo: undefined,
        status: undefined,
        onLineStatus: undefined,
        apiUrl: undefined,
        companyName: undefined,
        tel: undefined,
        contact: undefined,
        addr: undefined,
        companyType: undefined,
        createTime: undefined,
        updateTime: undefined
      },
      // 表单参数
      form: {},

      //客户端类型
      deviceTypeMap: {
        1: "赋码系统",
        2: "UDIMS平台",
        3: "企业客户端",
        4: "UDI自助平台"
      },

      companyTypeMap: {
        1: "生产企业",
        2: "流通企业",
        3: "医疗机构",
      },

      // 表单校验
      rules: {
        type: [
          {required: true, message: "客户端类型不能为空", trigger: "change"}
        ],
        ip: [
          {required: true, message: "客户端IP地址不能为空", trigger: "blur"}
        ],
        creditNo: [
          {required: true, message: "统一社会信用号不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "企业状态不能为空", trigger: "blur"}
        ],
        onLineStatus: [
          {required: true, message: "上线状态不能为空", trigger: "blur"}
        ],
        apiUrl: [
          {required: true, message: "接口服务地址不能为空", trigger: "blur"}
        ],
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ],
        companyName: [
          {required: true, message: "公司名称不能为空", trigger: "blur"}
        ],
        tel: [
          {required: true, message: "联系电话不能为空", trigger: "blur"}
        ],
        contact: [
          {required: true, message: "联系人不能为空", trigger: "blur"}
        ],
        addr: [
          {required: true, message: "地址不能为空", trigger: "blur"}
        ],
        companyType: [
          {required: true, message: "企业类型不能为空", trigger: "change"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询biz列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deviceKey: undefined,
        type: undefined,
        ip: undefined,
        creditNo: undefined,
        status: 0,
        onLineStatus: 0,
        apiUrl: undefined,
        remark: undefined,
        companyName: undefined,
        tel: undefined,
        contact: undefined,
        addr: undefined,
        companyType: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户端";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改客户端";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除biz编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delDevice(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
