<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="前端标识" prop="token">
        <el-input v-model="queryParams.token" placeholder="请输入前端标识" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="所属地区" prop="deptId">
        <DeptSelect
          v-model="queryParams.deptId"
          placeholder="请选择所属地区"
          clearable
        />
      </el-form-item>
      <el-form-item label="是否禁用" prop="disabled">
        <el-select v-model="queryParams.disabled" placeholder="请选择是否禁用" clearable>
          <el-option v-for="dict in dict.type.sys_bool" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['file:project:add']">新增</el-button>
      </el-col>
      <!--
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['file:project:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['file:project:remove']">删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['file:project:export']" :loading="exportLoading">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column type="index" label="序号" width="50" align="center" />
      <!--<el-table-column label="ID" align="center" prop="id" min-width="60" />-->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="前端标识" align="center" prop="token" />
      <el-table-column label="远程地址前缀" align="center" prop="remoteHost" />
      <el-table-column label="磁盘路径" align="center" prop="diskPath" />
      <el-table-column label="部门ID" align="center" prop="deptId" />
      <el-table-column label="是否禁用" align="center" prop="disabled">
        <template slot-scope="scope">
          <dict-tag clickable :options="dict.type.sys_bool" :value="scope.row.disabled" @trigger="switchDisabled(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleLook(scope.row)" v-hasPermi="['file:project:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['file:project:edit']">修改</el-button>
          <el-dropdown :split-button="false" type="text" class="dropdown el-dropdown-link">
            <span style="cursor: pointer; font-size: 10px; color:#1890FF"><!--更多--><i class="el-icon-arrow-down el-icon--right"></i></span>
            <el-dropdown-menu slot="dropdown" style="padding: 10px">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['file:project:remove']">删除</el-button>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看项目对话框 -->
    <el-dialog :title="title" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-descriptions :column="descColumn" border :labelStyle="{width: `${descLabelWidth}%`}" :contentStyle="{width: `${(100 / descColumn) - descLabelWidth}%`}">
        <el-descriptions-item label="名称">{{ form.name }}</el-descriptions-item>
        <el-descriptions-item label="前端标识">{{ form.token }}</el-descriptions-item>
        <el-descriptions-item label="远程地址前缀">{{ form.remoteHost }}</el-descriptions-item>
        <el-descriptions-item label="磁盘路径">{{ form.diskPath }}</el-descriptions-item>
        <el-descriptions-item label="部门ID">{{ form.deptId }}</el-descriptions-item>
        <el-descriptions-item label="是否禁用"><el-tooltip effect="light" :content="form.disabled" placement="right"><dict-tag :options="dict.type.sys_bool" :value="form.disabled"/></el-tooltip></el-descriptions-item>
      </el-descriptions>

      <!-- 弹框操作按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"  show-word-limit :maxlength="64"/>
        </el-form-item>
        <el-form-item label="前端标识" prop="token">
          <el-input v-model="form.token" placeholder="请输入前端标识"  show-word-limit :maxlength="128"/>
        </el-form-item>
        <el-form-item label="远程地址前缀" prop="remoteHost">
          <el-input v-model="form.remoteHost" placeholder="请输入远程地址前缀"  show-word-limit :maxlength="128"/>
        </el-form-item>
        <el-form-item label="磁盘路径" prop="diskPath">
          <el-input v-model="form.diskPath" placeholder="请输入磁盘路径"  show-word-limit :maxlength="255">
            <el-button slot="append" icon="el-icon-search" @click="openFileChooser">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <DeptSelect
            v-model="form.deptId"
            placeholder="请选择所属地区"
            clearable
          />
        </el-form-item>
        <el-form-item label="是否禁用" prop="disabled">
          <el-radio-group v-model="form.disabled" placeholder="请选择是否禁用">
            <el-radio v-for="dict in dict.type.sys_bool" :key="dict.value" :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="diglogStatus" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择目录" :visible.sync="fileOpen" width="600px" append-to-body>
      <ServerFileChooser v-model="form.diskPath" :file-type="2" @selected="closeFileChooser"/>
    </el-dialog>
  </div>
</template>

<script>
import {listProject, getProject, delProject, addProject, updateProject, switchDisable} from "@/api/file/project";
import DeptSelect from "@/components/common/DeptSelect.vue";
import ServerFileChooser from "@/components/ServerFileChooser.vue";


export default {
  name: "Project",
  components: {
    ServerFileChooser,
    DeptSelect
  },
  dicts: ['sys_bool'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 遮罩按钮新增点击状态
      diglogStatus: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 默认隐藏搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否禁用 字典 sys_bool字典
      disabledOptions: [],
      // 查询参数
      queryParams: {
        // 分页
        pageNum: 1,
        pageSize: 10,
        // 查询排序
        //orderByColumn: "id",
        //isAsc: "desc",
        // 翻译字典
        //translate_dict: "1",
        name: null,
        token: null,
        remoteHost: null,
        diskPath: null,
        deptId: null,
        disabled: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
        token: [
          { required: true, message: "前端标识不能为空", trigger: "blur" },
        ],
        remoteHost: [
          { required: true, message: "远程地址前缀不能为空", trigger: "blur" },
        ],
        diskPath: [
          { required: true, message: "磁盘路径不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "部门ID不能为空", trigger: "blur" },
        ],
        disabled: [
          { required: true, message: "是否禁用 字典 sys_bool不能为空", trigger: "change" },
        ],
      },
      // 详情组件列数
      descColumn: 2,
      // 详情组件Label所占百分比, 最大={100 / descColumn}. 内容所占百分比={100 / descColumn - descLabelWidth}
      descLabelWidth: 15,
      // 对话框显示只读的详情
      viewOpen: false,
      fileOpen: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
      }).finally(() => this.loading = false );
    },
    // 是否禁用 字典 sys_bool字典翻译
    disabledFormat(row, column) {
      return this.selectDictLabel(this.dict.type.sys_bool, row.disabled);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.viewOpen = false;
      this.fileOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        token: null,
        remoteHost: null,
        diskPath: null,
        deptId: null,
        disabled: 'N',
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      // 防止表单提交失败再次打开按钮隐藏
      this.diglogStatus = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.viewOpen = false;
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.viewOpen = false;
        this.open = true;
        this.title = "修改项目";
      });
    },
    /** 查看按钮操作 */
    handleLook(row) {
      this.reset();
      // 查看时不允许提交
      this.diglogStatus = false;
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.viewOpen = true;
        this.open = false;
        this.title = "查看项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.diglogStatus = false;
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              if(response.code != 200) throw response.msg;
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.diglogStatus = true;
            }).catch(() => this.diglogStatus = true);
          } else {
            addProject(this.form).then(response => {
              if(response.code != 200) throw response.msg;
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.diglogStatus = true;
            }).catch(() => this.diglogStatus = true);
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目编号为"' + ids + '"的数据项？').then(function() {
        return delProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      var _this = this;
      this.$modal.confirm('是否确认导出所有项目数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        _this.exportLoading = true;
        _this.download('file/project/export', {
          ..._this.queryParams
        }, `project_${new Date().getTime()}.xlsx`).then(() => _this.exportLoading = false);
      }).catch(() => { _this.exportLoading = false });
    },
    openFileChooser() {
      this.fileOpen = true;
    },
    closeFileChooser() {
      this.fileOpen = false;
    },
    switchDisabled(row) {
      let what = row.disabled === '1' ? '启用' : '禁用';
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认' + what + '项目编号为"' + ids + '"的数据项？').then(function() {
        return switchDisable(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(what + "成功");
      }).catch(() => {});
    },
  },
};
</script>
