<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="巡检计划编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plancode">
              <a-input v-model="model.plancode" placeholder="请输入巡检计划编号"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="巡检计划名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planname">
              <a-input v-model="model.planname" placeholder="请输入巡检计划名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chargeruser">
              <!-- <a-input v-model="model.chargeruser" placeholder="请输入待办人"  ></a-input> -->
              <j-dict-select-tag v-model="model.chargeruser" placeholder="请选择负责人" dictCode="sys_user,realname,username"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否开启时间设置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="iftimeset">
              <j-dict-select-tag v-model="model.iftimeset"  placeholder="" type="radio" dictCode="common_yes_no"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag v-model="model.status"  type="radio" dictCode="common_status"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.iftimeset === 'Y'">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="begintime">
              <j-date placeholder="请选择开始时间" v-model="model.begintime"  style="width: 100%" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.iftimeset === 'Y'">
            <a-form-model-item label="重复周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repeatnum">
              <a-input-number v-model="model.repeatnum" placeholder="请输入重复周期" style="width: 40%;"/>
              <j-dict-select-tag  v-model="model.repeattype"  placeholder="请选择重复单位" dictCode="plan_cycle_unit" style="width: 30%;"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="重复类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repeattype">
              
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12" v-if="model.iftimeset === 'Y'">
            <a-form-model-item label="提前通知时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noticenum">
              <a-input-number v-model="model.noticenum" placeholder="请输入提前通知时间" style="width: 40%" />
              <j-dict-select-tag v-model="model.noticetype" placeholder="请选择提前通知单位" dictCode="plan_notice_unit" style="width: 30%;"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="提前通知类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noticetype">
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <!-- <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input> -->
              <a-textarea v-model="model.remark" rows="2" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'InspectPlanForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          iftimeset: 'Y',
          status: '0',
          noticetype: '天',
          repeattype: '日',
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          planname: [
            { required: true, message: '请输入巡检计划名称!'},
          ],
          chargeruser: [
            { required: true, message: '请选择负责人!'},
          ],
          iftimeset: [
            { required: true, message: '请选择!'},
          ],
          status: [
            { required: true, message: '请选择!'},
          ],
          begintime: [
            { required: true, message: '请选择开始时间!'},
          ],
          repeatnum: [
            { required: true, message: '请输入重复周期!'},
          ],
          noticenum: [
            { required: true, message: '请输入提前通知时间!'},
          ],
        },
        url: {
          add: "/cmmsInspectPlan/cmmsInspectPlan/add",
          edit: "/cmmsInspectPlan/cmmsInspectPlan/edit",
          queryById: "/cmmsInspectPlan/cmmsInspectPlan/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm (list) {
        console.log(list)
        const that = this;
        if(list.length === 0){
          that.$message.warning('计划列表未填写');
          return
        }
        this.model.taskList = list
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          } else {
            that.$message.warning('计划设置参数未填写完全');
          }
         
        })
      },
    }
  }
</script>