<template>
  <el-dialog
    title="指标权重明细"
    :visible.sync="isShowNormDialog"
    :width="'80%'"
    :close-on-click-modal="false"
    :before-close="closeHandler"
  >
    <div class="app-container" style="margin: 0px">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
      >
        <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:NORMFEE: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:NORMFEE: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:NORMFEE:remove']"
            >删除</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table :data="normData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="分类占比" align="center" prop="normRatio" />
        <el-table-column
          label="指标参数名称"
          align="center"
          prop="fname"
          width="150"
        />
        <el-table-column
          label="指标编码"
          align="center"
          prop="fcode"
          width="150"
        />
        <el-table-column
          label="计算公式"
          align="center"
          prop="formula"
          width="150"
        >
          <template slot-scope="scope">
            <el-link style="text-decoration: underline;color: #36aafd" @click="formulaDialog(scope.row.id,scope.row.formula)">{{scope.row.formula}}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="计量单位"
          align="center"
          prop="funitName"
          width="100"
        />
        <el-table-column
          label="权数"
          align="center"
          prop="fnumber"
          width="90"
        />
        <el-table-column
          label="排序"
          align="center"
          prop="sort"
          width="90"
        />
        <el-table-column
          label="绩效公差上下公差占比"
          align="center"
          prop="msRetio"
          width="130"
        />
        <el-table-column
          label="采集公差上下公差占比"
          align="center"
          prop="dfRetio"
          width="130"
        />
        <el-table-column
          label="采集参考值"
          align="center"
          prop="collValue"
          width="130"
        />
        <el-table-column
          label="绩效参考值"
          align="center"
          prop="meritsValue"
          width="130"
        />

        <el-table-column
          label="是否启用"
          align="center"
          prop="status"
          width="100"
        >
          <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="startTime" />
        <el-table-column label="停用时间" align="center" prop="stopTime" /> -->
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          width="150"
        />
        <el-table-column label="操作" align="center" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['business:NORMFEE:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['business:NORMFEE:remove']"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="formulaDialog(scope.row.id,scope.row.formula)"
              v-hasPermi="['business:NORMFEE:edit']"
            >计算公式</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="totalNum > 0"
        :total="totalNum"
        :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="170px">
          <el-form-item label="权数" prop="fnumber">
            <el-input
              v-model="form.fnumber"
              aria-placeholder="请输入数值"
            ></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input
              v-model="form.sort"
              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="selUnitHandler"
              placeholder="请选择"
              style="width: 100%"
            >
              <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="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>

    <formula @closeFormulaHandler="closeFormulaHandler" @dataFormulaHandler="dataFormulaHandler" :id="id" :normId="normId" reset :isShowFormulaDialog="isShowFormulaDialog" />

  </el-dialog>
</template>

<script>
import {
  listNORMFEE,
  getNORMFEE,
  delNORMFEE,
  addNORMFEE,
  updateNORMFEE,
} from "@/api/norm/NORMFEE";
import formula from './formula';

import { listData } from "@/api/system/dict/data";

import { formatDate } from "@/utils/index";

export default {
  name: "NORMFEE",
  components: {
    formula
  },
  props: {
    isShowNormDialog: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    normId: {
      type: String,
      default: () => {
        return null;
      },
    },
    normData: {
      type: Array,
      defaultL: () => {
        return [];
      },
    },
    totalNum:{
      type:Number,
      default:0
    }
  },
  data() {
    return {
      dict: {
        type: {
          dictType: "",
        },
      },
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      isShowFormulaDialog: false,
      id: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        // createTime: null,
        // createBy: null,
        // updateTime: null,
        // updateBy: null,
        // delFlag: null,
        // deptId: null,
        // normId: null,
        // normRatio: null,
        // fname: null,
        // funit: null,
        // fnumber: null,
        // fvalue: null,
        // status: null,
        // startTime: null,
        // stopTime: null,
        // remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fnumber: [{ required: true, message: "权数为数值", trigger: "blur" }],
        sort: [{ required: true, message: "排序为数值", trigger: "blur" }],
        normRatio: [
          { required: true, message: "分类占比不能为空", trigger: "blur" },
        ],
        fname: [
          { required: true, message: "指标参数名称不能为空", trigger: "blur" },
        ],
        funit: [
          { required: true, message: "计量单位不能为空", trigger: "blur" },
        ],
        dfRetio: [
          { required: true, message: "公差占比不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "明细类型不能为空", trigger: "blur" },
        ],
        msRetio: [
          {
            required: true,
            message: "绩效公差上下公差占比不能为空",
            trigger: "blur",
          },
        ],
        collValue: [
          { required: true, message: "采集参考值不能为空", trigger: "blur" },
        ],
        meritsValue: [
          { required: true, message: "绩效参考值不能为空", trigger: "blur" },
        ],
      },
      unitSel: null,
    };
  },

  mounted() {
    listData({
      pageNum: 1,
      pageSize: 10,
      dictType: "norm_uint",
    }).then((res) => {
      this.unitSel = res.rows;
    });
    this.getList();
  },
  methods: {
    formulaDialog(id,formula) {
      this.isShowFormulaDialog = true;
      this.id = id;
    },
    dataFormulaHandler(data) {
      this.isShowFormulaDialog = false;
      console.log("dataHandler111",data)
      this.queryParams.normId = data;
      console.log("11111111111",this.queryParams)
      listNORMFEE(this.queryParams).then((response) => {
        response.rows.forEach((item, index) => {
          response.rows[index].status = item.status == 0 ? false : true;
        });
        this.normData = response.rows;
      });
    },
    /** 查询指标权重明细列表 */
    getList() {
      this.loading = true;
      this.queryParams.normId = this.normId;
      console.log("11111111111",this.queryParams)
      listNORMFEE(this.queryParams).then((response) => {
        response.rows.forEach((item, index) => {
          response.rows[index].status = item.status == 0 ? false : true;
        });
        this.NORMFEEList = response.rows;
        this.loading = false;
        this.$emit("dataHandler", response.rows);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        deptId: null,
        normId: null,
        normRatio: null,
        fname: null,
        funit: null,
        fnumber: null,
        fvalue: null,
        status: "0",
        startTime: null,
        stopTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // this.form = selection[0];
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.reset();
      this.title = "添加指标权重明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getNORMFEE(id).then((response) => {
        this.form = response.data;
        this.open = true;
        // this.form = row;
        this.title = "修改指标权重明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.form.status = 1;
        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.normRatio) == this.form.normRatio ||
              parseFloat(this.form.normRatio) == this.form.normRatio
            )
          ) {
            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;
          }

          this.form.normId = this.normId;
          this.form.status = 1;
           if (this.form.id != null) {
             updateNORMFEE(this.form).then(response => {
               this.$modal.msgSuccess("修改成功");
               this.open = false;
               this.getList();
             });
           } else {
          addNORMFEE(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
           }

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      getNORMFEE(ids)
        .then((response) => {
          this.fname = response.data.fname;
          this.$modal
            .confirm('是否确认删除指标参数名称为"' + this.fname + '"的数据项？')
            .then(() => {
              return delNORMFEE(ids);
            })
            .then(() => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            })
            .catch(() => {});
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/normFee/export",
        {
          ...this.queryParams,
        },
        `NORMFEE_${new Date().getTime()}.xlsx`
      );
    },

    // 关闭Dialog窗口
    closeHandler() {
      this.$emit("closeHandler");
    },
    closeFormulaHandler() {
      this.isShowFormulaDialog = false;
    },
    // 选择单位处理
    selUnitHandler(val) {
      this.form.funit = val;
    },

    changeSwitchHandler(data) {
      let id = data.id;
      if (data.status) {
        updateNORMFEE({
          id,
          startTime: formatDate(new Date()),
          status: 1,
        });
      } else {
        updateNORMFEE({
          id,
          stopTime: formatDate(new Date()),
          status: 0,
        });
      }
    },
  },
};
</script>
