<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">

        <a-form-model-item label="开票内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoiceCategoryId">
          <j-search-select-tag placeholder="请选择开票内容" v-model="model.invoiceCategoryId" :dictOptions="invoiceCategoryOptions" :disabled="model.id ? true : false"/>
        </a-form-model-item>
        <a-form-model-item label="启用状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isOn">
          <j-dict-select-tag type="radio" v-model="model.isOn" placeholder="请选择"
                             dictCode="is_on"></j-dict-select-tag>
        </a-form-model-item>

      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

  export default {
    name: "HcwInvoiceCategoryModal",
    components: {
      JSearchSelectTag
    },
    data () {
      return {
        title:"操作",
        width:680,
        visible: false,
        model: {
          isOn: 1
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          invoiceCategoryId: [
            { required: true, message: '请选择开票内容!'},
          ],
          isOn: [
                  { required: true, message: '请选择启用状态!'},
            ],
        },
        url: {
          add: "/wisdom/hlwSubcontractorInvoiceCategory/add",
          edit: "/wisdom/hlwSubcontractorInvoiceCategory/edit",
          findAllList: '/wisdom/hlwInvoiceCategory/findAllList'
        },
        invoiceCategoryOptions: [],
      }
    },
    created () {
    },
    methods: {
      add (record) {
        this.edit({
          isOn: 1,
          subcontractorId: record.subcontractorId
        });
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.findAllList();
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        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';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = this.model;
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          } else {
            return false;
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.model = Object.assign(this.model, row);
      },
      findAllList(){
        let that = this;
        that.confirmLoading = true;
        getAction(that.url.findAllList,{}).then((res)=>{
          if(res.success){
            this.invoiceCategoryOptions=[]
            for(let i = 0;i<res.result.length;i++){
              var name = '*' + res.result[i].invoiceCategoryName + '*' + res.result[i].taxCategoryName;
              var value = res.result[i].id.toString()
              this.invoiceCategoryOptions.push({label: name, value:value})
            }
          }else{
            console.log('查询失败');
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      },
      
    }
  }
</script>