<template>
  <div id="containt">
    <el-tabs v-model="activeName" class="tab-box">
      <el-tab-pane :label="$t1('常规')" name="first">
        <editView v-if="showEdit" visible-key="showEdit" :_dataId.sync="dataId" :parent-target="_self"
                  @reload="$reloadHandle"></editView>
        <itemEditView v-if="showItemEdit" visible-key="showItemEdit" :_dataId.sync="itemEditId" :param="pRow"
                      :parent-target="_self" @reload="$reloadHandle"></itemEditView>
      </el-tab-pane>
      <el-tab-pane :label="$t1('列表')" name="second">
        <div class="grid-height">
          <div style="height: 345px;">
            <vxe-grid
              ref="table-m1"
              :auto-resize="true"
              v-bind="vxeOption"
              @resizable-change="$vxeTableUtil.onColumnWitchChange"
              @custom="$vxeTableUtil.customHandle"
              @checkbox-change="chooseCellM1"
              class="is-pointer"
            >
              <template #form>
                <div style="margin-bottom: 8px;" class="clearfix">
                  <div class="fl">
                    <vxe-button status="primary" class="button-sty" icon="el-icon-plus" @click="openEditDialog">
                      {{ $t1('新增属性名') }}
                    </vxe-button>
                  </div>
                  <div class="fr">
                    <vxe-button icon="el-icon-brush" class="button-sty" @click="resetEvent" type="text" status="primary"
                                plain>{{ $t1('重置') }}
                    </vxe-button>
                    <vxe-button status="warning" icon="el-icon-search" class="button-sty" @click="searchEvent">
                      {{ $t1('搜索') }}
                    </vxe-button>
                  </div>
                </div>
                <vxe-form ref="form" class="screen-box" :data-form="formData" title-width="92px" title-align="right"
                          @submit="searchEvent" @reset="searchEvent">
                  <vxe-form-item :title="$t1('属性名')+':'" field="name">
                    <template v-slot="">
                      <el-input v-model="formData.name" size="small" clearable/>
                    </template>
                  </vxe-form-item>
                  <vxe-form-item :title="$t1('属性编号')+':'" field="code">
                    <template v-slot="">
                      <el-input v-model="formData.code" size="small" clearable/>
                    </template>
                  </vxe-form-item>
                </vxe-form>
              </template>
            </vxe-grid>
          </div>
          <label id="labBtn" class="transverse">
            <div class="icon">
              <i class="el-icon-more"></i>
              <i class="el-icon-more"></i>
            </div>
          </label>
          <div style="height: calc(100vh - 345px - 105px);">
            <vxe-grid ref="table-m2" v-bind="vxeOption1" @resizable-change="$vxeTableUtil.onColumnWitchChange"
                      @custom="$vxeTableUtil.customHandle">
              <template #row_edit>
                <a href="#" class="a-link"><i class="el-icon-edit"/></a>
              </template>
            </vxe-grid>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import {treeScolly} from '@base/utils/global.js';
import editView from './edit.vue';
import itemEditView from './itemEdit.vue';

export default {
  name: 'common_attribute:list',
  components: {
    editView,
    itemEditView
  },
  created() {
  },
  mounted() {
    treeScolly({target: this, type: 'default'});
    this.initTableList();
    this.initTableM2();
  },
  data() {
    return {
      dataId: 0,
      showEdit: false,
      showItemEdit: false,
      vxeOption: {},
      vxeOption1: {},
      formData: {},
      editId: '',
      itemEditId: '',
      pRow: '',
      activeName: 'second',
      currentRow: {}
    };
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
      this.chooseCellM1({row: {}});
    },
    resetEvent() {
      this.formData = {};
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.showItemEdit = false;
      this.$openEditView('showEdit');
    },
    openEditItemDialog(id, currentRow) {
      if (id) {
        this.pRow = '';
        this.itemEditId = id;
        this.activeName = 'first';
        this.showEdit = false;
        this.$openEditView('showItemEdit');
      } else if (currentRow) {
        if (currentRow && currentRow.name) {
          this.pRow = currentRow;
          this.itemEditId = '';
          this.activeName = 'first';
          this.showEdit = false;
          this.$openEditView('showItemEdit');
        }
      }
    },
    initTableList() {
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'basic_common_attribute_list-m1',
        path: USER_PREFIX + '/common_attribute/listPage',
        param: () => {
          return {
            ...this.formData,
            enabled: true
          };
        },
        config: {
          checkboxConfig: {
            checkStrictly: true,
            showHeader: false,
            trigger: 'row'
          }
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('属性名'),
            field: 'name',
            width: 150,
            fixed: 'left'
          },
          {
            title: this.$t1('属性编号'),
            field: 'code',
            width: 150
          },
          {
            title: this.$t1('创建时间'),
            field: 'createDate',
            width: 150
          },
          {
            width: 180,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row}) => {
                return [
                  <div>
                    <a
                      class="a-link"
                      onclick={() => {
                        this.openEditItemDialog('', row);
                      }}
                    >
                      <el-tooltip enterable={false} effect="dark" content={this.$t1('新增属性值')} placement="top"
                                  popper-class="tooltip-skin">
                        <div>
                          <i class="el-icon-circle-plus-outline"/>
                          <span>{this.$t1('新增属性值')}</span>
                        </div>
                      </el-tooltip>
                    </a>
                    <a
                      href="javascript:void(0);"
                      class="a-link"
                      onclick={() => {
                        this.openEditDialog(row.id);
                      }}
                    >
                      <el-tooltip enterable={false} effect="dark" content={this.$t1('查看')} placement="top"
                                  popper-class="tooltip-skin">
                        <i class="el-icon-edit"/>
                      </el-tooltip>
                    </a>
                  </div>
                ];
              }
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
      });
    },
    initTableM2() {
      var that = this;
      let tableOption1 = {
        vue: that,
        tableRef: 'table-m2',
        tableName: 'basic_common_attribute_list-m2',
        config: {
          maxHeight: ''
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('属性值'),
            field: 'value',
            width: 250,
            fixed: 'left',
            slots: {
              default: ({row}) => {
                return [<div class="lineHeight">{row.value}</div>];
              }
            }
          },
          {
            title: this.$t1('属性值编号'),
            field: 'sn',
            width: 150,
            slots: {
              default: ({row}) => {
                return [<div class="lineHeight">{row.sn}</div>];
              }
            }
          },
          {
            field: 'enabled',
            title: this.$t1('是否启用'),
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.enabled) {
                  return [<div class="txt-status">{this.$t1('启用')}</div>];
                } else {
                  return [<div class="txt-status s-3">{this.$t1('禁用')}</div>];
                }
              }
            }
          },
          {
            title: this.$t1('属性名'),
            field: 'name',
            width: 250
          },
          {
            title: this.$t1('属性编号'),
            field: 'code',
            width: 150
          },
          {
            title: this.$t1('组织编码'),
            field: 'exeCompanyCode',
            width: 250
          },
          {
            title: this.$t1('序号'),
            field: 'orders',
            width: 150
          },
          {
            title: this.$t1('参数1'),
            field: 'param1',
            width: 150
          },
          {
            title: this.$t1('参数2'),
            field: 'param2',
            width: 150
          },
          {
            title: this.$t1('参数3'),
            field: 'param3',
            width: 150
          },
          {
            title: this.$t1('参数4'),
            field: 'param4',
            width: 150
          },
          {
            title: this.$t1('参数5'),
            field: 'param5',
            width: 150
          },
          {
            title: this.$t1('备注'),
            field: 'remark',
            width: 150
          },
          {
            title: this.$t1('创建时间'),
            field: 'createDate',
            width: 150
          },
          {
            width: 47,
            fixed: 'right',
            sortable: false,
            title: '',
            slots: {
              default: ({row}) => {
                return [
                  <a
                    href="javascript:void(0);"
                    class="a-link"
                    onclick={() => {
                      this.openEditItemDialog(row.id);
                    }}
                  >
                    <el-tooltip enterable={false} effect="dark" content={this.$t1('查看')} placement="top"
                                popper-class="tooltip-skin">
                      <i class="el-icon-edit"/>
                    </el-tooltip>
                  </a>
                ];
              }
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption1).then(opts => {
        that.vxeOption1 = opts;
      });
    },
    chooseCellM1({row}) {
      if (row && row.id) {
        let $table1 = this.$refs['table-m1'];
        this.$nextTick(() => {
          $table1.clearCheckboxRow();
          $table1.setCheckboxRow(row, true);
        });
        this.currentRow = row;
        this.$http({
          method: 'post',
          url: USER_PREFIX + '/common_attribute/listItems',
          data: {code: row.code || ''},
          success: res => {
            let rows = res.objx || [];
            rows.forEach(function (item) {
              item.name = row.name;
            });
            this.$refs['table-m2'].loadData(rows);
          }
        });
      } else {
        this.$refs['table-m2'].loadData([]);
      }
    }
  }
};
</script>
