<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="类型名称" show-overflow-tooltip prop="jblx_lxmc"></el-table-column>
      <el-table-column label="创建人" prop="jblx_cjr"></el-table-column>
      <el-table-column label="创建时间" prop="jblx_cjsj"></el-table-column>
      <el-table-column label="是否启用" >
        <template slot-scope="scope">
          <el-switch 
          v-model="scope.row.jblx_sfqy_option_k" 
          active-value="1"  
          inactive-value="2"
          @change=switchChang($event,scope.row)
          ></el-switch>
        </template>
      </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>

    <!-- dialog 弹出 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="dialogForm"  ref="addForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="类型名称" prop="lxmc">
          <el-input v-model="dialogForm.lxmc"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="opneIt">
          <el-switch v-model="dialogForm.opneIt"></el-switch>
        </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 { jblx_list, jblx_add, jblx_edit, jblx_del, org_users,jblx_detail } from "./itemApi";
import { useUserStore } from "@/stores/user";
const { real_name, dept_ids } = useUserStore();
export default {
  name: "jblx",
  data() {
    return {
      keyword: "",
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        lxmc: '',
        opneIt:''
      },
      rules: {
        lxmc: [
          { required: true, message: '请输入类型名称', trigger: 'blur' }
        ]
      },
      title: "新建加班类型",
      isEdit: false,
      loading: false,
      chooseId: "",
      userList: [],
    };
  },
  mounted() {
    // this.getUserList();
    this.getListData();
  },
  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 = {
        issubmit:1,
        page: this.cur_page,
        limit: this.limit,
        jblx_lxmc: this.keyword,
      };
      jblx_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();
    },
    //添加switch
    addData() {
      this.title = "新建加班类型";
      this.dialogForm = {
        lxmc: '',
        opneIt:false
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    //编辑按钮
    editData(item) {
      this.title = "编辑加班类型";
      this.isEdit = true;
      this.chooseId = item.jblx_id;
      let transObj = {
        jblx_id:this.chooseId,
      }
      jblx_detail(transObj)
      .then(res => {
        if (res.code == 1) {
          let resData = res.data.one_info;
          this.dialogForm = {
            lxmc: resData.jblx_lxmc,
            opneIt: resData.jblx_sfqy_option_k == '1' ? true : false
          };
        }
      })
      .catch(error=>{console.log(error)})
      this.dialogFormVisible = true;
    },
    //列表开关
    switchChang($event, state) {
      console.log(typeof $event);
      //调用编辑接口改变状态
      let dataObj = {
        issubmit: 1,
        jblx_id:state.jblx_id,
        jdbg_jbcb_jblx: {
          // jblx_lxmc: state.lxmc,
          jblx_sfqy: $event,//是否启用 1 启用 2不启用
          // jblx_cjr: real_name,
        }
      };
      jblx_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)})
    },
    //提交
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          let dataObj = {
            issubmit:1,
            jdbg_jbcb_jblx: {
              jblx_lxmc: this.dialogForm.lxmc,
              jblx_sfqy: this.dialogForm.opneIt==true ? 1 : 2,//是否启用 1 启用 2不启用
              jblx_cjr: real_name,
              jblx_cjsj: this.getDataFormat()
            }
          };
          if (!this.isEdit) {//新增
            jblx_add(dataObj).then(res => {
              if (res.code == 1) {
                this.$message({
                  message: "添加成功！",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getListData();
              }else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            })
          } else {//编辑
            let dataObj = {
              issubmit: 1,
              jblx_id:this.chooseId,
              jdbg_jbcb_jblx: {
                jblx_lxmc: this.dialogForm.lxmc,
                jblx_sfqy: this.dialogForm.opneIt==true ? 1 : 2,//是否启用 1 启用 2不启用
                jblx_cjr: real_name,
                jblx_cjsj: this.getDataFormat()
              }
            };
            jblx_edit(dataObj)
              .then(res => {
                if (res.code == '1') {
                  this.$message({
                    message: "编辑成功！",
                    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 = {
        jblx_id: item.jblx_id,
      }
      this.$confirm("确认删除该设置?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        jblx_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>
