<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="type">
        <el-select v-model="queryParams.type" placeholder="请选择特性类型" clearable>
          <el-option v-for="item in dict.type['sap_property_type']" :key="item.value" :label="item.label" :value="item.value"/>
        </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="关联属性" prop="field">
        <el-input v-model="queryParams.field" placeholder="请输入视图属性" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="sap属性" prop="sapField">
        <el-input v-model="queryParams.sapField" placeholder="请输入sap属性" 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 v-hasPermi="['md:sapField:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="showEdit(null)">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['md:sapField:edit']" type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="showEdit(ids)">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['md:sapField:remove']" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['md:sapField:export']" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="page.records" row-key="id" lazy :load="onLoad" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column label="特性名称" prop="name">
        <span slot-scope="{row}" :style="{marginLeft: (!row.hasChildren && row.parentId == 0) ? '25px' : '' }">
          {{ row.name }}
        </span>
      </el-table-column>
      <el-table-column label="特性类型" prop="type" width="120">
        <template slot-scope="{row}">
          {{ (dict.type['sap_property_type'].find(item => item.value == row.type) || {label: row.type}).label }}
        </template>
      </el-table-column>
      <el-table-column label="数据类型" prop="dataType" width="90">
        <template slot-scope="{row}">
          {{ (dict.type['sys_data_type'].find(item => item.value == row.dataType) || {label: row.dataType}).label }}
        </template>
      </el-table-column>
      <el-table-column label="关联属性" prop="field" width="120"/>
      <el-table-column label="sap属性" prop="sapField" width="120"/>
      <el-table-column label="是否必填" prop="required" width="85">
        <template slot-scope="{row}">{{ row.required ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column label="是否显示" prop="show" width="85">
        <template slot-scope="{row}">{{ row.show ? '是' : '否' }}</template>
      </el-table-column>
      <el-table-column label="错误提示语" prop="errMsg"/>
      <el-table-column label="操作" class-name="small-padding fixed-width" width="150">
        <template slot-scope="{row}">
          <el-button v-hasPermi="['md:sapField:edit']" size="mini" type="text" icon="el-icon-edit" @click="showEdit(row.id)">修改</el-button>
          <el-button v-hasPermi="['md:sapField:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
          <el-button size="mini" type="text" @click="onShowPreview(row)">预览</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="page.total>0" :total="page.total" :page.sync="queryParams.current" :limit.sync="queryParams.size" @pagination="getList"/>
    <edit-property ref="editProperty" @handleSubmit="getList"/>
    <el-dialog :visible.sync="showPreview" width="100%">
      <el-form>
        <el-row :gutter="10">
          <SapForm ref="sapForm"/>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { delView, pageView, listView } from '@/api/mes/md/sapField'
import EditProperty from '@/views/mes/md/sapField/edit'
import SapForm from '@/views/mes/md/sapField/components/SapForm'
export default {
  dicts: ['sap_property_type', 'sys_data_type'],
  components: {
    EditProperty,
    SapForm,
  },
  data() {
    return {
      loading: false,
      showPreview: false,
      page: { records: [], total: 0 },
      queryParams: { current: 1, size: 10, parentId: 0 },
      dictList: [],
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      open: false,
    }
  },
  methods: {
    /** 新增按钮操作 */
    showEdit(id) {
      this.$refs.editProperty.init(id)
    },
    onShowPreview(row) {
      this.showPreview = true
      this.$nextTick(() => this.$refs.sapForm.init(row.type, row.id))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除Sap特性视图编号为"' + ids + '"的数据项？').then(() => {
        return delView(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/view/export', {
        ...this.queryParams
      }, `view_${new Date().getTime()}.xlsx`)
    },
    onLoad(tree, treeNode, resolve) {
      listView({parentId: tree.id}).then(res => resolve(res.data))
    },
    /** 查询Sap特性视图列表 */
    getList() {
      this.loading = true
      pageView(this.queryParams).then(response => {
        this.page.records = response.data.rows
        this.page.total = response.data.total
      }).finally(() => this.loading = false)
    },
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  },
  created() {
    this.getList()
  }
}
</script>
