<!-- 人员交接 -->
<template>
  <view class="form-page">
	<scroll-view scroll-y class="form-scroll">
		<uni-forms v-model="form" label-width="190rpx"  ref="customForm" :rules="customRules">
		<view class="form-card" v-if="form.ticket_">
			<ticketd :ticket_="form.ticket_"></ticketd>
		</view>
		<view class="form-card">
			<view class="form-card-title">交接信息</view>
		<uni-forms-item label="需求部门" required name="dept_need">
			<uni-data-picker
				v-model="form.dept_need"
				:localdata="group"
				:disabled="mode === 'show'"
				:map="{ text: 'label', value: 'value', children: 'children' }"
				:show-all-levels="false"
				clearable
				placeholder="请选择部门"
			/>
		</uni-forms-item>

		<!-- 日期 -->
		<uni-forms-item label="日期" required name="join_date">
			<uni-datetime-picker
			type="date"
			:clear-icon="false"
			v-model="form.join_date"
			/>
		</uni-forms-item>
		</view>
		</uni-forms>
	</scroll-view>
    <!-- 人员列表 -->
    <view class="person-list">
      <view
        class="person-card"
        v-for="(item, index) in form.person"
        :key="index"
      >
        <uni-forms :modelValue="item">
          <uni-forms-item label="姓名" required>
            <uni-easyinput v-model="item.name" placeholder="请输入姓名" />
          </uni-forms-item>

          <uni-forms-item label="性别" required>
            <uni-data-select
              v-model="item.gender"
              :localdata="genderList"
              placeholder="请选择"
            />
          </uni-forms-item>

          <uni-forms-item label="身份证号" required label-width="190rpx">
            <uni-easyinput v-model="item.IDcard" />
          </uni-forms-item>

          <uni-forms-item label="手机号" required>
            <uni-easyinput v-model="item.phone" />
          </uni-forms-item>

          <uni-forms-item label="备注">
            <uni-easyinput v-model="item.note" />
          </uni-forms-item>

          <!-- 岗位（条件显示） -->
          <uni-forms-item
            v-if="showPost"
            label="岗位"
            required
          >
            <uni-data-select
              v-model="item.post"
              :localdata="postList"
              placeholder="请选择岗位"
            />
          </uni-forms-item>

          <!-- 删除 -->
          <button
            v-if="mode !== 'show'"
            class="del-btn"
            @click="removePerson(index)"
          >
            删除
          </button>
        </uni-forms>
      </view>
    </view>

    <!-- 新增人员 -->
    <button
      v-if="mode !== 'show'"
      class="form-btn form-btn-primary"
	  style="margin-bottom: 20rpx"
      @click="addPerson"
    >
      新增人员
    </button>

    <!-- 底部操作 -->
    <view class="footer_fixed">
      <button v-if="mode=='edit'" class="form-btn form-btn-danger" @click="handleDel" :loading="saveLoading" :disabled="saveLoading">
				删除
		</button>
		<button v-if="mode!='show'" class="form-btn form-btn-primary" @click="handleSave" :loading="saveLoading" :disabled="saveLoading">
			提交审批
		</button>
		<ticketd_b :workflow_key="'wf_empjoin'" v-if="form.ticket_ && mode == 'show'" :t_id="form.id" :ticket_="form.ticket_" 
		:ticket_data="ticket_data" @success="()=>{uni.navigateBack()}" ref="ticketd_b"></ticketd_b>
    </view>
  </view>
</template>
<script>

import { genTree } from "@/utils/verificate";
import ticketd_b from "../wf/ticketd_b.vue"
import ticketd from "../wf/ticketd.vue"
export default {
	components: { ticketd_b, ticketd },
  data() {
    return {
      mode: 'show',
	  t_id: null,
	  saveLoading: false,
	  ticket_data:{},
      form: {
        dept_need: '',
        join_date: '',
        person: [],
      },
      group: [],
      postList: [],
      genderList: [
        { text: '男', value: 1 },
        { text: '女', value: 2 }
      ]
    }
  },
  computed: {
    showPost() {
      const stateName = this.form?.ticket_?.state_?.name
	  return ['交接部门负责人', '结束'].includes(stateName)
    }
  },
  watch: {
        'form.person'(val){
            this.ticket_data.person = val
  		},
  },
  async onLoad(options) {
	let that = this;
	that.mode = options.mode?options.mode:'show';
	that.t_id = options.t_id?options.t_id:null;
	if(that.t_id) {
			const res = await that.$api.empjoinItem(that.t_id);
			this.form = res
			if(that.form.ticket_.state_.type == 1 && that.form.create_by == uni.getStorageSync("userInfo").id ) {
				that.mode = "edit";
			}else{
				that.mode = "show";
			}
		}
	this.getGroup();
	this.getPost();
	},

  methods: {
    addPerson() {
      this.form.person.push({
        name: '',
        gender: '',
        IDcard: '',
        phone: '',
        note: '',
        post: ''
      })
    },
    removePerson(index) {
      this.form.person.splice(index, 1)
    },
    async getGroup() {
      const res = await this.$api.deptList({ page: 0 })
      this.group = genTree(res);
    },
	async getPost(){
		const res = await this.$api.postList({ page: 0 })
		this.postList = res.map(item =>({
			text: item.name,
			value: item.id
		}
      )
        )
	},
    async handleSave() {
      await this.$api.empjoinCreate(this.form)
      uni.showToast({ title: '提交成功' })
      uni.navigateBack()
    },
	async handleDel(){
		let that = this;
		await that.$api.empjoinDelete(that.form.id)
		uni.navigateBack()
	},
  }
}

</script>

<style lang="scss" scoped>
.form-page { min-height: 100vh; background: #F0F2F5; }
.form-scroll { padding: 0; padding-bottom: 180rpx; }
.form-card { background: #fff; border-radius: 0; padding: 24rpx; margin-bottom: 0; box-shadow: none; }
.form-card-title { font-size: 30rpx; font-weight: 600; color: #1F2937; margin-bottom: 16rpx; padding-left: 16rpx; border-left: 6rpx solid #2BA471; }
.form-textarea { width: 100%; min-height: 160rpx; border: 2rpx solid #E5E7EB; border-radius: 12rpx; padding: 16rpx; font-size: 28rpx; color: #374151; box-sizing: border-box; background: #F9FAFB; }
.form-btn { flex: 1; height: 80rpx; line-height: 80rpx; border-radius: 14rpx !important; font-size: 28rpx; font-weight: 500; border: none !important; }
.form-btn-primary { background: linear-gradient(135deg, #2BA471, #1F8C5E) !important; color: #fff !important; box-shadow: 0 4rpx 12rpx rgba(43,164,113,0.2); }
.form-btn-danger { background: linear-gradient(135deg, #EF4444, #DC2626) !important; color: #fff !important; box-shadow: 0 4rpx 12rpx rgba(239,68,68,0.3); }
.uni-data-checklist .checklist-group .checklist-box{
	margin: 10px 0!important;
}
.flex_file_picker>.uni-file-picker__files{
	flex-direction: row;
}
.flex_file_picker{
	width: 90px;
	flex: none;
}
.del-btn {
	height: 56rpx;
	line-height: 56rpx;
	padding: 0 20rpx;
	font-size: 24rpx;
}
</style>