<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="cbs_name"
        width="auto"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        width="200"
        prop="cbs_sqr"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        width="150"
        prop="cbs_cjsj"
      ></el-table-column>
      <el-table-column label="审核人员" prop="cbs_shr" width="150">
      </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.cbs_id)"
            >删除
          </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>
    <el-dialog title="新建设置" :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="dialogForm" label-width="100px" label-position="left">
        <el-form-item label="设置名称" prop="gzmc">
          <el-input v-model="dialogForm.gzmc"></el-input>
        </el-form-item>
        <el-form-item label="创建日期" prop="cjsj">
          <el-date-picker
            v-model="dialogForm.cjsj"
            placeholder="选择日期"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="审核人" prop="shr">
          <el-select
              style="width: 100%"
              v-model="dialogForm.shr"
              placeholder="请选择审核人员"
          >
            <el-option
                v-for="item in userList"
                :key="item.user_id"
                :label="item.user_realname"
                :value="{value:item.user_id,label:item.user_realname}"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <ul class="time-type-list">
          <li
            v-for="item in time_type"
            :key="item.value"
            :class="item.id == dialogForm.btrqlx.id ? 'selected' : ''"
            @click="chooseTimeType(item)"
          >
            {{ item.label }}
          </li>
        </ul>
        <el-form-item label="补贴类型" style="width: 300px" prop="btgzlx">
          <el-select
            style="width: 100%"
            v-model="dialogForm.btgzlx"
            placeholder="请选择类型"
          >
            <el-option
              v-for="item in btlxList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="" label="最小补贴时间" prop="zxbtsj">
          <el-input style="width: 200px" v-model="dialogForm.zxbtsj"></el-input>
          小时
        </el-form-item>
        <el-form-item label="加/值班补贴" prop="btsjje">
          <el-input style="width: 200px" v-model="dialogForm.btsjje"></el-input>
          元/小时</el-form-item
        >
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { shsz_list, shsz_add, shsz_edit, shsz_del,org_users } from "./api";
import { useUserStore } from "@/stores/user";
const { real_name,dept_ids } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      keyword: "",
      tableData: [],

      shlxList: [
        {
          label: "对象1",
          value: "1",
        },
        {
          label: "对象2",
          value: "2",
        },
        {
          label: "对象3",
          value: "3",
        },
      ],
      btlxList: [
        {
          label: "加班",
          value: "1",
        },
        {
          label: "交通",
          value: "2",
        },
        {
          label: "餐补",
          value: "3",
        },
      ],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        gzmc: "",
        sqr: "",
        cjsj: "",
        btrqlx: {
          id: "1",
          label: "工作日",
        },
        btgzlx: "",
        zxbtsj: "",
        btsjje: "",
        shr: "",
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      time_type: [
        {
          id: "1",
          label: "工作日",
        },
        {
          id: "2",
          label: "休息日",
        },
        {
          id: "3",
          label: "节假日",
        },
        {
          id: "4",
          label: "其他",
        },
      ],
      userList:[],
    };
  },
  methods: {
    getUserList() {
      let data = {
        dept_id: dept_ids[0],
        page: 1,
        limit: 9999
      }
      org_users(data).then(res => {
        if (res.code == 1) {
          this.userList = res.data.page_data;
        }
      })
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };

      shsz_list(data).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 = {
        gzmc: "",
        sqr: {},
        cjsj: "",
        btrqlx: {
          id: "1",
          label: "工作日",
        },
        btgzlx: "",
        zxbtsj: "",
        btsjje: "",
        shr: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    editData(item) {
      this.title = "编辑设置";
      this.isEdit = true;
      this.id = item.cbs_id;
      this.dialogForm.gzmc = item.cbs_name;
      this.dialogForm.sqr = item.cbs_sqr;
      this.dialogForm.cjsj = item.cbs_cjsj;
      this.dialogForm.yydx = item.cbs_yydx;
      this.dialogForm.btrqlx = item.cbs_btrqlx;
      this.dialogForm.btgzlx = item.cbs_btgzlx;
      this.dialogForm.zxbtsj = item.cbs_zxbtsj;
      this.dialogForm.btsjje = item.cbs_btsjje;
      this.dialogForm.shr = item.cbs_shr;

      this.dialogFormVisible = true;
    },
    formSubmit() {
      let data = {
        cbs_name: this.dialogForm.gzmc,
        cbs_sqr: real_name,
        cbs_cjsj: this.dialogForm.cjsj,
        cbs_yydx: this.dialogForm.yydx,
        cbs_btrqlx: this.dialogForm.btrqlx,
        cbs_btgzlx: this.dialogForm.btgzlx,
        cbs_zxbtsj: this.dialogForm.zxbtsj,
        cbs_btsjje: this.dialogForm.btsjje,
        cbs_shr: this.dialogForm.shr.label,
        cbs_shr_user_id: this.dialogForm.shr.value
      };
      if (!this.isEdit) {
        shsz_add(data).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 {
        data.cbs_id = this.id;
        shsz_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "编辑成功！",
              type: "success",
            });

            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },

    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        cbs_id: id,
      };
      this.$confirm("确认删除该设置?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          shsz_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    chooseTimeType(item) {
      this.dialogForm = {
        btrqlx: {
          id: "1",
          label: "工作日",
        },
        btgzlx: "",
        zxbtsj: "",
        btsjje: "",
      };
      this.dialogForm.btrqlx.id = item.id;
      this.dialogForm.btrqlx.label = item.label;
    },
  },
  mounted() {
    this.getUserList();
    this.getListData();
  },
};
</script>
<style scoped lang="scss">
.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>