<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="岗级" prop="postLevel">
        <el-input v-model="queryParams.postLevel" placeholder="请输入岗级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="薪档" prop="salaryLevel">
        <el-input v-model="queryParams.salaryLevel" placeholder="请输入薪档" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="月薪" prop="salary">
        <el-input v-model="queryParams.salary" 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="handleAdd"
          v-hasPermi="['oa:salary:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['oa:salary:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['oa:salary:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['oa:salary:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="tabelOpen" type="info" plain icon="el-icon-data-analysis" size="mini"
          @click="generateChart">生成图表</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="!tabelOpen" type="info" plain icon="el-icon-arrow-left" size="mini"
          @click="generateChart">返回列表</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="tabelOpen" v-loading="loading" :data="salaryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="成本id" align="center" prop="salaryId" /> -->
      <el-table-column label="岗级" align="center" prop="postLevel">
        <template slot-scope="scope">
          {{ formatLevel(scope.row.postLevel, dict.type.sys_user_postlevel) }}
        </template>
      </el-table-column>
      <el-table-column label="薪档" align="center" prop="salaryLevel">
        <template slot-scope="scope">
          {{ formatLevel(scope.row.salaryLevel, dict.type.sys_user_salarylevel) }}
        </template>
      </el-table-column>
      <el-table-column label="月薪" align="center" prop="salary" />
      <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="['oa:salary:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:salary:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!-- 
    <pagination v-if="tabelOpen" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" /> -->

    <!-- 添加或修改cmc岗位工资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="岗级" prop="postLevel">
          <el-input v-model="form.postLevel" placeholder="请输入岗级" />
        </el-form-item>
        <el-form-item label="薪档" prop="salaryLevel">
          <el-input v-model="form.salaryLevel" placeholder="请输入薪档" />
        </el-form-item>
        <el-form-item label="月薪" prop="salary">
          <el-input v-model="form.salary" 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>

    <div id="charts" style="padding: auto; height: 80vh;"></div>
  </div>
</template>

<script>
import { listSalary, getSalary, delSalary, addSalary, updateSalary } from "@/api/oa/staff/salary";
import * as echarts from 'echarts';
import 'echarts-gl';
export default {
  name: "Salary",
  dicts: ['sys_user_postlevel', 'sys_user_salarylevel'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc岗位工资表格数据
      salaryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 999,
        postLevel: null,
        salaryLevel: null,
        salary: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tabelOpen: true,
      option: {},
      myChart: undefined,
    };
  },
  created() {
    this.getList();
    this.getListAll();
  },
  mounted() {
    this.myChart = echarts.init(document.getElementById('charts'));
    document.getElementById("charts").style.display = "none";
  },
  methods: {
    /** 查询cmc岗位工资列表 */
    getList() {
      this.loading = true;
      listSalary(this.queryParams).then(response => {
        this.salaryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询cmc岗位工资列表 */
    getListAll() {
      this.queryParams.pageSize = 58;
      listSalary(this.queryParams).then(response => {
        var data = [];
        for (let salary of response.rows) {
          data.push([Number(salary.postLevel) + 1, Number(salary.salaryLevel) + 1, salary.salary]);
        }
        var salarys = ['', '一级', '二级', '三级', '四级', '五级', '六级', '七级', '八级'];
        var posts = ['', '一岗', '二岗', '三岗', '四岗', '五岗', '六岗', '七岗', '八岗'];
        this.option = {
          tooltip: {},
          backgroundColor: '#fff',
          visualMap: {
            max: 8000,
            inRange: {
              color: [
                '#313695',
                '#4575b4',
                '#74add1',
                '#abd9e9',
                '#e0f3f8',
                '#ffffbf',
                '#fee090',
                '#fdae61',
                '#f46d43',
                '#d73027',
                '#a50026'
              ]
            }
          },
          xAxis3D: {
            type: 'category',
            data: posts,
            min: 1,
          },
          yAxis3D: {
            type: 'category',
            data: salarys,
            min: 1,
          },
          zAxis3D: {
            type: 'value',
          },
          grid3D: {
            boxWidth: 160,
            boxDepth: 80,
            viewControl: {
              projection: 'orthographic',
              alpha: 10,
              beta: -60
            }
          },
          series: [
            {
              name: '岗位工资',
              type: 'bar3D',
              shading: 'lambert',
              data: data,
              emphasis: {
                label: {
                  fontSize: 20,
                  color: '#000'
                },
                itemStyle: {
                  color: '#900'
                }
              }
            }
          ]
        };
        this.myChart.setOption(this.option);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        salaryId: null,
        postLevel: null,
        salaryLevel: null,
        salary: 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.salaryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc岗位工资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const salaryId = row.salaryId || this.ids
      getSalary(salaryId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc岗位工资";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.salaryId != null) {
            updateSalary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSalary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const salaryIds = row.salaryId || this.ids;
      this.$modal.confirm('是否确认删除cmc岗位工资编号为"' + salaryIds + '"的数据项？').then(function () {
        return delSalary(salaryIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/salary/export', {
        ...this.queryParams
      }, `salary_${new Date().getTime()}.xlsx`)
    },
    generateChart() {
      if (this.tabelOpen) {
        this.tabelOpen = false;
        document.getElementById("charts").style.display = "";
      }
      else {
        this.tabelOpen = true;
        document.getElementById("charts").style.display = "none";
      }
    },
    formatLevel(level, levelList) {
      if (level) {
        let levelLabel = levelList[level].label
        return levelLabel
      } else {
        return ''
      }
    },
  }
};
</script>
