<template>
  <Navbar title="设备管理" :is-go-back="false" />
  <main>
    <a-button @click="editPrinter(true)">添加打印机</a-button>
    <CommonTable :columns="printerColumns" :data-source="printerDatas">
      <template #bodyCell="{ record, column }">
        <template v-if="column.key === 'supportRfid'">
          <span v-if="record.supportRfid === true"> 是 </span>
          <span v-else> 否 </span>
        </template>
        <template v-if="column.key === 'operation'">
          <span>
            <a @click="editPrinter(false, record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="deletePrinter(record.id)">删除</a>
          </span>
        </template>
      </template>
    </CommonTable>

    <a-modal
      v-model:open="editVisible"
      :title="editTitle"
      width="800px"
      ok-text="保存配置"
      cancel-text="取消"
      @ok="saveOrUpdateSetting"
    >
      <a-form :model="printerState">
        <a-row :gutter="24">
          <a-col :span="11">
            <a-form-item
              label="打印机名称"
              :label-col="{ span: 8 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-select
                v-model:value="printerState.printerName"
                style="width: 220px"
                :options="printerNames"
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item
              label="打印机类型"
              :label-col="{ span: 10 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-select
                v-model:value="printerState.printerType"
                style="width: 220px"
              >
                <a-select-option value="GK888">GK888</a-select-option>
                <a-select-option value="Zebra">Zebra</a-select-option>
                <a-select-option value="TOSHIBA">TOSHIBA</a-select-option>
                <a-select-option value="蓝牙打印机">蓝牙打印机</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="11">
            <a-form-item
              label="旋转角度"
              :label-col="{ span: 8 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-select v-model:value="printerState.rotateAngel">
                <a-select-option value="0">0</a-select-option>
                <a-select-option value="90">90</a-select-option>
                <a-select-option value="180">180</a-select-option>
                <a-select-option value="270">270</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item
              label="支持RFID功能"
              :label-col="{ span: 10 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-checkbox v-model:checked="printerState.supportRfid" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="11">
            <a-form-item
              label="打印深度"
              :label-col="{ span: 8 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-input-number
                v-model:value="printerState.darkness"
                style="width: 220px"
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item
              label="进纸方向偏移(mm)"
              :label-col="{ span: 10 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-input-number
                v-model:value="printerState.offsetHeight"
                style="width: 220px"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="11">
            <a-form-item
              label="标签后退距离"
              :label-col="{ span: 8 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-input-number
                v-model:value="printerState.backLength"
                style="width: 220px"
              />
            </a-form-item>
          </a-col>
          <a-col :span="11">
            <a-form-item
              label="水平方向偏移(mm)"
              :label-col="{ span: 10 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-input-number
                v-model:value="printerState.offsetWidth"
                style="width: 220px"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="11">
            <a-form-item
              label="打印机分辨率"
              :label-col="{ span: 8 }"
              :wrapper-col="{ span: 16 }"
            >
              <a-select
                v-model:value="printerState.printheads"
                style="width: 220px"
              >
                <a-select-option :value="6">152.4</a-select-option>
                <a-select-option :value="8">203.2</a-select-option>
                <a-select-option :value="12">304.8</a-select-option>
                <a-select-option :value="24">609.6</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
  </main>
</template>

<script setup>
import Common from '../common/Common';
import { message } from 'ant-design-vue';
import { tableColumns } from './configData.js';
import { ref, reactive, onMounted } from 'vue';
import CommonTable from '../common/CommonTable.vue';
import { getTableScroll } from '../common/tableScroll.js';
import PrintUtil from '../api/printer/printerWebsocket.js';
import {
  deleteById,
  saveOrUpdate,
  queryAllPrinters,
} from '../api/printer/printerConfiguration.js';

const editTitle = ref('');
const printerDatas = ref([]);
const printerNames = ref([]);
const editVisible = ref(false);
const printerColumns = ref(tableColumns);
const printerState = ref({
  printerName: '',
  printerType: undefined,
  supportRfid: false,
  rotateAngel: 0,
  backLength: 0,
  offsetHeight: 0,
  offsetWidth: 0,
  darkness: 30,
  printheads: 8,
});
const base = reactive({
  clientId: '',
  macAddress: '',
});
const createState = ref({});
// 编辑打印机配置
const editPrinter = (flag, record) => {
  editVisible.value = true;
  if (flag) {
    editTitle.value = '添加打印机';
    printerState.value = JSON.parse(JSON.stringify(createState.value));
  } else {
    editTitle.value = '编辑打印机';
    printerState.value = record;
  }
  getPrinterNames();
};

onMounted(() => {
  base.clientId = JSON.parse(localStorage.getItem('#LoginInfo')).loginClientId;
  createState.value = JSON.parse(JSON.stringify(printerState.value));
  getMacAddress();
  queryPrinters();
});

// 获取mac地址
const getMacAddress = () => {
  PrintUtil.getMacAddress().then(
    success => {
      base.macAddress = success.data;
    },
    err => {
      console.log(err);
    },
  );
};

// 获取所有打印机名称
const getPrinterNames = () => {
  PrintUtil.getPrinters().then(
    success => {
      if (success.errorCode === 0) {
        if (success.data && success.data.length > 0) {
          printerNames.value = success.data.map(item => {
            return (item = {
              value: item,
              label: item,
            });
          });
        }
      } else {
        message.warning(success.errorMessage);
      }
    },
    error => {
      console.log(error);
    },
  );
};

// 获取所有打印配置
const queryPrinters = () => {
  queryAllPrinters().then(
    success => {
      if (success.errorCode === 0) {
        if (success.datas && success.datas.length > 0) {
          printerDatas.value = success.datas;
        }
      } else {
        message.warning(success.errorMessage);
      }
    },
    err => {
      Common.processException(err);
    },
  );
};

//新增或更新打印配置
const saveOrUpdateSetting = () => {
  const printerInfo = JSON.parse(JSON.stringify(printerState.value));
  const params = { ...printerInfo, ...base };
  saveOrUpdate(params).then(
    success => {
      if (success.errorCode === 0) {
        if (editTitle.value === '添加打印机') {
          message.success('添加打印机配置成功。');
        } else {
          message.success('更新打印机配置成功。');
        }
        editVisible.value = false;
        queryPrinters();
      } else {
        message.warning(success.errorMessage);
      }
    },
    err => {
      Common.processException(err);
    },
  );
};

// 删除打印机配置
const deletePrinter = id => {
  deleteById(id).then(
    success => {
      if (success.errorCode === 0) {
        message.success('删除打印机配置成功。');
        queryPrinters();
      } else {
        message.warning(success.errorMessage);
      }
    },
    err => {
      Common.processException(err);
    },
  );
};
</script>

<style scoped>
</style>