<template>
  <folder-page type="element" @click="folderHandleSelected" ref="folder">
    <div class="header">
      <el-button icon="el-icon-circle-plus-outline" size="mini" @click="addElementData">创建元素</el-button>
      <div class="searchInput">
        <i class="el-icon-search"></i>
        <el-input class="input" v-model="searchForm.name" placeholder="根据名称搜索" @blur="searchName"></el-input>
      </div>
    </div>
    <div class="list">
      <el-table :data="elementList">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="50" align="center" />
        <el-table-column prop="name" label="元素名称" min-width="150" align="center" />
        <el-table-column prop="locType" label="定位类型" min-width="150" align="center" />
        <el-table-column prop="elementLoc" label="元素定位" min-width="150" align="center" />
        <el-table-column prop="groupNamePath" label="所属模块" min-width="150" align="center" />
        <el-table-column prop="createBy" label="创建人" min-width="150" sortable align="center" />
        <el-table-column prop="createTime" label="创建时间" min-width="180" align="center" sortable />
        <el-table-column prop="updateBy" label="更新人" min-width="150" sortable align="center" />
        <el-table-column prop="updateTime" label="更新时间" min-width="180" align="center" sortable />
        <el-table-column prop="action" label="操作" align="center" fixed="right" width="150px">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit-outline" @click="hadleClickEdit(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" style="color: red;"
              @click="hadleClickDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-size="searchForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" />
    </div>
    <!-- 创建元素、编辑元素 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <div class="form">
        <el-form :model="editForm" :rules="rules" label-position="right" label-width="80px">
          <el-form-item label="元素名称" prop="name">
            <el-input v-model="editForm.name" placeholder="请输入元素名称"></el-input>
          </el-form-item>
          <el-form-item label="所属模块" prop="groupId">
            <el-select v-model="editForm.groupId" placeholder="请选择">
              <el-option v-for="item in groupList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="定位类型" prop="locType">
            <el-select v-model="editForm.locType" placeholder="请选择">
              <el-option v-for="(item, index) in typeList" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="元素定位" prop="elementLoc">
            <el-input v-model="editForm.elementLoc" placeholder="请输入元素定位"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="description">
            <el-input v-model="editForm.description" placeholder="请输入备注" type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleWithAdd()">确 定</el-button>
      </span>
    </el-dialog>
  </folder-page>
</template>

<script>
import FolderPage from "@/components/FolderPage/index.vue"
import { listGroup } from "../../../api/test/group";
import { getElementList, addElement, deleteElement, updateElement } from "../../../api/uiTest/elementLibrary";

export default {
  name: "ElementLibrary.vue",
  components: { FolderPage },
  data() {
    return {
      elementList: [],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        groupId: null,
      },
      total: 0,
      dialogTitle: '',
      dialogVisible: false,
      editForm: {
        name: null, // 元素名称
        groupId: null,  // 所属模块
        locType: null, // 定位类型
        elementLoc: null, // 元素定位
        description: null, // 备注
      },
      typeList: ['id', 'name', 'className', 'tagName', 'linkText', 'partialLinkText', 'css', 'xpath', 'lable', 'value', 'index'],
      rules: {
        name: [{ required: true, message: '请输入元素名称', trigger: 'blur' }],
        groupId: [{ required: true, message: '请选择所属模块', trigger: 'blur' }],
        locType: [{ required: true, message: '请选择定位类型', trigger: 'blur' }],
        elementLoc: [{ required: true, message: '请输入元素定位', trigger: 'blur' }],
      },
      deleteVisible: false,
      groupList: [], // 分组
    }
  },
  mounted() {
    this.getElementData()
    this.getGroupListData()
  },
  methods: {
    folderHandleSelected(id) {
      if (id) {
        // 获取列表
        this.searchForm.groupId = id;
        this.getElementData();
      } else {
        this.elementList = [];
      }
    },
    // 
    getGroupListData() {
      listGroup('element').then(res => {
        if (res.code === 200) {
          this.groupList = res.data
        }
      })
    },
    // 获取元素
    getElementData() {
      getElementList(this.searchForm).then(res => {
        if (res.code === 200) {
          this.elementList = res.rows
          this.total = res.total
        }
      })
    },
    // 搜索
    searchName() {
      this.getElementData();
    },
    // 创建元素
    addElementData() {
      this.dialogTitle = '创建元素'
      this.dialogVisible = true
      for (let key in this.editForm) {
        this.editForm[key] = null
      }
    },
    // 编辑
    hadleClickEdit(val) {
      this.dialogTitle = '编辑元素'
      this.dialogVisible = true
      this.editForm = val
    },
    // 删除
    hadleClickDelete(val) {
      this.$modal.confirm('确认删除元素' + '？').then(() => {
        deleteElement(val.id).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("删除成功");
            this.getElementData();
          } else {
            this.$modal.msgError("删除失败");
          }
        })
      }).catch(() => { })
    },
    // 确定
    handleWithAdd() {
      if (this.dialogTitle === '创建元素') {
        // 创建元素
        addElement(this.editForm).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("创建成功")
            this.dialogVisible = false
            this.getElementData()
          } else {
            this.$modal.msgError("创建失败");
          }
        })
      } else {
        // 编辑元素
        updateElement(this.editForm).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess("编辑成功");
            this.dialogVisible = false
            this.getElementData()
          } else {
            this.$modal.msgError("编辑失败");
          }
        })
      }
    },
    // 分页
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getElementData();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val
      this.getElementData();
    },
  },
}

</script>



<style scoped lang="scss">
.header {
  display: flex;
  justify-content: space-between;

  .searchInput {
    display: flex;
    align-items: center;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    padding: 0 10px;

    .input {
      width: 200px;
    }

    /* 去除 el-input 的边框 */
    ::v-deep .el-input__inner {
      border: none !important;
    }
  }
}

.list {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  align-items: flex-end;
}
</style>
