<template>
  <t-card :bordered="false">
    <div class="form-step-container">
            <t-button @click="add">新增</t-button>
      <t-table
        rowKey="index"
        :data="list"
        :columns="columns"
        :stripe="true"
        :bordered="false"
        :hover="true"
        size="large"
        table-layout="auto"
        cellEmptyContent="-"
      >
        <template #type="{ row }">
          <t-tag theme="primary" v-if="row.type==1">理事会</t-tag>
          <t-tag theme="success" v-if="row.type==2">监事会</t-tag>
        </template>
        <template #select="{ row }">
          <t-space size="24px">
            <t-button theme="warning" @click="edit(row)">编辑</t-button>
            <t-popconfirm  content="确认删除吗" @confirm="del(row)">
              <t-button theme="danger" >删除</t-button>
            </t-popconfirm>
          </t-space>
        </template>
      </t-table>
    </div>
    <t-dialog header="新增职位" :visible="addMode" :onClose="onCloseMy" @confirm="onSubmit">
      <t-form>
        <t-form-item label="职位名称" name="name">
          <t-input placeholder="请输入职位名称" v-model="position_name"/>
        </t-form-item>
        <t-form-item label="职位类别" name="name">
          <t-select v-model="type" :style="{ width: '200px' }">
            <t-option label="理事会" value="1"></t-option>
            <t-option label="监事会" value="2"></t-option>
          </t-select>
        </t-form-item>
      </t-form>
    </t-dialog>
  </t-card>

</template>
<script lang="ts">
export default {
  data() {
    return {
      position_name: '',
      type:null,
      addMode: false,
      isEdit: false,
      editID:0,
      list: [],
      columns: [
        {colKey: 'position_name', title: '职位名称'},
        {colKey: 'type', title: '职位类别'},
        {colKey: 'select', title: '操作', width: 200},
      ],
    }
  },
  mounted() {
    //this.member_id=this.$route.query.id;
    this.getList();
  },
  methods: {
    del(d){
      console.log(d);
      this.$request
        .post('/position/del',{id:d.id})
        .then((res) => {
          if(res.code==1){
            this.$message.success(res.msg);
            this.getList();
          }else {
            this.$message.error(res.msg);
          }
          console.log(res);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    add(){
      this.addMode=true;
      this.isEdit=false;
    },
    edit(d){
      console.log(d);
      this.editID=d.id;
      this.position_name=d.position_name;
      this.type=String(d.type);
      this.addMode=true;
      this.isEdit=true;
    },
    getList() {
      this.$request
        .post("/position")
        .then((res) => {
          console.log(res);
          this.list = res.data;
        })
        .catch((e) => {
          console.log(e);
        });
    },
    onSubmit() {
      if (this.position_name == '') {
        this.$message.error('职位名称不能为空');
        return;
      }
      if (this.type == null) {
        this.$message.error('职位类别不能为空');
        return;
      }
      var url='/position/add';
      if(this.isEdit){
        url='/position/update';
      }
      this.$request
        .post(url,{position_name:this.position_name,type:this.type,id:this.editID})
        .then((res) => {
          if(res.code==1){
            this.$message.success(res.msg);
            this.addMode=false;
            this.getList();
          }else {
            this.$message.error(res.msg);
          }
          console.log(res);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    onCloseMy() {
      this.addMode = false;
    }
  }
}
</script>
<style scoped lang="less">

</style>
