<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="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%">
      <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="lc_name"></el-table-column>
      <el-table-column label="创建人" prop="lc_create_username"></el-table-column>
      <el-table-column label="创建时间" prop="create_dateline"></el-table-column>
      <el-table-column label="应用对象" prop="lc_yydx_name"></el-table-column>
      <el-table-column label="是否启用" >
        <template slot-scope="scope">
          <el-switch v-model="scope.row.lc_status_option_k=='1' ? true : false " @change='changeStatus($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="detailData(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>

    <!-- dialogt弹出--新建和编辑 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="60%;" v-loading="dialog_loading">
      <el-form :model="dialogForm" ref="addForm" :rules="check_rules" label-width="100px" label-position="left">
        <el-form-item label="流程名称" prop="lcmc">
          <el-input v-model="dialogForm.lcmc"></el-input>
        </el-form-item>
          <template>
            <div class="approvalDesign" style="margin-bottom:22px;">
              <h4 class="titleBi">审批流程设计</h4>
              <div class="singleItem" v-for="(item,index) in addAll" :key="index">
                <h4>审批部门</h4>
                <div class="formItem">
                  <el-select v-model="item.detpart_all" placeholder="请选择审批部门"  filterable @change="changeDepLinkNumber">
                    <el-option v-for="secItem in orgLIST" :key="secItem.value" :label="secItem.label" :value="secItem">
                    </el-option>
                  </el-select>
                </div>
                <h4>审批人</h4>
                <div class="formItem">
                  <el-select v-model="item.checker_all" placeholder="请选择审批人" filterable>
                    <el-option v-for="thrItem in relUserList" :key="thrItem.value" :label="thrItem.label" :value="thrItem" >
                    </el-option>
                  </el-select>
                </div>
                <h4>职位</h4>
                <div class="formItem">
                  <el-input v-model="item.posts_name" placeholder="请输入职位"></el-input>
                </div>
                <div class="delIcon" @click="singleDel(item,index)">
                  <i class="el-icon-delete" style="color:red;font-size:20PX;"></i>
                </div>
              </div>  
              <div class="addApprovel">
                <el-button type="primary" size="small" @click="addOne">
                  <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>   
             
            </div>
          </template>
        <el-form-item label="应用对象">
          <el-select v-model="dialogForm.applyObj" placeholder="请选择应用对象" @change="applyDepart" filterable>
            <el-option v-for="item in orgLIST" :key="item.value" :label="item.label"
              :value="{value:item.value,label:item.label}">
            </el-option>
          </el-select>
        </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>

    <!-- dialog弹出——详情 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible_detail">
      <el-form :model="form_detail">
        <el-form-item label="流程名称" :label-width="formLabelWidth_detail">
          <el-input v-model="form_detail.lcmc" autocomplete="off" :disabled="markDis"></el-input>
        </el-form-item>
        <el-form-item label="创建人" :label-width="formLabelWidth_detail">
          <el-input v-model="form_detail.cjr" autocomplete="off" :disabled="markDis"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth_detail">
          <el-input v-model="form_detail.cjsj" autocomplete="off" :disabled="markDis"></el-input>
        </el-form-item>
        <el-form-item label="应用对象" :label-width="formLabelWidth_detail">
          <el-input v-model="form_detail.yydx" autocomplete="off" :disabled="markDis"></el-input>
        </el-form-item>
         <el-form-item label="是否启用" :label-width="formLabelWidth_detail">
          <el-input v-model="form_detail.sfqy" autocomplete="off" :disabled="markDis"></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 { lcgl_list, lcgl_add, lcgl_edit, lcgl_detail, lcgl_del,org_list,org_users } from "./itemApi";
import { useUserStore } from "@/stores/user";
const { real_name, dept_ids } = useUserStore();
export default {
  name: "lcgl",
  data() {
    return {
      keyword: "",
      tableData: [],
      type_value: "",
      type_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "启用",
          value: "1",
        },
        {
          label: "未启用",
          value: "2",
        },
      ],
      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: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        lcmc: '',
        qcgwArr:'',
        applyObj:''
      },
      check_rules: {
        lcmc: [
          { required: true, message: '请输入项目类别名称', trigger: 'blur' }
        ],
        applyObj: [
          {required: true, message: '请选择应用对象', trigger: 'change'}
        ]

      },
      title: "",
      isEdit: false,
      chooseId: "",
      loading: false,
      time_type: [
        {
          id: "1",
          label: "工作日",
        },
        {
          id: "2",
          label: "休息日",
        },
        {
          id: "3",
          label: "节假日",
        },
        {
          id: "4",
          label: "其他",
        },
      ],
      userList: [],
      orgLIST:[],
      relUserList: [],
      addAll: [
        {
          detpart_all:'',
          checker_all: '',
          posts_name:'',
        }
      ],
      addDeptNum: [
        {
          detpartId: '',
          detpartName:'',
          checkerId: '',
          checker:'',
        }
      ],
      dialogFormVisible_detail: false,
      form_detail: {
        lcmc: '',
        cjr: '',
        cjsj: '',
        yydx: '',
        sfqy: '',
      },
      formLabelWidth_detail: '120px',
      markDis: false,
      dialog_loading:false,
      
    };
  },
  mounted() {
    // this.getUserList();
    this.orgList();
    this.getListData();
  },
  methods: {
    getUserList() {
      let transObj = {
        dept_id: dept_ids[0],
        all: '1',
        page: 1,
        limit: 9999,
      };
      org_users(transObj).then((res) => {
        if (res.code == 1) {
          this.userList = res.data.page_data;
        }
      });
    },
    //组织机构列表
    orgList() {
      let transObjs = {
        limit: 999,
        uo_pid:'385',
      }
      org_list(transObjs)
        .then(res => {
          if (res.code == "1") {
            let singleData = res.data.page_data;
            singleData.forEach(item => {
              this.orgLIST.push({
                label: item.uo_name,
                value: item.uo_id
              })
            });
          }
        })
        .catch(err => { console.log(err) });
    },
    //初始化列表数据
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        lc_status:this.type_value,
      };
      lcgl_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);
      });
    },
    //部门加1
    addOne() {
      this.addAll.push({
        detpart_all:'',
        checker_all: '',
        posts_name:'',
      });
    },
    //dialog-特定删除
    singleDel(item,index) {
      this.addAll.splice(index, 1);;
    },
    //新增dialog-新增
    addData() {
      this.title = "新建流程";
      this.dialogForm = {
        lcmc: '',
        applyObj:''
      };
      this.addAll= [
        {
          detpart_all: '',
          checker_all: '',
          posts_name: '',
        }
      ];
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    // 编辑
    editData(item) {
      this.dialog_loading = false;
      this.title = "编辑流程";
      this.isEdit = true;
      this.chooseId = item.lc_id;
      let transObj = {
        lc_id:item.lc_id
      };
      lcgl_detail(transObj)
        .then(res => {
          if (res.code == "1") {
            let singleDATA=res.data.one_info;
             //流程的审核
             let transD = JSON.parse(singleDATA.lc_splc);
            this.addAll = [];
            transD.forEach(item => {
              this.changeDepLinkNumber(item.department_id)
              this.addAll.push({
                detpart_all: {
                  value: item.department_id,
                  label: item.department_name
                },
                checker_all: {
                  label: item.user_name,
                  value: item.user_id
                },
                posts_name: item.position,
              });
              this.dialog_loading=false;
            })

            this.dialogForm = {
              lcmc: singleDATA.lc_name,
              applyObj:{label:singleDATA.lc_yydx_name,value:singleDATA.lc_yydx},
            };
          }
        })
      .catch(err=>{console.log(err)})
      this.dialogFormVisible = true;
    },
    //添加流程提交
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 编辑上送json格式
          let tempArr=[]
          this.addAll.forEach(item => {
            tempArr.push({
              department_name: item.detpart_all.label,
              department_id: item.detpart_all.value,
              user_name: item.checker_all.label,
              user_id: item.checker_all.value,
              position:item.posts_name,//职位
            })
          })
          let transObj = {
            issubmit:1,
            jdbg_xmlxsb_shlc: {
              lc_name: this.dialogForm.lcmc,
              lc_yydx_name:this.dialogForm.applyObj.label,
              lc_yydx:this.dialogForm.applyObj.value,//应用对象id
              lc_create_username:real_name,
              lc_status:1,//是否启用
              lc_splc:JSON.stringify(tempArr),//示例值：[{"department_name":"部门名称","department_id":"部门id","user_name":"审批人名称","user_id":"审批人id"}]
            }
          };
          if (!this.isEdit) {
            lcgl_add(transObj)
              .then((res) => {
                if (res.code == "1") {
                  this.dialogFormVisible = false;
                  this.getListData();
                }
              })
              .catch(err=>{console.log(err)})
          } else {
            transObj.lc_id = this.chooseId;
            lcgl_edit(transObj)
              .then((res) => {
                if (res.code == 1) {
                  this.$message({
                    message: res.msg,
                    type: "success",
                  });
                  this.dialogFormVisible = false;
                  this.getListData();
                } 
              })
              .catch(err=>{console.log(err)})
          }
        }
      })
    },
    //table中 switch开关
    changeStatus(val, item) {
      let transObj = {
        issubmit:1,
        lc_id:item.lc_id,
        jdbg_xmlxsb_shlc: {
          lc_status: val == true ? 1 : 2
        }
      }
      lcgl_edit(transObj)
        .then(res => {
          if (res.code == "1") {
            this.$message({
              message: res.msg,
              type: 'success'
            });
            this.getListData();
          }
        })
      .catch(err=>{console.log(err)})
    },
    //详情
    detailData(item) {
      console.log(item);
      this.form_detail = {
        lcmc: item.lc_name,
        cjr: item.lc_create_username,
        cjsj: item.create_dateline,
        yydx: item.lc_yydx_name,
        sfqy: item.lc_status,
      };
      this.markDis = true;
      this.dialogFormVisible_detail = true;
    },
    //删除
    delData(item) {
      let transObj = {
        lc_id: item.lc_id,
      };
      this.$confirm("确认删除该设置?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          lcgl_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(() => { });
    },
    //点击-应用对象
    applyDepart(val) {
     console.log(val,"00")
      // org_list()
    },
    //选择部门后请求以下成员
    changeDepLinkNumber(val) {
      this.relUserList = [];
      let tempArr = [];
      let transObj = {
        dept_id: val.value,
        all: '1',
        page: 1,
        limit: 999,
      };
      org_users(transObj).then((res) => {
        if (res.code == "1") {
          let changeDa = res.data.page_data;
          changeDa.forEach(item => {
            tempArr.push({
              label:item.user_realname,
              value:item.user_id,
            })
          });

          this.relUserList = tempArr;
        }
      });
    },
    //翻页
    handleCurrentChange(val) {
      this.cur_page = val;
      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;
    }
  }
}
.titleBi{
  margin-bottom:20px;
}
.singleItem{
  display:flex;
  height: 40px;
  line-height:40px;
  justify-content: space-between;
  margin-bottom:10px;
  h4{
    width:14%;
    text-align: center;
  }
  .formItem{
    width:30%;
  }
  .delIcon{
    width:12%;
    box-sizing:border-box;
    padding-left:5%;
    cursor: pointer;
  }
}
.addApprovel{
  margin-top:40px;
}
</style>
