<script>
import {selectDeviceList,editDevice, removeDevice, saveDevice} from "../../../api/cdc/device";
import dayjs from "dayjs";

const tableId = "2000001"
const tableName = "设备信息"
const table1 = "2000001Table1"
export default {
  name: "device",
  props:{
    height:{
      type:Number,
      default:700
    },
    component:{
      type:Boolean,
      default: false
    }
  },
  data(){
    return{
      // 遮罩层
      loading: true,
      // 设备信息表格数据
      infoList: [],
      // 弹出层标题
      title: "设备信息",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        deviceNo: '',
        deviceDesc: null,
        deviceType: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceDesc: [
          { required: true, message: "设备描述不能为空", trigger: "blur" }
        ],
        deviceType: [
          { required: true, message: "设备类型不能为空", trigger: "change" }
        ],
        deviceIp: [
          { required: true, message: "采集HTTP信息不能为空", trigger: "blur" }
        ],
        deviceNo: [
          { required: true, message: "设备号不能为空", trigger: "blur" }
        ],
      },
      deviceColumns:[
        {
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}DeviceNo`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'deviceNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '设备号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}DeviceDesc`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'deviceDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '设备描述',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}DeviceType`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'deviceType',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '设备类型',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}AddressDesc`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'addressDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '位置描述',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}DeviceIp`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'deviceIp',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '采集HTTP信息',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}Active`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'active',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '在用',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}CreateBy`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'createBy',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '录入人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },{
          userId: this.$store.state.user.name,
          functionId: tableId,
          serialNumber: `${table1}CreateTime`,
          tableId: table1,
          tableName: tableName,
          columnProp: 'createTime',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '录入时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
      ],
    }
  },
  methods:{
    /** 查询设备信息列表 */
    getList() {
      let params = {
        ...this.queryParams,
        site:this.$store.state.user.site
      }
      this.loading = true;
      selectDeviceList(params).then(({data}) => {
        if (data && data.code === 0){
          this.infoList = data.rows;
        }
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceNo: '',
        site: null,
        deviceDesc: null,
        deviceType: '',
        addressDesc: null,
        deviceIp: null,
        deviceAddress: null,
        active: 'Y',
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {...row}
      this.form.newDeviceNo = this.form.deviceNo;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid,obj) => {
        if (valid) {
          if (!this.form.newDeviceNo) {
            this.form.site = this.$store.state.user.site;
            this.form.createBy = this.$store.state.user.name;
            this.form.createTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
            saveDevice(this.form).then(({data}) => {
              this.form.site = null;
              if (data && data.code === 0){
                this.$message.success(data.msg);
                this.open = false;
                this.getList();
              }else {
                this.$message.warning(data.msg);
              }
            }).catch((error)=>{
              this.$message.error(error);
            });
          } else {
            editDevice(this.form).then(({data}) => {
              if (data && data.code === 0){
                this.$message.success(data.msg);
                this.open = false;
                this.getList();
              }else {
                this.$message.warning(data.msg);
              }
            }).catch((error)=>{
              this.$message.error(error);
            });
          }
        }else {
          Object.keys(obj).forEach((key)=>{
            this.$message.warning(obj[key][0].message);
            return
          })
        }
      });
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该设备信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeDevice(row).then(({data})=>{
          if (data && data.code === 0){
            this.$message.success(data.msg);
            this.getList();
          }else {
            this.$message.success(data.msg);
          }
        }).catch((error)=>{
          this.$message.error(error);
        })
      }).catch(() => {
      });
    },
  },
  watch:{
    "queryParams.deviceNo"(newVal, oldVal){
      this.queryParams.deviceNo = newVal.toUpperCase();
    },
    "form.deviceNo"(newVal, oldVal){
      this.form.deviceNo = newVal.toUpperCase();
    },
  },
  created() {
    this.getList();
  }
}
</script>

<template>
  <div class="app-container">
    <el-form :model="queryParams" label-position="top" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="设备号" prop="deviceNo">
        <el-input
          v-model="queryParams.deviceNo"
          placeholder="请输入设备号"
          clearable
        />
      </el-form-item>
      <el-form-item label="设备描述" prop="deviceDesc">
        <el-input
          v-model="queryParams.deviceDesc"
          placeholder="请输入设备描述"
          clearable
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型"  >
          <el-option label="全部" value=""/>
          <el-option label="涂布机器-大线" value="涂布机器-大线"/>
          <el-option label="涂布机器-小线" value="涂布机器-小线"/>
          <el-option label="涂布机器-浓度-大线" value="涂布机器-浓度-大线"/>
          <el-option label="涂布机器-浓度-小线" value="涂布机器-浓度-小线"/>
          <el-option label="配胶间设备" value="配胶间设备"/>
        </el-select>
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="handleQuery" plain>查询</el-button>
        <el-button type="primary" @click="handleAdd" v-if="!component">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" border :data="infoList" :height="height">
      <el-table-column label="操作" align="center" v-if="!component">
        <template slot-scope="scope">
          <el-link @click="handleUpdate(scope.row)">编辑</el-link>
          <el-link @click="handleDelete(scope.row)">删除</el-link>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in deviceColumns" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed === ''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改设备信息对话框 -->
    <el-dialog :title="title" v-if="!component" v-drag :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" label-position="top" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="设备号" prop="deviceNo" :show-message="false">
              <el-input v-model="form.deviceNo" :disabled="form.newDeviceNo" placeholder="请输入设备号" />
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="设备描述" prop="deviceDesc" :show-message="false">
              <el-input v-model="form.deviceDesc" placeholder="请输入设备描述" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="设备类型" prop="deviceType" :show-message="false">
              <el-select v-model="form.deviceType" placeholder="请选择设备类型">
                <el-option label="涂布机器-大线" value="涂布机器-大线"/>
                <el-option label="涂布机器-小线" value="涂布机器-小线"/>
                <el-option label="涂布机器-浓度-大线" value="涂布机器-浓度-大线"/>
                <el-option label="涂布机器-浓度-小线" value="涂布机器-浓度-小线"/>
                <el-option label="配胶间设备" value="配胶间设备"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="位置描述" prop="addressDesc" :show-message="false">
              <el-input v-model="form.addressDesc" placeholder="请输入位置描述" />
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="采集IP" prop="deviceIp" :show-message="false">
              <el-input v-model="form.deviceAddress" placeholder="请输入采集IP" />
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="采集HTTP信息" prop="deviceIp" :show-message="false">
              <el-input v-model="form.deviceIp" placeholder="请输入采集HTTP信息" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label=" " prop="active" :show-message="false">
              <el-checkbox v-model="form.active" true-label="Y" false-label="N">在用</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
      </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>

<style scoped>

</style>
