<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仪器类型" prop="model">
        <el-select v-model="queryParams.type" @keyup.enter.native="handleQuery" style="width: 50%">
          <el-option label="全部" :value="null"></el-option>
          <el-option label="非IOT仪器" :value="1"></el-option>
          <el-option label="IOT仪器" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仪器名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入仪器名称进行搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入仪器ID进行搜索"
          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="['system:instrument:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="instrumentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" fixed="left" width="55" align="center" />
      <el-table-column label="升序排序" align="center" prop="sortNo" />
      <el-table-column label="仪器ID" align="center" prop="id" />
      <el-table-column label="仪器类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-show="scope.row.type == 1">普通仪器</span>
          <span v-show="scope.row.type == 2">iot仪器</span>
        </template>
      </el-table-column>
      <el-table-column label="仪器封面" align="center" prop="banner" width="150px">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="100" :src="scope.row.banner"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="仪器LOGO" align="center" prop="logo" width="150px">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="100" :src="scope.row.logo"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="仪器名称" align="center" prop="name" width="150px"/>
      <el-table-column label="仪器型号" align="center" prop="model" />
      <el-table-column label="保修日期" align="center" prop="guarantee" >
        <template slot-scope="scope">
          <span v-show="scope.row.guarantee != null">{{scope.row.guarantee}}年</span>
        </template>
      </el-table-column>
      <el-table-column label="小程序标签" align="center" prop="miniTagNames" width="150px">
        <template slot-scope="scope">
          <el-tag v-for="item in scope.row.miniTagNames.split(',')">
            {{ item }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="企微标签" align="center" prop="wecomTagNames" width="150px">
        <template slot-scope="scope">
          <el-tag v-for="item in scope.row.wecomTagNames.split(',')">
            {{ item }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="日常打卡奖励" align="center" prop="dailyClockCredit" />
      <el-table-column label="额外打卡奖励" align="center" prop="extraClockCredit" />
      <el-table-column label="操作" align="center" width="200px" fixed="right" 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="['system:instrument:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >设置小程序可见标签</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >设置微信可见标签</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >正品控名称配置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >配置说明书</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:instrument: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"
    />

    <!-- 添加或修改仪器列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仪器名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入仪器名称" />
        </el-form-item>
        <el-form-item label="iot仪器特有：仪器型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入iot仪器特有：仪器型号" />
        </el-form-item>
        <el-form-item label="仪器封面" prop="banner">
          <el-input v-model="form.banner" placeholder="请输入仪器封面" />
        </el-form-item>
        <el-form-item label="仪器logo图" prop="logo">
          <el-input v-model="form.logo" placeholder="请输入仪器logo图" />
        </el-form-item>
        <el-form-item label="序列号位置图片" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入序列号位置图片" />
        </el-form-item>
        <el-form-item label="保修日期" prop="guarantee">
          <el-input v-model="form.guarantee" placeholder="请输入保修日期" />
        </el-form-item>
        <el-form-item label="日常打卡获得积分" prop="dailyClockCredit">
          <el-input v-model="form.dailyClockCredit" placeholder="请输入日常打卡获得积分" />
        </el-form-item>
        <el-form-item label="额外打卡奖励积分" prop="extraClockCredit">
          <el-input v-model="form.extraClockCredit" placeholder="请输入额外打卡奖励积分" />
        </el-form-item>
        <el-form-item label="额外打卡时间开始" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择额外打卡时间开始">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="额外打卡时间结束" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择额外打卡时间结束">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="标签ID(xxxx,xxxx,xxx)" prop="miniTagIds">
          <el-input v-model="form.miniTagIds" placeholder="请输入标签ID(xxxx,xxxx,xxx)" />
        </el-form-item>
        <el-form-item label="标签名字(xxx,xxx,xxx)" prop="miniTagNames">
          <el-input v-model="form.miniTagNames" placeholder="请输入标签名字(xxx,xxx,xxx)" />
        </el-form-item>
        <el-form-item label="标签ID(xxxx,xxxx,xxx)" prop="wecomTagIds">
          <el-input v-model="form.wecomTagIds" placeholder="请输入标签ID(xxxx,xxxx,xxx)" />
        </el-form-item>
        <el-form-item label="标签名称(xxx,xxx,xxx)" prop="wecomTagNames">
          <el-input v-model="form.wecomTagNames" placeholder="请输入标签名称(xxx,xxx,xxx)" />
        </el-form-item>
        <el-form-item label="场景ID(xxxx,xxxx,xxx)" prop="sceneIds">
          <el-input v-model="form.sceneIds" placeholder="请输入场景ID(xxxx,xxxx,xxx)" />
        </el-form-item>
        <el-form-item label="场景名称(xxx,xxx,xxx)" prop="sceneNames">
          <el-input v-model="form.sceneNames" placeholder="请输入场景名称(xxx,xxx,xxx)" />
        </el-form-item>
        <el-form-item label="护理时长" prop="nursingTime">
          <el-date-picker clearable
            v-model="form.nursingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择护理时长">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="iot版本" prop="iotVersion">
          <el-input v-model="form.iotVersion" placeholder="请输入iot版本" />
        </el-form-item>
        <el-form-item label="iot升级数据文件" prop="iotUpgradeData">
          <el-input v-model="form.iotUpgradeData" placeholder="请输入iot升级数据文件" />
        </el-form-item>
        <el-form-item label="是否开启扫机身码：0、关闭 1、开启" prop="isScanCode">
          <el-input v-model="form.isScanCode" placeholder="请输入是否开启扫机身码：0、关闭 1、开启" />
        </el-form-item>
        <el-form-item label="是否支持购买：0、不支持 1、支持" prop="isPurchase">
          <el-input v-model="form.isPurchase" placeholder="请输入是否支持购买：0、不支持 1、支持" />
        </el-form-item>
        <el-form-item label="商城小程序appid" prop="shoppingAppid">
          <el-input v-model="form.shoppingAppid" placeholder="请输入商城小程序appid" />
        </el-form-item>
        <el-form-item label="商城小程序path" prop="shoppingPath">
          <el-input v-model="form.shoppingPath" placeholder="请输入商城小程序path" />
        </el-form-item>
        <el-form-item label="蓝牙连接中图片" prop="bluetoothConnecting">
          <el-input v-model="form.bluetoothConnecting" placeholder="请输入蓝牙连接中图片" />
        </el-form-item>
        <el-form-item label="蓝牙关闭图片" prop="bluetoothClosed">
          <el-input v-model="form.bluetoothClosed" placeholder="请输入蓝牙关闭图片" />
        </el-form-item>
        <el-form-item label="iot版本升级说明" prop="iotVersionUpgrade">
          <el-input v-model="form.iotVersionUpgrade" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="排序:值越大，排序越前" prop="sortNo">
          <el-input v-model="form.sortNo" placeholder="请输入排序:值越大，排序越前" />
        </el-form-item>
        <el-form-item label="手动绑定介绍图片" prop="manualCodeBinding">
          <el-input v-model="form.manualCodeBinding" placeholder="请输入手动绑定介绍图片" />
        </el-form-item>
        <el-form-item label="扫码绑定介绍图片" prop="scanCodeBinding">
          <el-input v-model="form.scanCodeBinding" placeholder="请输入扫码绑定介绍图片" />
        </el-form-item>
        <el-form-item label="${comment}" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInstrument, getInstrument, delInstrument, addInstrument, updateInstrument } from "@/api/system/instrument";

export default {
  name: "Instrument",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仪器列表格数据
      instrumentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        name: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "仪器名称不能为空", trigger: "blur" }
        ],
        model: [
          { required: true, message: "iot仪器特有：仪器型号不能为空", trigger: "blur" }
        ],
        banner: [
          { required: true, message: "仪器封面不能为空", trigger: "blur" }
        ],
        logo: [
          { required: true, message: "仪器logo图不能为空", trigger: "blur" }
        ],
        serial: [
          { required: true, message: "序列号位置图片不能为空", trigger: "blur" }
        ],
        guarantee: [
          { required: true, message: "保修日期不能为空", trigger: "blur" }
        ],
        sceneIds: [
          { required: true, message: "场景ID(xxxx,xxxx,xxx)不能为空", trigger: "blur" }
        ],
        nursingTime: [
          { required: true, message: "护理时长不能为空", trigger: "blur" }
        ],
        manualCodeBinding: [
          { required: true, message: "手动绑定介绍图片不能为空", trigger: "blur" }
        ],
        scanCodeBinding: [
          { required: true, message: "扫码绑定介绍图片不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询仪器列列表 */
    getList() {
      this.loading = true;
      listInstrument(this.queryParams).then(response => {
        this.instrumentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        model: null,
        banner: null,
        logo: null,
        serial: null,
        guarantee: null,
        dailyClockCredit: null,
        extraClockCredit: null,
        startTime: null,
        endTime: null,
        miniTagIds: null,
        miniTagNames: null,
        wecomTagIds: null,
        wecomTagNames: null,
        sceneIds: null,
        sceneNames: null,
        nursingTime: null,
        iotVersion: null,
        iotUpgradeData: null,
        isScanCode: null,
        isPurchase: null,
        shoppingAppid: null,
        shoppingPath: null,
        bluetoothConnecting: null,
        bluetoothClosed: null,
        iotVersionUpgrade: null,
        sortNo: null,
        status: null,
        manualCodeBinding: null,
        scanCodeBinding: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      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.reset();
      const id = row.id || this.ids
      getInstrument(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仪器列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInstrument(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInstrument(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除仪器列编号为"' + ids + '"的数据项？').then(function() {
        return delInstrument(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
