<template>
  <div class="app-container">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('特性类型')" prop="type">
        <el-select v-model="queryParams.type" 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="$t('特性名称')" prop="name">
        <el-input v-model="queryParams.name" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('关联属性')" prop="field">
        <el-input v-model="queryParams.field" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('sap属性')" prop="sapField">
        <el-input v-model="queryParams.sapField" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</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)">{{ $t('common.add') }}</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)">{{ $t('common.edit') }}</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">{{ $t('common.del') }}</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">{{ $t('common.export') }}</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="$t('特性名称')" prop="name">
        <span slot-scope="{row}" :style="{marginLeft: (!row.hasChildren && row.parentId == 0) ? '25px' : '' }">
          {{ row.name }}
        </span>
      </el-table-column>
      <el-table-column :label="$t('特性类型')" 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="$t('数据类型')" 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="$t('关联属性')" prop="field" width="120"/>
      <el-table-column :label="$t('sap属性')" prop="sapField" width="120"/>
      <el-table-column :label="$t('是否必填')" prop="required" width="85">
        <template slot-scope="{row}">{{ row.required ? $t('是') : $t('否') }}</template>
      </el-table-column>
      <el-table-column :label="$t('是否显示')" prop="show" width="85">
        <template slot-scope="{row}">{{ row.show ? $t('是') : $t('否') }}</template>
      </el-table-column>
      <el-table-column :label="$t('错误提示语')" prop="errMsg"/>
      <el-table-column :label="$t('common.option')" 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)">{{ $t('common.edit') }}</el-button>
          <el-button v-hasPermi="['md:sapField:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)">{{ $t('common.del') }}</el-button>
          <el-button size="mini" type="text" @click="onShowPreview(row)">{{$t('预览')}}</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(this.$t('common.confirm_del_data')).then(() => {
        return delView(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(this.$t('删除成功'))
      }).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>
