<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="addData">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建类型津贴</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="keyword">
          <el-input placeholder="请输入搜索关键字" v-model="keyword" clearable @clear="getListData"></el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData">搜索</el-button>
      </el-form>
    </div>
    <el-table :data="tableData" tooltip-effect="dark" v-loading="loading" style="width: 100%">
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
      <el-table-column label="类型津贴名称" width="200" show-overflow-tooltip prop="lxjt_lxjtmc"></el-table-column>
      <el-table-column label="时长" prop="lxjt_dwsc"  width="100"></el-table-column>
      <el-table-column label="次数" prop="lxjt_dwcs" width="100"></el-table-column>
      <el-table-column label="金额" width="100">
        <template slot-scope="scope">
          <div>
            {{scope.row.lxjt_btje}}元
          </div>
        </template>
      </el-table-column>
      <el-table-column label="补贴说明" prop="lxjt_btsm" width="auto" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="delData(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer class="flex-item-none" style="display: flex; justify-content: flex-end; margin-top: 30px">
      <el-pagination background layout="total,prev, pager, next" :page-size="limit" :total="total"
        :current-page.sync="cur_page" @current-change="handleCurrentChange"></el-pagination>
    </footer>

    <!-- diglog弹出 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="dialogForm" ref="addForm" :rules="rules" label-width="120px" label-position="left">
        <el-form-item label="类型津贴名称" prop="lxjtmc">
          <el-select  style="width: 100%"  v-model="dialogForm.lxjtmc"  placeholder="选择津贴名称">
            <el-option v-for="item in lxjtLIST" :key="item.value" :label="item.label" :value="{value:item.value,label:item.label}">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时长" prop="sc">
          <el-input v-model="dialogForm.sc" placeholder="请输入时长"></el-input>
        </el-form-item>
        <el-form-item label="次数" prop="cs">
          <el-input v-model="dialogForm.cs" placeholder="请输入次数"></el-input>
        </el-form-item>
        <el-form-item label="金额" prop="je">
          <el-input v-model="dialogForm.je" placeholder="请输入金额"></el-input>
        </el-form-item>
        <el-form-item label="补贴说明" prop="btsm">
          <el-input type="textarea" v-model="dialogForm.btsm" placeholder="请输入补贴说明"></el-input>
        </el-form-item>
       
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { lxjt_list, lxjt_add, lxjt_edit, lxjt_del,lxjt_detail,jblx_list,org_users} from "./itemApi";
import { useUserStore } from "@/stores/user";
const { real_name, dept_ids } = useUserStore();
export default {
  name: "jblx",
  data() {
    return {
      keyword: "",
      tableData: [],
      type_value: "",
      lxjtLIST: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        lxjtmc: '',
        sc: '',
        cs: '',
        je: '',
        btsm:''
      },
      rules: {
        lxjtmc: [
          { required: true, message: '请输入类型津贴名称', trigger: 'blur' }
        ],
        sc: [
          {required: true, message: '请输入时长', trigger: 'blur'}
        ],
        cs: [
          {required: true, message: '请输入次数', trigger: 'blur'}
        ],
        je: [
          {required: true, message: '请输入金额', trigger: 'blur'}
        ],
        btsm:[
          {required: true, message: '请输入补贴说明', trigger: 'blur'}
        ]
      },
      title: "新建津贴类型",
      isEdit: false,
      chooseId: "",
      loading: false,
      userList: [],
    };
  },
  mounted() {
    // this.getUserList();
    this.getListData();
    this.jblxList();
  },
  methods: {
    getUserList() {
      let dataObj = {
        dept_id: dept_ids[0],
        page: 1,
        limit: 9999,
      };
      org_users(dataObj).then((res) => {
        if (res.code == 1) {
          this.userList = res.data.page_data;
        }
      });
    },
    //初始化列表数据
    getListData() {
      this.loading = true;
      let dataObj = {
        page: this.cur_page,
        limit: this.limit,
        lxjt_lxjtmc: this.keyword,
      };
      lxjt_list(dataObj).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    //翻页
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建津贴类型";
      this.dialogForm= {
        lxjtmc: '',
        sc: '',
        cs: '',
        je: '',
        btsm:''
      },
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    //加班类型列表
    jblxList() {
      this.lxjtLIST = [];
      let transObj = {
        limit:999,
        jblx_sfqy:1
      };
      jblx_list(transObj)
        .then(res => {
          if (res.code == "1") {
            let allD = res.data.page_data;
            allD.forEach(item=>{
              this.lxjtLIST.push({
                label: item.jblx_lxmc,
                value: item.jblx_id,
              })
            })
          }
        })
      .catch(err=>{console.log(err)})
    },
    editData(item) {
      this.title = "编辑津贴类型";
      this.isEdit = true;
      this.chooseId = item.lxjt_id;
      let transObj = {
        lxjt_id:this.chooseId,
      }
      lxjt_detail(transObj)
      .then(res => {
        if (res.code == 1) {
          let resData = res.data.one_info;
          this.dialogForm = {
            lxjtmc: resData.lxjt_lxjtmc,
            sc:resData.lxjt_dwsc,
            cs:resData.lxjt_dwcs,
            je:resData.lxjt_btje,
            btsm:resData.lxjt_btsm
          };
        }
      })
      .catch(error=>{console.log(error)})
      this.dialogFormVisible = true;

    },
    //提交
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          let dataObj = {
            issubmit:1,
            jdbg_jbcb_lxjt: {
              lxjt_lxjtmc:this.dialogForm.lxjtmc.label,
              lxjt_jblx:this.dialogForm.lxjtmc.value,//传加班类型ID
              lxjt_dwsc:this.dialogForm.sc,
              lxjt_dwcs:this.dialogForm.cs,
              lxjt_btje:this.dialogForm.je,
              lxjt_btsm:this.dialogForm.btsm,
            }
          };
          if (!this.isEdit) {//新增
            lxjt_add(dataObj).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: res.msg,
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getListData();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            dataObj.lxjt_id = this.chooseId;
            lxjt_edit(dataObj)
              .then(res => {
                if (res.code == '1') {
                  this.$message({
                    message: res.msg,
                    type: "success",
                  });
                  this.dialogFormVisible = false;
                  this.getListData();
                }
              })
            .catch(error=>{console.log(error)})
          }
          
        }
      })
    },
    //获取年-月-日
    getDataFormat() {
      // 获取当前日期
      var date = new Date();
      // 获取当前月份
      var nowMonth = date.getMonth() + 1;
      // 获取当前是几号
      var strDate = date.getDate();
      // 添加分隔符“-”
      var seperator = "-";
      // 对月份进行处理，1-9月在前面添加一个“0”
      if (nowMonth >= 1 && nowMonth <= 9) {
        nowMonth = "0" + nowMonth;
      }
      // 对月份进行处理，1-9号在前面添加一个“0”
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      // 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
      var nowDate = date.getFullYear() + seperator + nowMonth + seperator + strDate;
      return nowDate
    },
   //删除
   delData(item) {
      let dataObj = {
        lxjt_id: item.lxjt_id,
      }
      this.$confirm("确认删除该设置?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        lxjt_del(dataObj).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "删除成功！",
              type: "success",
            });
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      })
      .catch(() => { });
    },
    
  },
  
};
</script>

<style lang="scss" scoped>
.time-type-list {
  width: 100%;
  border-bottom: 1px solid #f3f3f3;
  margin-bottom: 20px;
  display: flex;

  li {
    position: relative;
    top: 2px;
    cursor: pointer;
    width: 50px;
    font-size: 14px;
    line-height: 30px;
    text-align: center;

    &+li {
      margin-left: 20px;
    }

    &.selected {
      color: #265cd4;
      border-bottom: 2px solid #265cd4;
    }
  }
}
</style>
