<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="grid-height">
          <vxe-grid
            ref="table-m1"
            :auto-resize="true"
            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>
                  <div class="txt-name el-icon-house" style="font-weight: unset;"
                       v-if="showFileSpaceInfo">{{ $t1('文件服务磁盘空间总量') }}：<span class="f-red">{{
                      fileSpaceInfo.totalSpace
                    }}</span>，{{ $t1('已用') }}：<span class="f-red">{{ fileSpaceInfo.usedSpace }}</span>，{{
                      $t1('剩余')
                    }}：<span
                      class="f-red">{{ fileSpaceInfo.freeSpace }}</span>
                  </div>
                </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('工作区名称')+':'">
                  <template v-slot="">
                    <el-input v-model="formData.storeAreaName" size="small" clearable/>
                  </template>
                </vxe-form-item>
                <vxe-form-item :title="$t1('工作区编码')+':'">
                  <template v-slot="">
                    <el-input v-model="formData.storeAreaCode" size="small" clearable/>
                  </template>
                </vxe-form-item>
              </vxe-form>
            </template>
          </vxe-grid>
        </div>
      </el-tab-pane>
      <el-tab-pane :label="$t1('工作区用户')" name="third">
        <userEdit v-if="showUserEdit" visible-key="showUserEdit" :fileStoreArea.sync="operateRow" :parent-target="_self"
                  @reload="$reloadHandle"></userEdit>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import editView from './edit.vue';
import userEdit from "../../../views/user/file_type/userEdit";

export default {
  name: 'file_type:list',
  components: {
    editView,
    userEdit
  },
  created() {
  },
  mounted() {
    this.getFileSpaceInfo();
    this.initTableList();
  },
  data() {
    return {
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      formData: {storeAreaType: null},
      activeName: 'second',
      showUserEdit: false,
      operateRow: null,
      showFileSpaceInfo: false,
      fileSpaceInfo: {
        freeSpace: null,
        totalSpace: null,
        usedSpace: null
      }
    };
  },
  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.$openEditView('showEdit');
    },
    openUserEditDialog(row) {
      this.operateRow = row;
      this.activeName = 'third';
      this.$openEditView('showUserEdit');
    },
    initTableList() {
      let storeAreaTypeMap = {
        0: this.$t1('是'),
        1: this.$t1('否')
      }
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'pcp_file_type_list-m1',
        path: USER_PREFIX + '/file_store_area/listPage',
        param: () => {
          return this.formData;
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('工作区名称'),
            field: 'storeAreaName',
            width: 150,
            fixed: 'left'
          },
          {
            title: this.$t1('工作区编码'),
            field: 'storeAreaCode',
            width: 150
          },
          {
            field: 'hasAuth',
            title: this.$t1('是否权限控制'),
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.hasAuth == 1) {
                  return [<div class="txt-status">{this.$t1('是')}</div>];
                } else {
                  return [<div class="txt-status s-3">{this.$t1('否')}</div>];
                }
              }
            }
          },
          {
            field: 'toNotify',
            title: this.$t1('文件操作，是否发系统通知'),
            width: 220,
            slots: {
              default: ({row}) => {
                if (row.toNotify == 1) {
                  return [<div class="txt-status">{this.$t1('是')}</div>];
                } else {
                  return [<div class="txt-status s-3">{this.$t1('否')}</div>];
                }
              }
            }
          },
          {
            field: 'toDownWatermark',
            title: this.$t1('文件下载，是否打水印'),
            width: 200,
            slots: {
              default: ({row}) => {
                if (row.toDownWatermark == 1) {
                  return [<div class="txt-status">{this.$t1('是')}</div>];
                } else {
                  return [<div class="txt-status s-3">{this.$t1('否')}</div>];
                }
              }
            }
          },
          {
            title: this.$t1('备注'),
            field: 'note',
            width: 150
          },
          {
            title: this.$t1('创建时间'),
            field: 'createDate',
            width: 150
          },
          {
            width: 180,
            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>,
                  <a
                    href="javascript:void(0);"
                    class="a-link"
                    onclick={() => {
                      this.openUserEditDialog(row);
                    }}
                    v-show={row.hasAuth == 1}
                  >
                    <el-tooltip enterable={false} effect="dark" content={this.$t1('查看工作区用户')} placement="top"
                                popper-class="tooltip-skin">
                      <i class="el-icon-user"/>
                    </el-tooltip>
                  </a>
                ];
              }
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
      });
    },
    getFileSpaceUrl(callback) {
      this.$http({
        url: USER_PREFIX + `/common/fileSpaceUrl`,
        method: `post`,
        data: {},
        success: res1 => {
          let url = res1.objx;
          if (url) {
            callback && callback(url)
          }
        }
      });
    },
    getFileSpaceInfo(url) {
      this.getFileSpaceUrl((url) => {
        if (url) {
          this.$http({
            url: url,
            method: `post`,
            data: {},
            success: res2 => {
              let data = res2.objx;
              if (data) {
                this.showFileSpaceInfo = true;
                this.fileSpaceInfo = data
              }
            }
          });
        }
      })
    },
  }
};
</script>
