<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="time">
          <el-date-picker
              style="width: 100%"
              @change="getListData"
              value-format="yyyy-MM-dd"
              v-model="time"
              type="date"
              placeholder="请选择日期"
          ></el-date-picker>
        </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
          label="序号"
          align="center"
          type="index"
          width="50"
      ></el-table-column>
      <el-table-column
          label="名称"
          show-overflow-tooltip
          prop="wlc_name"
          width="auto"
      ></el-table-column>
      <el-table-column label="流程类型" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.wlc_cate_option_k == 5">自定义流程</span>
        </template>
      </el-table-column>

      <el-table-column
          label="创建时间"
          width="150"
          prop="wlc_cjsj"
      ></el-table-column>
      <el-table-column
          label="创建者"
          prop="wlc_cjr"
          width="120"
      ></el-table-column>
      <el-table-column
          label="审核人"
          prop="wlc_shr"
          width="150"
      ></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑</el-button>
          <el-button type="text" @click="delData(scope.row.wlc_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="cur_page"
          @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="dialogForm" label-width="80px">
        <el-form-item label="流程名称" prop="lcmc">
          <el-input
              v-model="dialogForm.lcmc"
              placeholder="请输入流程名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="cjsj">
          <el-date-picker
              style="width: 100%"
              v-model="dialogForm.cjsj"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人员" prop="shry">
          <el-select
              style="width: 100%"
              v-model="dialogForm.shry"
              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>
        <el-form-item label="流程附件">
          <FileUpload :limit="5" v-if="dialogFormVisible" :full.sync="dialogForm.wlc_zdyfj" :key="timestamp" multiple>
            <div slot="tip" class="el-upload__tip">
              支持文件类型:doc(.docx)、.ppt(.pptx)、.xls(.xlsx)、.pps、.wps、.pdf、.txt文件格式。
            </div>
          </FileUpload>
        </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 { useUserStore } from "@/stores/user";
const { real_name,dept_ids } = useUserStore();
import FileUpload from "@/components/FileUpload/index.vue";
import { lcgl_list, lcgl_add, lcgl_edit, lcgl_del,org_users } from "./api";
export default {
  name: "index",
  components:{
    FileUpload
  },
  data() {
    return {
      time: "",
      state_value: "",
      keyword: "",
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        lcmc: "",
        cjsj: "",
        lclx: "5",
        wlc_zdyfj:'',
        shry: {},
      },
      dx_list: [
        {
          label: "后勤处",
          value: "1",
        },
        {
          label: "校办",
          value: "2",
        },
        {
          label: "工会",
          value: "3",
        },
        {
          label: "体育部",
          value: "4",
        },
      ],
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      userList:[],
      timestamp: Date.now(),
    };
  },
  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,
        wlc_cate: '5',
        wlc_cjsj: this.time,
      };

      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);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "新建自定义流程";
      this.dialogForm = {
        lcmc: "",
        lclx: "5",
        cjsj: "",
        yydx: "",
        wlc_zdyfj:'',
        shry: {},
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑自定义流程";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.wlc_id;
      this.dialogForm.lcmc = item.wlc_name;
      this.dialogForm.lclx = item.wlc_cate_option_k;
      this.dialogForm.shry = {
        label: item.wlc_shr,
        value: item.wlc_shr_user_id
      };
      this.dialogForm.cjsj = item.wlc_cjsj;
      this.dialogForm.wlc_zdyfj = item.wlc_zdyfj;
    },
    formSubmit() {
      let data = {
        wlc_name: this.dialogForm.lcmc,
        wlc_cate: this.dialogForm.lclx,
        wlc_obj: this.dialogForm.yydx,
        wlc_cjsj: this.dialogForm.cjsj,
        wlc_cjr: real_name,
        wlc_shr: this.dialogForm.shry.label,
        wlc_shr_user_id: this.dialogForm.shry.value,
        wlc_zdyfj: this.dialogForm.wlc_zdyfj
      };
      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 {
        data.wlc_id = this.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",
            });
          }
        });
      }
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    delData(id) {
      let data = {
        wlc_id: id,
      };
      this.$confirm("确认删除该自定义流程?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            lcgl_del(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "自定义流程删除成功！",
                  type: "success",
                });
                this.getListData();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          }).catch(() => {});
    },
  },
  mounted() {
    this.getUserList();
    this.getListData();
  },
};
</script>

<style scoped>
</style>