<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="(item, index) in 2" :key="index" :label="item == 1 ? '启用' : '不启用'" :value="item == 1 ? 1 : 0" />
        </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:GIVE: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:GIVE: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:GIVE:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="GIVEList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报送参数名称" align="center" prop="fname"></el-table-column>
      <el-table-column label="计量单位" align="center" prop="funit"></el-table-column>
      <el-table-column label="分类占比" align="center" prop="normRatio"></el-table-column>
      <el-table-column label="权数" align="center" prop="fnumber"></el-table-column>
      <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="启用时间" width="150px" align="center" prop="startTime">
        <template slot-scope="scope">
          <div v-if="scope.row.status == '1' ? true : false">{{ scope.row.startTime }}</div>
        </template>
      </el-table-column>
      <el-table-column label="停用时间" width="150px" align="center" prop="stopTime">
        <template slot-scope="scope">
          <div v-if="scope.row.status == '0' ? true : false">{{ scope.row.stopTime }}</div>
        </template>
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark"></el-table-column>
      <el-table-column label="绩效公差上下公差占比" align="center" prop="msRetio" />
      <el-table-column label="采集公差上下公差占比" align="center" prop="dfRetio" />
      <el-table-column label="采集参考值" align="center" prop="collValue" />
      <el-table-column label="绩效参考值" align="center" prop="meritsValue" />

      <!-- <el-table-column label="明细类型(1=绩效,2=采集)" align="center" prop="type"></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-edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:GIVE:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['business:GIVE: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="600px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="权数" prop="fnumber">
          <el-input v-model="form.fnumber" aria-placeholder="请输入数值"></el-input>
        </el-form-item>

        <el-form-item label="分类占比" prop="normRatio">
          <el-input v-model="form.normRatio"></el-input>
        </el-form-item>

        <el-form-item label="计量单位" prop="funit">
          <el-select
            v-model="form.funit"
            @change="
              val => {
                form.funit = val;
              }
            "
            placeholder="请选择"
          >
            <el-option v-for="dict in unitSel" :key="dict.dictCode" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="报送参数名称" prop="fname">
          <el-input v-model="form.fname"></el-input>
        </el-form-item>

        <el-form-item label="采集公差上下公差占比" prop="dfRetio">
          <el-input v-model="form.dfRetio"></el-input>
        </el-form-item>

        <el-form-item label="绩效公差上下公差占比" prop="dfRetio">
          <el-input v-model="form.msRetio"></el-input>
        </el-form-item>

        <!-- <el-form-item label="明细类型" prop='type'>
          <template>
            <el-radio v-model="form.type" label="2">采集分类</el-radio>
            <el-radio v-model="form.type" label="1">绩效分类</el-radio>
          </template>
        </el-form-item> -->

        <el-form-item label="采集参考值" prop="collValue">
          <el-input v-model="form.collValue"></el-input>
        </el-form-item>

        <el-form-item label="绩效参考值" prop="meritsValue">
          <el-input v-model="form.meritsValue"></el-input>
        </el-form-item>

        <el-form-item label="备注">
          <el-input type="textarea" v-model="form.remark"></el-input>
        </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>
  </div>
</template>

<script>
import { listGIVE, getGIVE, delGIVE, addGIVE, updateGIVE } from '@/api/norm/GIVE';
import { listData } from '@/api/system/dict/data';
import { formatDate } from '@/utils/index';
export default {
  name: 'GIVE',
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报送数据项配置表格数据
      GIVEList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deptId: null,
        fname: null,
        funit: null,
        normRatio: null,
        fnumber: null,
        status: null,
        startTime: null,
        stopTime: null,
        remark: null,
        collValue: null,
        meritsValue: null,
        dfRetio: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: '$comment不能为空', trigger: 'blur' }],
        delFlag: [{ required: true, message: '删除标识不能为空', trigger: 'blur' }],
        deptId: [{ required: true, message: '部门id不能为空', trigger: 'blur' }],
        fvalue: [{ required: true, message: '指标参数不能为空', trigger: 'blur' }],
        fname: [{ required: true, message: '报送参数名称不能为空', trigger: 'blur' }],
        funit: [{ required: true, message: '计量单位不能为空', trigger: 'blur' }],
        normRatio: [{ required: true, message: '分类占比不能为空', trigger: 'blur' }],
        fnumber: [{ required: true, message: '权数不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '是否启用(1=启用,0不启用)不能为空', trigger: 'blur' }],
        startTime: [{ required: true, message: '启用时间不能为空', trigger: 'blur' }],
        stopTime: [{ required: true, message: '停用时间不能为空', trigger: 'blur' }],
        remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
        collValue: [{ required: true, message: '采集参考值不能为空', trigger: 'blur' }],
        meritsValue: [{ required: true, message: '绩效参考值不能为空', trigger: 'blur' }],
        dfRetio: [{ required: true, message: '上下公差占比不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '明细类型(1=绩效,2=采集)不能为空', trigger: 'change' }]
      },
      unitSel: null
    };
  },
  created() {
    this.getList();

    listData({
      pageNum: 1,
      pageSize: 10,
      dictType: 'norm_uint'
    }).then(res => {
      this.unitSel = res.rows;
    });
    this.getList();
  },

  methods: {
    /** 查询报送数据项配置列表 */
    getList() {
      this.loading = true;
      listGIVE(this.queryParams).then(response => {
        response.rows.forEach((item, index) => {
          response.rows[index].status = item.status == 0 ? false : true;
        });
        this.GIVEList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        deptId: null,
        fname: null,
        funit: null,
        normRatio: null,
        fnumber: null,
        status: '0',
        startTime: null,
        stopTime: null,
        remark: null,
        collValue: null,
        meritsValue: null,
        dfRetio: null,
        type: 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;
      getGIVE(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改报送数据项配置';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!(parseInt(this.form.fnumber) == this.form.fnumber || parseFloat(this.form.fnumber) == this.form.fnumber)) {
            this.$message.error('权数必须为整数或小数');
            return;
          }

          if (!(parseInt(this.form.meritsValue) == this.form.meritsValue || parseFloat(this.form.meritsValue) == this.form.meritsValue)) {
            this.$message.error('绩效参考值必须为整数或小数');
            return;
          }

          if (!(parseInt(this.form.collValue) == this.form.collValue || parseFloat(this.form.collValue) == this.form.collValue)) {
            this.$message.error('采集参考值必须为整数或小数');
            return;
          }

          if (!(parseInt(this.form.dfRetio) == this.form.dfRetio || parseFloat(this.form.dfRetio) == this.form.dfRetio)) {
            this.$message.error('采集公差上下公差占比必须为整数或小数');
            return;
          }

          if (!(parseInt(this.form.msRetio) == this.form.msRetio || parseFloat(this.form.msRetio) == this.form.msRetio)) {
            this.$message.error('绩效公差上下公差占比必须为整数或小数');
            return;
          }

          if (!(parseInt(this.form.normRatio) == this.form.normRatio || parseFloat(this.form.normRatio) == this.form.normRatio)) {
            this.$message.error('分类占比必须为整数或小数');
            return;
          }

          if (this.form.id != null) {
            updateGIVE(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addGIVE(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 delGIVE(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        '/norm/GIVE/export',
        {
          ...this.queryParams
        },
        `GIVE_${new Date().getTime()}.xlsx`
      );
    },

    // 新增按钮弹窗--->计量单位选择处理
    selUnitHandler(val) {},
    // 是否启用开关事件处理
    changeSwitchHandler(data) {
      let id = data.id;
      if (data.status) {
        updateGIVE({
          id,
          startTime: formatDate(new Date()),
          status: 1
        });
      } else {
        updateGIVE({
          id,
          stopTime: formatDate(new Date()),
          status: 0
        });
      }

      this.getList();
    }
  }
};
</script>
