<template>
  <div class="app-container">
    <el-card>
      
    <div style="margin-top: 10px">
      <el-button type="primary" icon="el-icon-plus" @click="handleCreate"
        >新增</el-button
      >
    </div>
    </el-card>
    <el-card style="margin-top: 10px">
      <el-table
        :data="wfstateList"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" />
        <el-table-column  width="180" label="名称">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
         <el-table-column  width="180" label="是否隐藏">
          <template slot-scope="scope"> 
            {{ !!(scope.row.is_hidde)?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column  width="180" label="顺序ID">
          <template slot-scope="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column  width="180" label="类型">
          <template slot-scope="scope">
             <el-tag effect="plain" v-if="scope.row.type==0">
                  普通类型
            </el-tag>
             <el-tag effect="plain" v-if="scope.row.type==1">
                  初始状态
            </el-tag>
            <el-tag effect="plain" v-if="scope.row.type==2">
                  结束状态
            </el-tag>
          </template>
        </el-table-column>
           <el-table-column  width="180" label="参与人类型">
          <template slot-scope="scope">{{ options_[scope.row.participant_type] }}</template>
        </el-table-column>
       
         <el-table-column  width="180" label="创建时间">
          <template slot-scope="scope">{{ scope.row.create_time }}</template>
        </el-table-column>
       

        <el-table-column
          align="center"
          label="操作"
          width="220px"
        >
          <template slot-scope="scope">
            <el-link
              v-if="checkPermission(['wfstate_update'])"
              @click="handleEdit(scope)"
              >编辑</el-link
            >
            <el-link
              v-if="checkPermission(['wfstate_delete'])"
              type="danger"
              @click="handleDelete(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
     
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑状态' : '新增状态'"
    >
      <el-form
        ref="Form"
        :model="wfstate"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="wfstate.name" placeholder="名称" />
        </el-form-item>
        <el-form-item
          label="是否隐藏"
          prop="is_hidden"
          label-width="120px"
        >
          <el-switch v-model="wfstate.is_hidden"></el-switch>
        </el-form-item>
         
      <el-form-item label="状态顺序" prop="sort">
          <el-input v-model="wfstate.sort" type="number"  placeholder="状态顺序" />
        </el-form-item>
       <el-form-item label="状态类型" prop="type">
         
           <el-select  style="width: 100%" v-model="wfstate.type" placeholder="请选择">
             <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
     <el-form-item
          label="允许撤回"
          prop="enable_retreat"
          label-width="120px"
        >
          <el-switch v-model="wfstate.enable_retreat"></el-switch>
        </el-form-item>
         <el-form-item label="参与者类型" prop="participant_type">
         
           <el-select  style="width: 100%" v-model="wfstate.participant_type" placeholder="请选择">
             <el-option
                v-for="item in typeoptions"
                :key="item.value"
                :label="item.label"
               :value="item.value">
              </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getWfStateList, createWfState,updateWfState,deleteWfState } from "@/api/workflow";
import checkPermission from "@/utils/permission";
const defaultwfstate = {
  name: "",
};
export default {
  components: { },
  name: "State",
  props: ["ID"],
  data() {
    return {
      wfstate: defaultwfstate,
      is_hidden:false,
      enable_retreat:false,
      wfstateList: {
        count:0
      },
      options_:{
        "0":'无处理',
        "1":'个人',
        "2":'多人',
        "3":'部门',
        "4":'角色',
        "5":'变量',
        "6":'普通类型',
        "5":'工单字段',
        "6":'父工单字段',
      },
      options: [{
          value: 0,
          label: '普通类型'
        }, {
          value: 1,
          label: '初始状态'
        }, {
          value: 2,
          label: '结束状态'
        }],
         typeoptions: [{
          value: 0,
          label: '无处理'
        }, {
          value: 1,
          label: '个人'
        }, {
          value: 2,
          label: '多人'
        }
        , {
          value: 3,
          label: '部门'
        }
        , {
          value: 4,
          label: '角色'
        }
        , {
          value: 5,
          label: '变量'
        }
        , {
          value: 7,
          label: '工单字段'
        }
        , {
          value: 8,
          label: '父工单字段'
        }],
      display_form_str:[],
      limit_expression:[],
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        sort: [{ required: true, message: "请输入", trigger: "blur" }],
        type:[{ required: true, message: "选择", trigger: "blur" }],

      },
    };
  },
 
  created() {
     
    this.getList();
  },
  methods: {
    checkPermission,
   
    getList() {
    
      getWfStateList(this.ID).then((response) => {

        if (response.data) {
          this.wfstateList = response.data;
        }
        
      });
    },
    
    
   
    handleCreate() {
      this.wfstate = Object.assign({}, defaultwfstate);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
   
    handleEdit(scope) {
      this.wfstate = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    
   
   
    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updateWfState(this.wfstate.id, this.wfstate).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          } else {
            this.wfstate.workflow=this.ID;
            createWfState(this.wfstate).then((res) => {
              if (res.code >= 200) {
                this.getList();
                 this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },


   handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteWfState(scope.row.id);
          this.getList();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
     


  },
};
</script>
