<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="应用所有者名称" prop="ownerName">
        <el-input v-model="queryParams.ownerName" placeholder="请输入应用所有者名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用所有者账号" prop="ownerAccount">
        <el-input v-model="queryParams.ownerAccount" placeholder="请输入应用所有者账号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用名称" prop="appName">
        <el-input v-model="queryParams.appName" placeholder="请输入应用名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用类型名称" prop="appTypeName">
        <el-input v-model="queryParams.appTypeName" placeholder="请输入应用类型名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="应用类型枚举" prop="appType">
        <el-select v-model="queryParams.appType" placeholder="请选择应用类型枚举" clearable>
          <el-option v-for="dict in dict.type.app_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" clearable @keyup.enter.native="handleQuery" />
      </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="handleSyn"
          v-hasPermi="['business:ctApply:syn']">同步应用数据</el-button>
          <span style="color: red;"> 提示：搜索更新并非实时数据,同步为最新数据</span>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ctApplyList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" fixed="left" /> -->
      <el-table-column label="序号" width="55" align="center" fixed="left">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="应用名称" align="center" prop="appName" width="200" fixed="left" >
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p> {{ scope.row.appName }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{scope.row.appName}}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="所属部门" align="center" prop="deptName" width="100" >
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p v-if="scope.row.deptName == null">应用名称前缀：部门-XXX 分配部门</p>
            <p v-if="scope.row.deptName != null"> {{ scope.row.deptName }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium" v-if="scope.row.deptName != null" >{{scope.row.deptName}}</el-tag>
              <el-tag size="medium" v-if="scope.row.deptName == null" >未分配部门</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="版本" align="center" prop="version" width="100" />
      <el-table-column label="是否支持应用参数" align="center" prop="supportParam" width="200">
        <template slot-scope="scope">
          <span v-if="scope.row.supportParam">
            是
          </span>
          <span v-if="scope.row.supportParam == 0">
            否
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="icon图下载地址" align="center" prop="icon" width="200" /> -->
      <!-- <el-table-column label="参数" align="center" prop="param" width="150" /> -->
      <el-table-column label="预计人工耗时（分钟）" align="center" prop="manualTime" width="150" />
      <el-table-column label="应用类型名称" align="center" prop="appTypeName" width="100" />
      <!-- <el-table-column label="应用类型枚举" align="center" prop="appType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_type" :value="scope.row.appType" />
        </template>
      </el-table-column> -->
      <el-table-column label="应用创建时间" align="center" prop="appCreateTime" width="100" />
      <el-table-column label="应用修改时间" align="center" prop="appUpdateTime" width="100" />
      <el-table-column label="应用所有者名称" align="center" prop="ownerName" width="120" />
      <!-- <el-table-column label="应用所有者账号" align="center" prop="ownerAccount" width="200" />
      <el-table-column label="所有者id" align="center" prop="ownerId" width="300" />
      <el-table-column label="appid" align="center" prop="appId" width="300" /> -->
      
      <!-- <el-table-column label="部门id" align="center" prop="deptId" width="100" /> -->
      <el-table-column label="创建者" align="center" fixed="right" prop="createBy" width="100" />
      <el-table-column label="创建时间" align="center" fixed="right" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" fixed="right" prop="updateBy" width="100" />
      <el-table-column label="更新时间" align="center" fixed="right" prop="updateTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="100" />
      <el-table-column label="操作" align="center" width="200" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.version != '未发版'" size="mini" type="text" icon="el-icon-edit" @click="showConfigTime(scope.row)"
            v-hasPermi="['business:ctApply:configTime']">设置人工用时</el-button>
        </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="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="应用类型名称" prop="appTypeName">
          <el-input v-model="form.appTypeName" placeholder="请输入应用类型名称" />
        </el-form-item>
        <el-form-item label="应用类型枚举" prop="appType">
          <el-select v-model="form.appType" placeholder="请选择应用类型枚举">
            <el-option v-for="dict in dict.type.app_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用创建时间" prop="appCreateTime">
          <el-date-picker clearable v-model="form.appCreateTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择应用创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="应用修改时间" prop="appUpdateTime">
          <el-date-picker clearable v-model="form.appUpdateTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择应用修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="应用所有者名称" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入应用所有者名称" />
        </el-form-item>
        <el-form-item label="应用所有者账号" prop="ownerAccount">
          <el-input v-model="form.ownerAccount" placeholder="请输入应用所有者账号" />
        </el-form-item>
        <el-form-item label="所有者id" prop="ownerId">
          <el-input v-model="form.ownerId" placeholder="请输入所有者id" />
        </el-form-item>
        <el-form-item label="appid" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入appid" />
        </el-form-item>
        <el-form-item label="版本，值内容：未发版、版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本，值内容：未发版、版本" />
        </el-form-item>
        <el-form-item label="icon图下载地址" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入icon图下载地址" />
        </el-form-item>
        <el-form-item label="预计人工耗时" prop="manualTime">
          <el-input v-model="form.manualTime" placeholder="请输入预计人工耗时" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 设置时间 -->
    <el-dialog title="配置预计人工用时" :visible.sync="showConfigTimeVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="预计人工用时" prop="manualTime">
          <el-input-number v-model="form.manualTime" @change="handleChange" label="请输入预计人工用时"></el-input-number>分钟
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitConfigTime">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCtApply, getCtApply, delCtApply, addCtApply, updateCtApply, syn } from "@/api/business/ctApply";

export default {
  name: "CtApply",
  dicts: ['app_type'],
  data() {
    return {
      // 显示配置时间
      showConfigTimeVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用信息管理表格数据
      ctApplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownerName: null,
        ownerAccount: null,
        appName: null,
        appTypeName: null,
        appType: null,
        supportParam: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 设置时间 */
    submitConfigTime() {
      let data = { id: this.form.id, manualTime: this.form.manualTime }
      updateCtApply(data).then(response => {
        this.$modal.msgSuccess("设置成功");
        this.showConfigTimeVisible = false;
        this.getList();
      });
    },
    /** 打开设置时间 */
    showConfigTime(row) {
      this.form.id = row.id
      this.showConfigTimeVisible = true;
    },
    /** 同步 */
    handleSyn() {
      syn().then(res => {
        this.getList()
        this.$modal.msgSuccess("操作成功");
      });
    },
    /** 查询应用信息管理列表 */
    getList() {
      this.loading = true;
      listCtApply(this.queryParams).then(response => {
        this.ctApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.showConfigTimeVisible = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ownerName: null,
        ownerAccount: null,
        ownerId: null,
        appId: null,
        appName: null,
        appTypeName: null,
        appType: null,
        appCreateTime: null,
        appUpdateTime: null,
        version: null,
        supportParam: null,
        icon: null,
        manualTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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.open = true;
      this.title = "添加应用信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCtApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应用信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCtApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCtApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除应用信息管理编号为"' + ids + '"的数据项？').then(function () {
        return delCtApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/ctApply/export', {
        ...this.queryParams
      }, `ctApply_${new Date().getTime()}.xlsx`)
    },
    handleChange(value) {
      console.log(value);
    }
  }
};
</script>
