<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>
      </el-tab-pane>
      <el-tab-pane :label="$t1('列表')" name="second">
        <div class="tree-box fl">
          <div class="tit"><b>{{ $t1('数据类型') }}</b></div>
          <div class="tree-btns" style="margin-top: 10px;">
            <el-button type="primary" plain icon="el-icon-circle-plus-outline" class="btn"
                       @click="addDataType()">{{ $t1('新增') }}
            </el-button>
            <el-button type="primary" plain icon="el-icon-edit-outline" class="btn" @click="editDataType()"
                       :disabled="!currentDataType || !currentDataType.dataTypeCode">{{ $t1('编辑') }}
            </el-button>
            <el-button type="primary" plain icon="el-icon-delete" class="btn" @click="deleteDataType()"
                       :disabled="!currentDataType || !currentDataType.dataTypeCode">{{ $t1('删除') }}
            </el-button>
          </div>
          <el-tree
            :props="defaultProps"
            :load="loadNode"
            node-key="id"
            ref="tree"
            highlight-current
            lazy
            :expand-on-click-node="false"
            @node-click="handleNodeClick"
            class="tree-list"
            icon-class="el-icon-arrow-down"
          ></el-tree>
        </div>
        <label id="labBtn">
          <div class="icon">
            <i class="el-icon-more"></i>
            <i class="el-icon-more"></i>
          </div>
        </label>
        <div class="main-right fr" style="padding-left:6px;">
          <div class="grid-height">
            <vxe-grid ref="table-m1" v-bind="vxeOption" @resizable-change="$vxeTableUtil.onColumnWitchChange"
                      @custom="$vxeTableUtil.customHandle">
              <template #form>
                <div class="clearfix screen-btns">
                  <div class="fl">
                    <vxe-button status="primary" class="button-sty" icon="el-icon-plus" @click="openEditDialog">
                      {{ $t1('新增') }}
                    </vxe-button>
                    <base-table-export :option="{ title: $t1('移动端单据页定义'), targetRef: 'table-m1'}"
                                       :parent-target="_self"/>
                  </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" title-width="92px" title-align="right" :data="formData"
                          @submit="searchEvent" @reset="searchEvent">
                  <vxe-form-item :title="$t1('数据类型')+':'" field="dataTypeName">
                    <template v-slot>
                      <el-input v-model="currentDataType.dataTypeName" size="small" disabled/>
                    </template>
                  </vxe-form-item>
                  <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-item :title="$t1('备注')+':'" field="remark">
                    <template v-slot>
                      <el-input v-model="formData.remark" size="small" clearable/>
                    </template>
                  </vxe-form-item>

                  <vxe-form-item :title="$t1('是否启用')+':'" field="enabled">
                    <template v-slot>
                      <el-select v-model="formData.enabled" clearable>
                        <el-option :value="true" :label="$t1('启用')"></el-option>
                        <el-option :value="false" :label="$t1('禁用')"></el-option>
                      </el-select>
                    </template>
                  </vxe-form-item>
                </vxe-form>
              </template>
            </vxe-grid>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <dataTypeEditDialog v-if="showDataTypeEditDialog" :visiable.sync="showDataTypeEditDialog"
                        :_dataId.sync="editDataTypeId" :dataTypeGroup="dataTypeGroup"
                        @confirm="confirmDataTypeEditDialog"></dataTypeEditDialog>
  </div>
</template>

<script>
import editView from './edit.vue';
import dataTypeEditDialog from "../../../views/user/data_type_setting/editDialog";
import {treeScollx} from '@base/utils/global.js';

export default {
  name: 'bill_setting:list',
  components: {editView, dataTypeEditDialog},
  data() {
    return {
      activeName: 'second',
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      formData: {},

      defaultProps: {
        label: 'dataTypeName', //这里是树结构中需显示的数据（即接口返回的需展示在页面上的参数）
        children: [],
        isLeaf: 'leaf'
      },
      editDataTypeId: null,
      showDataTypeEditDialog: false,
      currentDataType: {},
      dataTypeGroup: "billSetting"
    };
  },
  mounted() {
    treeScollx({target: this, type: 'default'});
    this.initTableList();
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.$openEditView('showEdit');
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'USER_bill_setting_list-m1',
        path: USER_PREFIX + '/bill_setting/listPage',
        param: () => {
          return {
            ...this.formData,
            dataTypeCode: this.currentDataType.dataTypeCode
          };
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('名称'),
            field: 'name',
            width: 150,
            fixed: 'left'
          },
          {
            title: this.$t1('编号'),
            field: 'code',
            width: 180
          },
          {
            field: 'enabled',
            title: this.$t1('是否启用'),
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.enabled) {
                  return [
                    <div class="txt-status">
                      <span>{this.$t1('启用')}</span>
                    </div>
                  ];
                } else {
                  return [
                    <div class="txt-status disable">
                      <span>{this.$t1('禁用')}</span>
                    </div>
                  ];
                }
              }
            }
          },
          {
            field: 'allCompany',
            title: this.$t1('全部组织使用'),
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.allCompany) {
                  return [
                    <div class="txt-status">
                      <span>{this.$t1('是')}</span>
                    </div>
                  ];
                } else {
                  return [
                    <div class="txt-status disable">
                      <span>{this.$t1('否')}</span>
                    </div>
                  ];
                }
              }
            }
          },
          {
            title: this.$t1('执行组织编码'),
            field: 'exeCompanyCode',
            width: 150
          },
          {
            title: this.$t1('备注'),
            field: 'remark',
            width: 150
          },
          {
            field: 'createDate',
            title: this.$t1('创建时间'),
            width: 150
          },
          {
            width: 47,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row}) => {
                return [
                  <div>
                    <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;
      });
    },
    handleNodeClick(data, node, v) {
      this.currentDataType = data;
      this.$forceUpdate();
      this.searchEvent();
    },
    // 异步树叶子节点懒加载逻辑
    loadNode(node, resolve) {
      let url = USER_PREFIX + '/data_type_setting/listPage';
      this.$http({
        url: url,
        method: 'post',
        data: {
          dataTypeGroup: this.dataTypeGroup
        },
        success: res => {
          let rows = [{id: -1, dataTypeCode: null, dataTypeName: this.$t1('全部'), leaf: true}]
          let records = res.objx && res.objx.records ? res.objx.records : [];
          records.forEach(value => {
            value.leaf = true;
          });
          rows.push(...records)
          resolve(rows);
          this.$nextTick(() => {
            let data = node.childNodes[0].data;
            this.$refs.tree.setCurrentKey(data);
            this.currentDataType = data;
          })
        }
      });
    },
    addDataType() {
      this.editDataTypeId = null;
      this.showDataTypeEditDialog = true;
    },
    editDataType() {
      let row = this.currentDataType;
      this.editDataTypeId = row.id;
      this.showDataTypeEditDialog = true;
    },
    confirmDataTypeEditDialog(row) {
      row.leaf = true;
      let editDataTypeId = this.editDataTypeId;
      if (!editDataTypeId) {
        this.$refs.tree.append(row);
      } else {
        let node = this.$refs.tree.getNode(row.id + "");
        node.data = row;
      }
    },
    deleteDataType() {
      let row = this.currentDataType;
      this.$baseConfirm(this.$t1('您确定要删除数据类型[{dataTypeName}]吗？', {dataTypeName: row.dataTypeName})).then(() => {
        var url = USER_PREFIX + `/data_type_setting/setDisabled`;
        this.$http({
          url: url,
          method: `post`,
          data: {id: row.id},
          isLoading: true,
          success: res => {
            this.$message({
              message: res.content,
              type: 'success',
              duration: 2000
            });
            this.$refs.tree.remove(row);

            let node = this.$refs.tree.getNode("-1");
            let data = node.data;
            this.$refs.tree.setCurrentKey(data);
            this.currentDataType = data;
            this.searchEvent();
          }
        });
      });
    }
  }
};
</script>
