<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>
        <el-button type="danger" size="medium" @click="batchDel" :disabled="multipleSelection.length == 0">
          <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="state_value">
          <el-select class="mr10" v-model="type_value" placeholder="请选择类别">
            <el-option v-for="item in type_list" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item> -->
        <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%" @selection-change="handleSelectionChange">
      <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="lb_name"></el-table-column>
      <el-table-column label="创建人" prop="lb_create_username"></el-table-column>
      <el-table-column label="创建时间" prop="create_dateline"></el-table-column>
      <el-table-column label="是否启用" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.lb_status_option_k=='1' ? true :false " @change="switchChange($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="detailShow(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>

    <!-- dialogt弹出 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="dialogForm" label-width="120px" label-position="left"  ref="addForm" :rules="rules_check">
        <el-form-item label="项目类别名称" prop="xmlbmc">
          <el-input v-model="dialogForm.xmlbmc"></el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch v-model="dialogForm.opneIt"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit('addForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!--详情dialog 显示-->
    <el-dialog title="详情" :visible.sync="dialogFormVisible_detail">
      <el-form :model="detail_form">
        <el-form-item label="项目类别名称" :label-width="formLabelWidth">
          <el-input v-model="detail_form.xmlbmc" autocomplete="off" :disabled="marDis"></el-input>
        </el-form-item>
        <el-form-item label="创建人" :label-width="formLabelWidth">
          <el-input v-model="detail_form.cjr" autocomplete="off" :disabled="marDis"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth">
          <el-input v-model="detail_form.cjsj" autocomplete="off" :disabled="marDis"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" :label-width="formLabelWidth">
          <el-input v-model="detail_form.sfqy" autocomplete="off" :disabled="marDis"></el-input>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible_detail = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { xmlb_list, xmlb_add, xmlb_edit, xmlb_del, xmlb_detail,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: "",
      type_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "未参与",
          value: "1",
        },
        {
          label: "已参与",
          value: "2",
        },
      ],
      multipleSelection: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        xmlbmc: '',
        opneIt:false
      },
      rules_check: {
        xmlbmc: [
          { required: true, message: '请输入项目类别名称', trigger: 'blur'}
        ]
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      userList: [],
      dialogFormVisible_detail: false,
      detail_form: {
        xmlbmc: '',
        cjr: '',
        cjsj: '',
        sfqy:'',
      },
      formLabelWidth: '120px',
      marDis:false,
    };
  },
  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 transObj = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
      };
      xmlb_list(transObj).then((res) => {
        this.loading = false;
        if (res.code == "1") {
          console.log(res,"000")
          this.tableData = res.data.page_data;
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
        }
        
      });
    },
     //多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //批量删除
    batchDel() {
      let delId = [];
       this.multipleSelection.forEach(item => {
         delId.push(item.lb_id);
      })
      let transObj = {
        lb_id:delId
      }
      this.$confirm("确认删除选中项目?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          xmlb_del(transObj).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => { });
    },
    //表格中switch 切换
    switchChange(val, row) {
      let transObj = {
        issubmit:1,
        lb_id: row.lb_id,
        jdbg_xmlxsb_xmlb: {
          lb_status:val== true ? 1 :2
        }
      }
      xmlb_edit(transObj)
        .then(res => {
          if (res.code == 1) {
            this.$message({
              message: "编辑成功！",
              type: "success",
            });

            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        })
      .catch(err=>{console.log(err)})
    },
    //新建项目类别
    addData() {
      this.title = "新建项目类别";
      this.dialogForm = {
        xmlbmc: '',
        opneIt: false
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    //编辑
    editData(item) {
      this.title = "编辑设置";
      this.isEdit = true;
      this.id = item.lb_id;
      let transObj = {
        lb_id:item.lb_id
      }
      xmlb_detail(transObj)
        .then(res => {
          if (res.code == "1") {
            let singleData=res.data.one_info;
            this.dialogForm= {
              xmlbmc: singleData.lb_name,
              opneIt: singleData.lb_status == '1' ? true : false,
            };
          }
        })
        .catch(err => { console.log(err) });

      this.dialogFormVisible = true;
    },
    //提交
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let transObj = {
            issubmit: 1,
            jdbg_xmlxsb_xmlb: {
              lb_name: this.dialogForm.xmlbmc,//名称
              lb_status: this.dialogForm.opneIt==true ? 1 : 2,//是否启用1-启用 2-不启用
              lb_create_username:real_name//创建用户名
            }
          };
          if (!this.isEdit) {
            xmlb_add(transObj).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 {

            transObj.lb_id = this.id;
            xmlb_edit(transObj).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;
    },
    //详情
    detailShow(item) {
      console.log(item, "000");
      this.detail_form = {
        xmlbmc: item.lb_name,
        cjr: item.lb_create_username,
        cjsj: item.create_dateline,
        sfqy: item.lb_status,
      };
      this.marDis = true;
      this.dialogFormVisible_detail = true;
    },
    //翻页
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
  },
  mounted() {
    // this.getUserList();
    this.getListData();
  },
};
</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>
