<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" :userType="userType" :flag="flag"></editView>
      </el-tab-pane>
      <el-tab-pane :label="$t1('列表')" name="second">
        <div class="grid-height">
          <vxe-grid
            id="table-m1"
            ref="table-m1"
            :auto-resize="true"
            v-bind="vxeOption"
            @resizable-change="$vxeTableUtil.onColumnWitchChange"
            @custom="$vxeTableUtil.customHandle"
          >
            <template #form>
              <tableForm :formData.sync="formData" :advancedFormData.sync="advancedFormData"
                         @searchEvent="searchEvent" @resetEvent="resetEvent">
                <template #buttonLeft>
                  <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"/>
                </template>
                <template #buttonRight>
                  <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>
                </template>
              </tableForm>
            </template>
          </vxe-grid>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import editView from './form_edit.vue';
import tableForm from "@base/components/table/tableForm";

export default {
  name: 'user:form_list',
  props: {
    userType: {
      type: Number
    },
    flag: String
  },
  components: {
    tableForm,
    editView
  },
  created() {

  },
  activated() {

  },
  async mounted() {
    this.initTableList();
  },
  data() {
    return {
      showEdit: false,
      activeName: 'second',
      showUpload: true,
      uploadSize: 10,
      value10: '',
      dataId: '',
      tp: false,
      showUserDialog: false,
      nickName: 'dd',
      formData: {saleOrgName: null, roleCodes: []},
      vxeOption: {
        className: 'list-table'
      },
      showAdvancedSearch: false,
      advancedFormData: {}
    };
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.advancedFormData = {};
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.$openEditView('showEdit');
    },
    initTableList() {
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'basic_user_list-m11',
        aes: true,
        path: USER_PREFIX + '/user/listAllPage',
        param: () => {
          let formData = {
            ...this.formData,
            ...this.advancedFormData,
            flag: 8
          };
          return formData;
        },
        columns: [
          {
            type: 'checkbox',
            width: 48,
            resizable: false,
            fixed: 'left'
          },
          {
            title: '登录名',
            field: 'loginAccount',
            width: 250,
            fixed: 'left'
          },
          {
            field: 'nickName',
            title: '姓名',
            width: 150
          },
          {
            field: 'groupName',
            title: '用户组',
            width: 150
          },
          {
            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>];
                }
              }
            }
          },
          {
            field: 'modifyDate',
            title: this.$t1('更新时间'),
            width: 150
          },
          {
            field: 'createDate',
            title: this.$t1('创建时间'),
            width: 150
          },
          {
            width: 47,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row}) => {
                return [
                  <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>
                ];
              }
            }
          }
        ],
        searchColumns: [
          {title: "登录名", field: "loginAccount", type: "input", common: true},
          {title: "姓名", field: "nickName", type: "input", common: true},
          {title: "登录名(批量)", field: "loginAccounts", type: "inputBatch", common: false},
          {
            title: this.$t1('是否启用'), field: "enabled", type: "select", common: false, itemOption: [
              {label: this.$t1('启用'), value: true},
              {label: this.$t1('禁用'), value: false}
            ]
          },
          ...(this.$store.getters.userFlag !== 8 ? [{
            title: this.$t1('用户组'),
            field: "groupCode",
            type: "select",
            common: true,
            itemOption: [],
            multiple: false,
            syncHandle: () => {
              return this.getUserGroups()
            }
          }] : [])

        ],
        config: {
          pagerConfig: {
            pageSize: 500
          }
        }
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
      });
    },
    getUserGroups() {
      return new Promise((resolve, reject) => {
        this.$http({
          aes: true,
          url: USER_PREFIX + '/groups/listPage',
          method: 'post',
          data: {},
          success: res => {
            let rows = res.objx?.records || [];
            let result = rows.map(row => {
              return {
                value: row.groupCode,
                label: row.groupName
              }
            });
            resolve(result);
          }
        });
      })

    }
  }
};
</script>
