<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="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
<!--            <a-form-model-item label="类型(1-服务；2-维修)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">-->
<!--              <a-input-number v-model="model.type" placeholder="请输入类型(1-服务；2-维修)" style="width: 100%" />-->
<!--            </a-form-model-item>-->
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="radio" v-model="model.type" placeholder="请选择类型" dictCode="service_repair_type"
                                 @change="e=>handleChangeType(e)" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="model.type == 1" >
<!--            <a-form-model-item label="服务模块(1-物品；2-服务内容)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceType">-->
<!--              <a-input-number v-model="model.serviceType" placeholder="请输入服务模块(1-物品；2-服务内容)" style="width: 100%" />-->
<!--            </a-form-model-item>-->
            <a-form-model-item label="服务模块" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceType">
              <j-dict-select-tag type="radio" v-model="model.serviceType" placeholder="请选择服务模块" dictCode="service_module"
                                 @change="e=>handleChangeServiceType(e)" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-show="model.type == 1 && model.serviceType == 1">
<!--            <a-form-model-item label="关联物品分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">-->
<!--              <a-input v-model="model.categoryId" placeholder="请输入关联物品分类"  ></a-input>-->
<!--            </a-form-model-item>-->
            <a-form-model-item label="物品分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
<!--              <a-input v-model="model.categoryId" placeholder="请输入关联物品分类"  ></a-input>-->
              <a-select
                      show-search
                      placeholder="请物品分类"
                      v-model="model.categoryId"
                      option-filter-prop="children"
              >
                <a-select-option v-for="item in categoryData" :key="item.id" >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </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: 'serviceRepairForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入名称!'},
           ],
           type: [
              { required: true, message: '请选择类型!'},
           ],
        },
        url: {
          add: "/business/busServiceRepair/add",
          edit: "/business/busServiceRepair/edit",
          queryById: "/business/busServiceRepair/queryById",
          queryCategoryData: "/business/busServiceRepairCategory/queryList"
        },
        categoryData:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      getAction(this.url.queryCategoryData,{}).then((res)=>{
        if(res.success){
          this.categoryData = res.result
        }else{

        }
      })
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        if (!record.id){
          record.type = 1;
          record.serviceType = 1;
        }
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
              var info = JSON.parse(localStorage.getItem("storeInfo"));
              this.model.hotelId = info.id;
            }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;
            })
          }

        })
      },
      handleChangeType(e){
        if (e != 1){
          this.model.serviceType = 0
          this.model.categoryId = '';
        }
      },
      handleChangeServiceType(e){
        if (e != 1){
          this.model.categoryId = '';
        }
      }
    }
  }
</script>
