<template>
  <el-container>
    <el-main>
      <el-form :model="localForm" label-width="150px" label-position="left" :rules="rules">
      <el-form-item label="名称" prop="name">
          <el-input v-model="localForm.name" clearable :disabled="localMode ==='show'"></el-input>
      </el-form-item>
        <el-form-item label="日期" prop="p_date">
          <el-date-picker
                v-model="localForm.p_date"
                type="date"
                align="right"
                value-format="YYYY-MM-DD"
                unlink-panels
                placeholder="选择日期"
                :disabled="localMode==='show'"
                >
                </el-date-picker>
      </el-form-item>
      <el-form-item label="需求部门" prop="p_dept">
          <el-cascader
              v-model="localForm.p_dept"
              :options="group"
              :props="groupsProps"
              :show-all-levels="false"
              clearable
              style="width: 220px"
          ></el-cascader>
      </el-form-item>
      <el-form-item label="建设期" prop="const">
          <el-input v-model="localForm.const" clearable ></el-input>
      </el-form-item>
        <el-form-item label="市级平台" prop="city_p">
            <el-radio-group v-model="localForm.city_p" :disabled="localMode ==='show'">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="省级平台" prop="province_p">
            <el-radio-group v-model="localForm.province_p" :disabled="localMode ==='show'">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="负责人" prop="charge">
          <el-input v-model="localForm.charge" clearable ></el-input>
      </el-form-item>
      <el-form-item label="平台基本情况与目标绩效" prop="condition">
          <el-input 
            type="textarea" 
            v-model="localForm.condition" 
            placeholder="请输入平台基本情况与目标绩效"
            rows = '3'
            clearable 
            ></el-input>
      </el-form-item>
      </el-form>
    <el-footer>
          <ticketd_b
            :workflow_key="'wf_plat'"
            :title="ticketTitle"
            :t_id="localForm.id"
            :ticket_="localForm.ticket_"
            :ticket_data="ticket_data"
            :submit_b_func="submit_b_func"
            ref="ticketd_b"
            @success="$emit('success', localMode)"
          />
    </el-footer>
    </el-main>
    <el-aside v-if="localForm.ticket_">
        <ticketd
        :ticket_="localForm.ticket_"
        :ticket_data="ticket_data"
        @success="$emit('success')"
        style="margin-top: 20px;"
        />
    </el-aside>
  </el-container>
</template>

<script>
import ticketd_b from "@/views/wf/ticketd_b.vue";
import ticketd from '@/views/wf/ticketd.vue';
import { genTree } from "@/utils/verificate";

export default {
  props: {
    mode: { type: String, default: "show" }, // add / edit / show
    t_id: {
		    type: String,
			  default: null
		},
    },
  components: {ticketd, ticketd_b},
  data() {
    return {
      ticket_data: {},
      ticketTitle: "平台审批",
      localMode : this.mode,
      localForm: {},
      groupsProps: {
                multiple: false,
                emitPath: false,
                checkStrictly: true,
            },
      group:[],
      rules: {
          name: [{required: true, message: "请输入名称", trigger: "blur"}],
          p_dept: [{required: true, message: "请输入部门", trigger: "change"}],
          city_p: [{required: true, message: "请选择市级平台", trigger: "change"}],
          const: [{required: true, message: "建设期", trigger: "blur"}],
          condition: [{required: true, message: "平台基本情况与目标绩效", trigger: "change"}]
        },
    };
  },
  mounted(){
    let that=this
    this.getGroup();
    if(that.t_id){
      that.getTid();
    }else{
      that.localMode = "add";
      that.localForm.belong_dept = that.$TOOL.data.get("USER_INFO").belong_dept;
    }
  },
  methods: {
    async submit_b_func() {
		    let that = this;
        if(that.localForm?.id){
        that.localMode = "edit";
      }
			if(that.localMode == "add") {
				let res = await that.$API.srm.platform.create.req(that.localForm);
				that.localForm.id = res.id;
			} else if (that.localMode == "edit") {
			    await that.$API.srm.platform.update.req(that.localForm.id, that.localForm);
			}
		},
    async getGroup() {
            let res = await this.$API.system.dept.list.req({ page: 0 });
            this.group = genTree(res);
        },
    getTid (){
			var that = this;
		    if (that.t_id) {
				that.$API.srm.platform.item.req(that.t_id).then(res=>{
          that.localForm = res;
					if(res.ticket_?.state_?.type == 1 && res.create_by == that.$TOOL.data.get("USER_INFO").id ) {
						that.localMode = "edit";
					}else{
						that.localMode = "show";
					}
        })
			}
		},
  },
};
</script>