<!--
 * @Author: Badguy
 * @Date: 2022-04-12 17:49:47
 * @LastEditTime: 2022-04-13 16:17:37
 * @LastEditors: your name
 * @Description: 源数据管理-首页
 * have a nice day!
-->

<template>
  <div class="sourceDataItem-home">
    <div class="wrap">
      <div class="protocol-header flex">
        <div class="manageTitle">源数据管理</div>
        <div class="addBtn">
          <el-button class="button-white" size="small" @click="showAddDialog()">新增</el-button>
        </div>
      </div>
      <div class="protocol-content">
        <el-row :gutter="24">
          <el-col v-for="item in dataList" :key="item.sourceDataID" :span="4">
            <div class="grid-content">
              <div class="head-line"></div>
              <div class="grid-content-hand flex">
                <div class="title flex-wrap">
                  <!-- <div class="name">{{ item.dataName }}</div> -->
                  <el-tooltip class="item" effect="dark" :content="item.dataName" placement="bottom">
                    <div class="name">{{ item.dataName }}</div>
                  </el-tooltip>
                  <i class="loger" @click="showEditDialog(item)" />
                </div>
                <i class="el-icon-close icon" @click="showRemoveDialog(item)" />
              </div>
              <div class="disc">{{item.dataTypeText}}</div>
            </div>
          </el-col>
        </el-row>
      </div>
       <!--新增/编辑-->
      <Dialog :flag="editDialogVisible" width="466px" :show-flag="true">
        <div class="airportInfoDialog dialog-public-background">
          <div class="title">{{ editDialogTitle }}</div>
          <div class="content">
            <el-form
              ref="ruleForm"
              :model="ruleForm"
              :rules="rules"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="源数据名称" prop="dataName">
                <el-input
                  v-model="ruleForm.dataName"
                  size="small"
                  placeholder="请输入源数据名称"
                />
              </el-form-item>
              <el-form-item label="数据格式" prop="dataType">
                <el-select
                  v-model="ruleForm.dataType"
                  size="small"
                  placeholder="请选择数据格式"
                >
                  <el-option label="JSON" value="1"></el-option>
                  <el-option label="XML" value="2"></el-option>
                  <el-option label=" REGULA" value="3"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="取值方法">
                <el-input
                  v-model="ruleForm.computingMethod"
                  size="small"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入取值方法"
                />
              </el-form-item>
              <el-form-item label="检测方法">
                <el-input
                  v-model="ruleForm.validationExpression"
                  size="small"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入检测方法"
                />
              </el-form-item>
              <el-form-item label="源数据描述">
                <el-input
                  v-model="ruleForm.dataDescribe"
                  size="small"
                  type="textarea"
                  :rows="2"
                  placeholder="请输入描述"
                />
              </el-form-item>
            </el-form>
          </div>
          <div class="foot center t30">
            <el-button
              size="medium"
              type="primary"
              class="r25 r26"
              @click="submitClickHandler()"
              >提交</el-button
            >
            <el-button size="medium" class="r26" @click="resetForm('ruleForm')"
              >取消</el-button
            >
          </div>
        </div>
      </Dialog>
      <!--删除弹框-->
      <Dialog :flag="removeDialogVisible">
        <div class="airportInfoDialog">
          <div class="del-title">删除协议</div>
          <div class="content er">
            <div class="log">是否确认删除{{ rmObj.protocolName }}？</div>
          </div>
          <div class="DelFoot right t30">
            <el-button size="medium" class="r25 r26" type="danger" @click="removeSubmit()">删除</el-button>
            <el-button size="medium" class="r26" @click="removeDialogVisible = false">取消</el-button>
          </div>
        </div>
      </Dialog>
    </div>
  </div>
</template>

<script>
import Dialog from "@/layout/components/Dialog/index.vue";
import { Query ,GeneralDataReception } from '@/api/dataIntegration';

export default {
  name: "ProtocolHome",
  components: { Dialog },
  data () {
    return {
      dataList: [],
      removeDialogVisible: false,
      rmObj: {},
      editDialogVisible:false,
      editDialogTitle:"目标数据新增",
      ruleForm: {
        // 信息表单
        dataName: "",
        dataDescribe: "",
        computingMethod: "",
        validationExpression:"",
        dataType: "",
      },
      rules: {
        // 机器信息表单验证
        dataName: [
          { required: true, message: "请输入源数据名称", trigger: "blur" },
        ],
        dataType: [
          { required: true, message: "请选择数据格式", trigger: "blur" },
        ],
      },
    };
  },
  created () {
    this.getList()
  },
  methods: {
    async getList() {
      try {
        const res = await Query({
          id: 4,
          dataContent: []
        });
        if (res.code === "0") {
          res.returnData.forEach(element => {
            if(element.dataType==1){
              element['dataTypeText'] = "JSON"
            }
            else if(element.dataType==2){
              element['dataTypeText'] = "XML"
            }
            else if(element.dataType==3){
              element['dataTypeText'] = "REGULA"
            }
            else{
              element['dataTypeText'] = "其他"
            }
          });
            this.dataList = res.returnData
        } else {
          this.$message.error(res.message ?? "失败");
        }
      } catch (error) {
        console.log("错误", error);
      }
    },
    // 新增-跳转
    showAddDialog () {
      this.editDialogTitle = "新增源数据";
      this.editDialogVisible = true;
    },
    // 编辑-跳转
    showEditDialog (item) {
      this.$router.push({path:'/systemSettings/sourceDataItemEdit',query:{id:item.sourceDataID}})
    },
    // 新增/编辑-确认
    submitClickHandler() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.addSubmit();
          this.resetForm("ruleForm");
        }
      });
    },
    async addSubmit() {
      this.ruleForm["operate"] = "1";
      try {
        const res = await GeneralDataReception({
          serviceId: "4",
          dataContent: JSON.stringify(this.ruleForm),
        });
        if (res.code === "0") {
          this.$message.success(res.message ?? "成功");
          this.getList()
        } else {
          this.$message.error(res.message ?? "失败");
        }
      } catch (error) {
        console.log("错误", error);
      }
    },
    // 删除-弹框
    showRemoveDialog (item) {
      this.rmObj = item;
      this.removeDialogVisible = true;
    },
    // 确认删除
    async removeSubmit () {
      this.rmObj["operate"] = "3";
      try {
        const res = await GeneralDataReception({
          serviceId: "4",
          dataContent:  JSON.stringify(this.rmObj),
        });
        if (res.code === "0") {
          this.removeDialogVisible = false;
          this.$message.success("删除成功");
          this.getList()
        } else {
          this.$message.error(res.message ?? "失败");
        }
      } catch (error) {
        console.log("错误", error);
      }
    },
    // 重置
    resetForm(formName) {
      this.ruleForm = {}
      this.$refs[formName].resetFields();
      this.editDialogVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.sourceDataItem-home {
  .wrap {
    padding: 16px 0;
    .protocol-header {
      line-height: 32px;
      font-size: 14px;
      .button-white {
        border: 1px solid #9ebbf7;
        background: #f5f7fa;
        -webkit-box-shadow: 0px 6px 7px 0px rgb(0 0 0 / 6%);
        box-shadow: 0px 6px 7px 0px rgb(0 0 0 / 6%);
        border-radius: 4px;
        font-family: Microsoft YaHei;
        font-weight: bold;
        color: #2d67e3;
      }
      margin-bottom: 30px;
    }
    .protocol-content {
      .grid-content {
        height: 144px;
        background: #ffffff;
        box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.1);
        border-radius: 4px;
        padding: 25px;
        margin-bottom: 24px;
        position: relative;
        .head-line {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 4px;
          background: #2d67e3;
          border-radius: 4px;
        }
        .disc {
          position: absolute;
          bottom: 25px;
          left: 25px;
          font-size: 14px;
          font-weight: 400;
          color: #101116;
        }
        .title {
          font-size: 16px;
          font-family: Helvetica;
          font-weight: bold;
          color: #101116;
          white-space: nowrap;
          max-width: 80%;
          text-overflow: ellipsis;
          overflow: hidden;
          .name {
            white-space: nowrap;
            max-width: 80%;
            text-overflow: ellipsis;
            overflow: hidden;
          }
          .loger {
            width: 14px;
            height: 14px;
            margin-left: 24px;
            background: url("../../../../assets/status/ic_edit_default.png")
              no-repeat;
            background-size: 100% 100%;
            cursor: pointer;
            position: relative;
            top: 2px;
          }
          .loger:hover {
            background: url("../../../../assets/status/ic_edit_hovar.png")
              no-repeat;
            background-size: 100% 100%;
          }
        }
        .icon {
          cursor: pointer;
          font-size: 16px;
          color: #606266;
        }
      }
    }
  }
}
</style>
