<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable filterable>
          <el-option v-for="dict in statusArr" :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" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['business:NORM:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['business:NORM:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['business:NORM:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="NORMList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="指标分类名称" align="center">
        <template slot-scope="scope">
          <el-link @click="tableBdClick(scope.row)" class="text-color" type="primaty">{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="指标分类编号" align="center" prop="code" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" @change="changeSwitchHandler(scope.row)" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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-edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:NORM:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['business:NORM:remove']">删除</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="500px" :close-on-click-modal="false"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label-width="100px" label="指标分类编号">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label-width="100px" label="指标分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label-width="100px" label="备注">
          <el-input type="textarea" v-model="form.remark" rows="5"></el-input>
        </el-form-item>
        <el-form-item label-width="100px" prop="status" label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">不启用</el-radio>
          </el-radio-group>
        </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>

    <feeindex :totalNum="totalNum" @closeHandler="closeHandler" @dataHandler="dataHandler" :normData="normData" reset :normId="normId" :isShowNormDialog="isShowNormDialog" />
  </div>
</template>

<script>
import { listNORM, getNORM, delNORM, addNORM, updateNORM, exportNORM } from '@/api/norm/NORM';
import { listNORMFEE, getNORMFEE, delNORMFEE, addNORMFEE, updateNORMFEE } from '@/api/norm/NORMFEE';
import feeindex from './feeindex.vue';
export default {
  name: 'NORM',
  components: {
    feeindex
  },
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 指标分类表格数据
      NORMList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      totalNum:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deptId: null,
        name: null,
        code: null,
        status: null,
        remark: null
      },

      normData: [],

      pageParams: {
        page: 1,
        pageSize: 10
      },

      // 指标列表每项Id
      normId: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: 'id不能为空', trigger: 'blur' }],
        delFlag: [{ required: true, message: '删除标识不能为空', trigger: 'blur' }],
        deptId: [{ required: true, message: '部门id不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '指标分类名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '指标分类编号不能为空', trigger: 'blur' }],
        status: [
          {
            required: true,
            message: '状态(1启用,0不启用)不能为空',
            trigger: 'blur'
          }
        ],
        remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
      },
      statusArr: [
        { value: 0, label: '不启用' },
        { value: 1, label: '启用' }
      ],

      // 点击列表展示模态框
      isShowNormDialog: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询指标分类列表 */
    getList() {
      this.loading = true;
      listNORM(this.queryParams).then(response => {
        response.rows.forEach((item, index) => {
          response.rows[index].status = item.status == 0 ? false : true;
        });
        this.NORMList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.isShowNormDialog = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        code: null,
        status: '0',
        remark: 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) {
      console.log(row);
      this.reset();
      const id = row.id || this.ids;
      getNORM(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) {
            updateNORM(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addNORM(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除？')
        .then(function () {
          return delNORM(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      exportNORM().then(res => {
        this.download(
          '/norm/export',
          {
            ...this.queryParams
          },
          `NORM_${new Date().getTime()}.xlsx`
        );
      });
      // this.download(
      //   "business/NORM/export",
      //   {
      //     ...this.queryParams,
      //   },
      //   `NORM_${new Date().getTime()}.xlsx`
      // );
    },

    // 关闭Dialog窗口
    closeHandler() {
      this.isShowNormDialog = false;
    },

    // 表格行点击事件处理
    tableBdClick(row) {
      this.normId = row.id;
      listNORMFEE({ normId: this.normId, ...this.pageParams }).then(res => {
        res.rows.forEach((item, index) => {
          res.rows[index].status = item.status == 0 ? false : true;
        });
        this.totalNum = res.total
        this.normData = res.rows;
      });
      this.isShowNormDialog = !this.isShowNormDialog;
    },

    // 启动开关事件处理
    changeSwitchHandler(data) {
      let status = data.status === true ? 1 : 0;

      updateNORM({ id: data.id, status }).then(res => {});
    },

    dataHandler(data) {
      this.normData = data;
    }
  }
};
</script>

<style lang="scss">
.text-color {
  color: #409eff;
}
</style>
