<template>
  <basic-container>
    <avue-form ref="crud" :option="option" v-model="form" @submit="handleSubmit"/>
  </basic-container>
</template>

<script>
  import {leaveProcess} from "@/api/work/process";
  import {deviceorder} from "../../../../api/work/process";
  import {dateFormat} from "@/util/date";

  export default {
    data() {
      return {
        form: {},
        option: {
          group: [
            {
              icon: 'el-icon-info',
              label: '告警设备',
              prop: 'group1',
              column: [
                {
                  label: '告警设备',
                  prop: 'deviceId',
                  type: 'select',
                  // dicUrl: '',
                  dicData: [{
                    label: '设备1',
                    value: 1,
                  }],
                  span: 8,
                  rules: [
                    {
                      required: true,
                      message: '请选择告警设备',
                      trigger: 'blur'
                    }
                  ]
                },{
                  label: '设备类型',
                  prop: 'deviceType',
                  type: 'select',
                  display: false,
                  span: 8,
                },{
                  label: '告警类型',
                  prop: 'alarmType',
                  type: 'select',
                  dicUrl: "/api/blade-system/dict-biz/dictionary?code=alarm_type",
                  props:{
                    label: "dictValue",
                    value: "dictKey",
                  },
                  span: 8,
                  rules: [
                    {
                      required: true,
                      message: '请选择告警类型',
                      trigger: 'blur'
                    }
                  ]
                },{
                  label: '告警地点',
                  prop: 'alarmPosition',
                  span: 8,
                  rules: [
                    {
                      required: true,
                      message: '请填写所在地点',
                      trigger: 'blur'
                    }
                  ]
                },
                {
                  label: '告警原因',
                  prop: 'alarmReason',
                  type: 'textarea',
                  span: 24,
                },
              ]
            },
            {
              icon: 'el-icon-info',
              label: '受理人',
              prop: 'group2',
              column: [
                {
                  label: '受理人',
                  prop: 'handlePerson',
                  type: 'select',
                  dicUrl: `/api/blade-user/user-list`,
                  props: {
                    label: "account",
                    value: "id",
                  },
                  span: 8,
                  rules: [
                    {
                      required: true,
                      message: '请选择受理人员',
                      trigger: 'blur'
                    }
                  ],
                  change: ({value,column}) => {
                    if (!this.validatenull(value)){
                      if (this.$refs.crud.DIC.hasOwnProperty(column.prop)){
                        let person = this.$refs.crud.DIC[column.prop].find(item => item.id == value);
                        if (!this.validatenull(person) && !this.validatenull(person.phone)){
                          return this.form.handlePhone = person.phone
                        }
                      }
                    }
                    this.form.handlePhone = '';
                  },
                },
                {
                  label: '受理人电话',
                  prop: 'handlePhone',
                  labelWidth: 120,
                  detail: true,
                  span: 8,
                  rules: [
                    {
                      required: true,
                      message: '请填写联系方式',
                      trigger: 'blur'
                    }
                  ]
                },
              ]
            }
          ],
        }
      }
    },
    methods: {
      handleSubmit() {
        const params = {
          processDefinitionId: this.$route.params.processDefinitionId,
          alarmTime : dateFormat(new Date(),"yyyy-MM-dd hh:mm:ss"),
          ...this.form,
        };
        deviceorder(params).then(resp => {
          const data = resp.data;
          if (data.success) {
            this.$message.success(data.msg);
            this.$router.$avueRouter.closeTag();
            this.$router.push({path: `/order/deviceorder`});
          } else {
            this.$message.error(data.msg || '提交失败');
          }
        });
      }
    }
  }
</script>
