<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew">
          <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="state_value"
            clearable
            @clear="getListData"
            placeholder="全部"
            @change="getListData"
          >
            <el-option
              v-for="item in state_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="applyTable"
      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="60">
      </el-table-column>
      <el-table-column
        label="流程名称"
        show-overflow-tooltip
        prop="lc_name"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        prop="user_name"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        show-overflow-tooltip
        prop="create_time"
      ></el-table-column>
      <el-table-column label="应用对象" show-overflow-tooltip>
        <template slot-scope="scope">
          <div v-if="scope.row.lc_yydx_name == ''">全部</div>
          <div v-else>{{ scope.row.lc_yydx_name }}</div>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" width="220">
        <template slot-scope="scope">
          <el-switch
            @change="isOpen(scope.row)"
            v-model="scope.row.lc_status"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="140" align="right">
        <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>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="580px">
      <el-form
        :model="dialogForm"
        label-width="100px"
        style="min-height: 280px"
      >
        <el-form-item label="流程名称" prop="lc_name">
          <el-input
            v-model="dialogForm.lc_name"
            placeholder="请输入流程名称"
          ></el-input>
        </el-form-item>
        <div class="processui">
          <h3>审批流程设计</h3>
          <div class="">
            <el-form-item
              v-for="(sj, sjindex) in lc_splc"
              class="process "
              :key="sjindex"
              label="审批部门"
            >
              <el-select
                style="width: 110px; margin-right: 10px"
                @change="bmChange($event, sj)"
                v-model="sj.dept_id"
                filterable
                placeholder="请选择部门"
              >
                <el-option
                  v-for="bm in bmList"
                  :label="bm.uo_name"
                  :value="bm.uo_id"
                  :key="bm.uo_id"
                />
              </el-select>
              审批人
              <el-select
                style="width: 110px; margin-right: 20px"
                @change="sprChange"
                filterable
                v-model="sj.user_id"
                placeholder="请选择审批人"
              >
                <el-option
                  v-for="r in sj.sprList"
                  :label="r.user_realname"
                  :value="r.user_id"
                  :key="r.user_id"
                />
              </el-select>
              <el-input
                style="width: 105px; margin-right: 8px"
                v-model="sj.position"
                placeholder="请输入职位"
              />
              <i
                @click="deleteSp(sjindex)"
                class="el-icon-delete font-size-25 color-13 hand"
              ></i>
            </el-form-item>
            <p class="text-red font-size-12">*审批顺序按照添加顺序依次审批</p>
            <el-button @click="addSp" type="success" icon="el-icon-plus"
              >添加</el-button
            >
          </div>
        </div>
        <el-form-item class="mt60 mb40" label="应用对象" prop="lc_yydx">
          <el-select
            style="width: 100%"
            v-model="dialogForm.lc_yydx"
            placeholder="请选择应用对象"
            ><el-option value="" label="全部">全部</el-option>
            <el-option
              v-for="bm in yydxList"
              :label="bm.uo_name"
              :key="bm.uo_id"
              :value="bm.uo_id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { useUserStore } from "@/stores/user";

const { real_name, dept_ids } = useUserStore();
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import {
  org_users,
  lcgl_list,
  lcgl_add,
  lcgl_edit,
  org_list,
  lcgl_del,
} from "./api";

export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      state_value: "",
      loading: false,
      state_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "启用",
          value: "1",
        },
        {
          label: "未启用",
          value: "2",
        },
      ],
      keyword: "",
      title: "",
      activeName: "first",
      chcy: [],
      shjd: [],
      lc_splc: [
        {
          user_id: "", //审批人
          dept_id: "", //审批部门id
          sprList: [],
          position: "",
        },
        {
          user_id: "", //审批人
          dept_id: "", //审批部门id
          sprList: [],
          position: "",
        },
      ],
      bmList: [],
      yydxList: [],
      sprList: [],
      applyTable: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      auditVisible: false,
      code: false,
      dialogForm: {
        lc_name: "",
        lc_id: "",
        cjr: "",
        cjsj: "",
        lc_splc: [],
        lc_yydx: {},
        lc_status: "",
      },
      isEdit: false,
      timestamp: Date.now(),
      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;
      //   }
      // });
    },
    getOrgList() {
      let data = {};
      org_list(data).then((res) => {
        if (res.code == 1) {
          this.bmList = res.data.page_data;
          this.yydxList = res.data.page_data;
        }
      });
    },
    getlcglList() {
      this.loading = true;
      let data = {
        page: 1,
        site: "jnmf",
        limit: 999,
      };
      lcgl_list(data).then((res) => {
        this.loading = false;
        this.applyTable = res.data.page_data;
        this.applyTable.forEach((item) => {
          item.lc_status = item.lc_status == 1 ? true : false;
        });
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        site: "jnmf",
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        lc_status: this.state_value,
        // lc_name: this.dialogForm.lc_name,
      };
      lcgl_list(data).then((res) => {
        if (res.code == 1) {
          this.applyTable = res.data.page_data;
          this.applyTable.forEach((item) => {
            item.lc_status = item.lc_status == 1 ? true : false;
          });
          this.cur_page = Number(res.data.page_now);
          this.total = Number(res.data.total_rows);
          this.loading = false;
        }
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    createNew() {
      this.isEdit = false;
      this.title = "新建流程";
      this.dialogForm = {
        lc_name: "",
        lc_yydx: "",
        lc_status: "",
        lc_splc: "",
      };
      this.getOrgList();
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.isEdit = true;
      this.title = "编辑流程";
      this.dialogForm = {
        lc_name: item.lc_name,
        lc_status: item.lc_status,
        lc_yydx: item.lc_yydx,
        lc_id: item.lc_id,
      };
      this.lc_splc = JSON.parse(item.lc_splc);
      this.lc_splc.forEach((ele) => {
        ele.sprList = [];
        this.bmChangeTwo(ele.dept_id, ele);
      });
      this.getOrgList();
      this.timestamp = Date.now();
      this.dialogFormVisible = true;
    },
    bmChangeTwo(e, sj) {
      let data = {
        site: "jnmf",
        admin: "1",
        all: "1",
        dept_id: e,
        limit: '999',
        page: 1,
      };
      org_users(data).then((res) => {
        if (res.code == 1) {
          sj.sprList = res.data.page_data;
          this.$forceUpdate();
        }
      });
      // for (var i = 0; i < this.bmList.length; i++) {
      //   var oneSubject = this.bmList[i];
      //   if (e === oneSubject.id) {
      //     this.sprList = oneSubject.spr;
      //   }
      // }
    },
    // 是否启用
    isOpen(item) {
      let status;
      if (item.lc_status == false) {
        status = 2;
      } else {
        status = 1;
      }
      let data = {
        jdbg_sbcg_lcgl: {
          lc_status: status,
        },
      };
      Object.assign(data, {
        lc_id: item.lc_id,
        site: "jnmf",
        issubmit: 1,
      });
      lcgl_edit(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            type: "success",
            message: "更改成功!",
          });
        }
      });
    },
    delData(item) {
      let data = {
        lc_id: item.lc_id,
      }
      this.$confirm("此操作将永久删除该流程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          lcgl_del(data).then((res) => {
            if (res.code == 1) {
              this.getListData();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    bmChange(e, sj) {
      let data = {
        site: "jnmf",
        admin: "1",
        all: "1",
        limit: '999',
        page:'1',
        dept_id: e,
      };
      org_users(data).then((res) => {
        if (res.code == 1) {
          sj.sprList = res.data.page_data;
          sj.user_id = "";
          this.$forceUpdate();
        }
      });
    },
    sprChange(e) {},
    addSp() {
      this.lc_splc.push({
        user_id: "", //审批人
        dept_id: "", //审批部门id
        sprList: [],
        position: "",
      });
    },
    deleteSp(index) {
      this.lc_splc.splice(index, 1);
    },
    formSubmit() {
      let flag = false;
      let a = [];
      this.lc_splc.forEach((item) => {
        let b = {};
        if (!item.user_id || !item.dept_id || !item.position) {
          flag = true;
        } else {
          this.bmList.forEach((items) => {
            let user_name, dept_name;
            if (items.uo_id == item.dept_id) {
              dept_name = items.uo_name;
              item.sprList.forEach((ele) => {
                if (ele.user_id == item.user_id) {
                  user_name = ele.user_realname;
                }
              });
              b = {
                user_id: item.user_id,
                user_name: user_name,
                dept_id: item.dept_id,
                dept_name: dept_name,
                position: item.position,
              };
            }
          });
          a.push(b);
        }
      });
      let uo_name;
      this.yydxList.forEach((i) => {
        if (i.uo_id === this.dialogForm.lc_yydx) {
          uo_name = i.uo_name;
        }
      });
      if (flag) {
        return this.$message.error("审批部门、审批人和职位必填");
      }
      let data = {
        site: "jnmf",
        issubmit: 1,
        jdbg_sbcg_lcgl: {
          lc_name: this.dialogForm.lc_name,
          lc_yydx: this.dialogForm.lc_yydx,
          lc_yydx_name: uo_name,
          // lc_status:this.dialogForm.lc_status,
          lc_status: 1,
          lc_splc: JSON.stringify(a),
        },
      };
      if (!this.isEdit) {
        lcgl_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 {
        Object.assign(data, {
          lc_id: this.dialogForm.lc_id,
        });
        lcgl_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",
            });
          }
        });
      }
    },
    add() {
      let data = {
        site: "jnmf",
        issubmit: 1,
        jdbg_sbcg_lcgl: {
          lc_name: "希望小学流程3",
          lc_yydx: 371,
          lc_yydx_name: "希望小学",
          // lc_status:this.dialogForm.lc_status,
          lc_status: 1,
          // lc_splc: '[{"user_id":"864","user_name":"许胡明","dept_id":"352","dept_name":"内部测试中心小学"}]',
          lc_splc:
            '[{"user_id":"1","user_name":"管理员","dept_id":"291","dept_name":"浙江省教育厅"}]',
        },
      };
      lcgl_add(data).then((res) => {});
    },
    detailData(row) {
      this.$router.push({
        name: "lcgl_detail",
        params: {
          id: row.lc_id,
        },
      });
    },
    verify(type, item) {
      let data = {
        kylx_zt: type,
      };
      //   kylx_verify(data).then((res) => {
      //     if (res.code == 1) {
      //       this.$message({
      //         message: "审核成功！",
      //         type: "success",
      //       });
      //       this.getListData();
      //     }
      //   });
    },
  },
  mounted() {
    // this.add();
    this.getUserList();
    this.getlcglList();
    this.getListData();
  },
};
</script>
