<template>
  <div class="app-container pageTopForm">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" size="small" label-width="68px" class="demo-ruleForm">
      <el-col :span="6">
        <el-form-item label="业务名称" prop="businessName">
            <el-input
              v-model="queryParams.businessName"
              placeholder="请输入业务名称"
              clearable
              @keyup.enter.native="handleQuery"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item class="lastBtnSty">
          <el-button type="primary" size="mini" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-col>
    </el-form>
    <TableList :columns="columns" :config="config" :queryParams="queryParams" :modelIdent="this.$options.name" :tableList="roleList" @fnClick="callback" @fnRenderList="getList" @value-change="handleValueChange">
      <template #tempBusinessName="{ row, column }">
       <div>
         <!-- 非编辑状态：显示文本 -->
         <template v-if="!row.editStatus">
           <span>{{ row.businessName }}</span>
           <el-button icon="el-icon-edit" type="text" @click="rowDataChange(row)"></el-button>
         </template>
         <template v-else>
           <!-- 编辑状态：显示输入框 -->
           <el-col :span="16">
             <el-input
               v-model="row.businessName"
               size="mini"
               @keyup.enter="callback({fnCode: 'edit'}, row)"
               @blur="callback({fnCode: 'edit'}, row)"
             ></el-input>
           </el-col>
           <el-col :span="8">
             <el-button size="mini" type="primary" @click="callback({fnCode: 'submit'}, row)">确定</el-button>
             <el-button size="mini" @click="callback({fnCode: 'cancel'}, row)">取消</el-button>
           </el-col>
         </template>
       </div>
      </template>
    </TableList>
  </div>
</template>

<script setup name="Topology">
  import TableList from "@/components/table/index.vue"
  import {listBusiness,updateBusiness,countBusinessName, delBusiness} from "@/api/disRevenue/earnManage"
  export default {
    name: 'BusinessIndex',
    components: {TableList},
    data() {
      return {
        editStatus: false,
        loading: false,
        showSearch: true,
        roleList: [],
        queryParams: {
          total: 0,
          pageNum: 1,
          pageSize: 10
        },
        // 列显隐信息
        columns: {
          businessName: { label: `业务名称`, slotName: 'tempBusinessName', visible: true},
          id: { label: `业务代码`, visible: true},
          createTime: { label: `创建时间`, visible: true},
        },
        config: {
          searcherForm: [
            {label: '资源组名称', prop: 'groupName', type: 'input'}
          ],
          tableButton: {
            top: [
              {content: '新增', fnCode: 'add', type: 'primary', icon: 'el-icon-plus', hasPermi: 'earnManage:business:add'},
              // {content: '删除', fnCode: 'delete', type: 'danger', icon: 'el-icon-delete', hasPermi: 'disRevenue:earnManage:business:detele'},
              {content: '导出', fnCode: 'export', type: 'warning', icon: 'el-icon-download', hasPermi: 'earnManage:business:export'},
            ],
            // line: [
            //   {content: '修改', fnCode: 'edit', type: 'text', icon: 'el-icon-edit', hasPermi: 'disRevenue:earnManage:business:edit'},
            // ]
          }
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      rowDataChange(row) {
        this.$set(row, 'editStatus', true);
      },
      // 处理子组件传递的新值
      handleValueChange(newValue) {
        // 父组件更新自身数据，实现同步
        this.showSearch = newValue;
        // console.log('父组件拿到新值:', newValue);
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        listBusiness(this.queryParams).then(response => {
          this.roleList = response.rows;
          this.queryParams.total = response.total;
          this.loading = false;
        })
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryRef");
        this.handleQuery();
      },

      callback(result, rowData, selectChange) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'add':
              this.$router.push({
                path:'/earnManage/business/details/index'});
              break;
            case 'submit':
              if (this.loading) return;
              this.loading = true;
              countBusinessName(rowData).then(res => {
                updateBusiness(rowData).then(response => {
                  this.$set(rowData, 'editStatus', false);
                  this.getList();
                  this.$modal.msgSuccess(response.msg);
                  this.loading = false;
                }).catch(() => {
                  this.$modal.msgError("操作失败");
                  this.loading = false;
                });
              }).catch(error => {
                // this.$modal.msgError("存在重复的业务名称，请重新输入！");
                this.loading = false;
              });
              break;
              case 'cancel':
                this.$set(rowData, 'editStatus', false);
                break;
            case 'delete':
              this.$modal.confirm('是否确认删除数据项？').then(function() {
                return delBusiness(selectChange)
              }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功")
              }).catch(() => {});
              break;
            case 'export':
              // let dataList = [];
              // Object.keys(this.columns).forEach(item => {
              //   if (item.visible) {
              //     dataList.push(item.prop);
              //   }
              // });
              // this.download("/system/Business/export", {properties: dataList,}, `业务管理_${new Date().getTime()}.xlsx`);
              let paramsList = Object.assign({}, this.queryParams,rowData);
              this.download("system/Business/export", paramsList, `业务管理_${new Date().getTime()}.xlsx`, null, 'json');
              break;
            default:

          }
        }
      }
    }
  }
</script>
<style scoped>
  ::v-deep .lastBtnSty .el-form-item__content{
    margin-left: 10px!important;
  }
</style>
