<template>

    <a-card :body-style="{padding: '24px 32px'}" :bordered="false">
    <a-spin :spinning="confirmLoading">


      <detail-list title="支付模式">


        <a-checkbox-group :options="[{label:'通过平台',value:1}]"  v-model="isThroughPlatform1" @change="changeIsThroughPlatform1"  style="padding:10px 0px"/>
        <br>
        <a-checkbox-group :options="plainOptions1"  v-model="subName1" @change="changeSubName1"  style="padding:0px 60px" :disabled="confirmDisabled1"/>

        <br>
        <a-checkbox-group :options="[{label:'不通过平台',value:0}]"  v-model="isThroughPlatform0" @change="changeIsThroughPlatform0"  style="padding:10px 0px"/>

        <br>
        <a-checkbox-group :options="plainOptions"  v-model="subName" @change="changeSubName"  style="padding:0px 60px" :disabled="confirmDisabled"/>
      </detail-list>



      <detail-list title="付款类型" style="padding-top: 30px">
          <a-checkbox-group :options="[{label:'应付',value:0},{label:'实付',value:1}]"  v-model="paymentType" @change="changePaymentType"  style="padding:10px 10px" />
      </detail-list>




      <div style="margin: 20px">
        <a-button class="ant-btn ant-btn-primary" style="margin-left: 8px"  @click="handleOk" :disabled="submit">保存</a-button>
      </div>
    </a-spin>
    </a-card>

</template>

<script>

  import { httpAction ,getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import DetailList from '@/components/tools/DetailList'

  const DetailListItem = DetailList.Item
  const findList = (params) => getAction('/wisdom/hlwApplicationSetting/findApplicationDetail', params)
  export default {
    name: "HlwApplicationSettingDetail",
    components: {
      JDate,
      DetailList,
      DetailListItem
    },
    props: ['value'],
    data () {
      return {
        submit:true,
        paymentType:[],
        isThroughPlatform1:[],
        isThroughPlatform0:[],
        isThroughPlatformList:[],
        subName:[],
        subName1:[],
        plainOptions:[],
        plainOptions1:[],
        confirmDisabled:false,
        confirmDisabled1:false,
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          applicationName: {
            rules: [
              { required: true, message: '请输入应用名称!'},
            ]
          },
          appid: {
            rules: [
              { required: true, message: '请输入应用APPID!'},
            ]
          },
          isThroughPlatform: {
            rules: [
              { required: true, message: '请输入是否通过平台（0：否 1：是）!'},
            ]
          },
          paymentType: {
            rules: [
              { required: true, message: '请输入付款类型（0：应付 1：实付）!'},
            ]
          },
          createTime: {
            rules: [
              { required: true, message: '请输入创建时间!'},
            ]
          },
          createBy: {
            rules: [
              { required: true, message: '请输入创建者!'},
            ]
          },
        },
        url: {
          add: "/wisdom/hlwApplicationSetting/add",
          edit: "/wisdom/hlwApplicationSetting/edit",
        }
      }
    },
    created () {
    },

    watch: {
      value: {
        immediate: true,
        handler(applicationId) {
          this.applicationId= applicationId
          this.findModelDetail(applicationId)
        }
      },
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'applicationName','appid','isThroughPlatform','paymentType','createTime','createBy','updateTime','updateBy'))
        })
      },
      findModelDetail(applicationId){
        //新加载先清空默认值
        this.isThroughPlatform1=[]
        this.isThroughPlatform0=[]
        this.paymentType=[]
        this.subName=[]
        this.subName1=[]
        this.isThroughPlatformList=[]
        this.model.subIdList=[]
        this.model.cpSubIdList=[]
        //清空服务商value
        this.plainOptions=[]
        this.plainOptions1=[]
        //取消禁用
        this.confirmDisabled=false
        this.confirmDisabled1=false
        findList({applicationId:applicationId}).then((res) => {
          if (res.success) {
            if(applicationId){
              this.submit=false
            }
           if(res.result){
             var sublist=res.result.subNamelist
             this.putSubList(sublist)
             this.confirmDisabled=true
             this.confirmDisabled1=true
              if(res.result.isThroughPlatform){
                this.edit(res.result)
                var isThroughPlatformArr=res.result.isThroughPlatform.split(",")
                if(isThroughPlatformArr.length>0){
                  this.submit=true
                  for (let i = 0; i <isThroughPlatformArr.length ; i++) {
                    if(isThroughPlatformArr[i]==="1"){
                      this.isThroughPlatform1.push(1)
                      this.isThroughPlatformList.push('1')
                      this.confirmDisabled1=false
                    }else if(isThroughPlatformArr[i]==='0' ){
                      this.isThroughPlatform0.push(0)
                      this.isThroughPlatformList.push('0')
                      this.confirmDisabled=false
                    }
                  }
                }
                var paymentTypeArr=res.result.paymentType.split(",")
                for (let i = 0; i <paymentTypeArr.length; i++) {
                  if(paymentTypeArr[i]==="1"){
                    this.paymentType.push(1)
                  }else if(paymentTypeArr[i]==='0' ){
                    this.paymentType.push(0)
                  }
                }
              }
           }
          }
        })
      },

      putSubList(sublist){
        console.log(11111111)
        if(sublist){
          for (let i = 0; i <sublist.length ; i++) {
            //isCpOn=1通过平台
            if(sublist[i].isCpOn==1){
              this.plainOptions1.push({label: sublist[i].name, value: sublist[i].id})
              if(sublist[i].checked){
                this.subName1.push((sublist[i].id))
              }
            }else{
              this.plainOptions.push({label: sublist[i].name, value: sublist[i].id})
              if(sublist[i].checked){
                this.subName.push((sublist[i].id))
              }
            }
          }
        }
      },


      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        if(this.isThroughPlatformList.length>0){

        }else{
          this.$message.warning("请选择支付模式!")
          return
        }
        if(!this.model.paymentType){
          this.$message.warning("请选择付款类型!")
          return
        }
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl+=this.url.edit;
            method = 'put';
            let formData = Object.assign(this.model, values);
            for (let i = 0; i <this.isThroughPlatformList.length ; i++) {
              if(i==0){
                formData.isThroughPlatform=this.isThroughPlatformList[i]
              }else{
                formData.isThroughPlatform=this.isThroughPlatformList[i]+","+formData.isThroughPlatform
              }
            }
            formData.id=this.applicationId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.findModelDetail(that.applicationId)
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'applicationName','appid','isThroughPlatform','paymentType','createTime','createBy','updateTime','updateBy'))
      },



      changeIsThroughPlatform1(checkedValues){
        this.confirmDisabled1=false
        if(checkedValues.length>0){
          this.confirmDisabled1=false
          for (let i = 0; i <this.isThroughPlatformList.length ; i++) {
            if(this.isThroughPlatformList[i]=='1'){
              return
            }
          }
          this.isThroughPlatformList.push('1')
        }else{
          this.confirmDisabled1=true
          for (let i = 0; i <this.isThroughPlatformList.length ; i++) {
            if(this.isThroughPlatformList.length==2){
              this.isThroughPlatformList=["0"]
              return
            }else if(this.isThroughPlatformList==1 && this.isThroughPlatformList[0]!=1){
              this.isThroughPlatformList=["0"]
              return
            }
            this.isThroughPlatformList=[]
          }
        }
      },

      changeIsThroughPlatform0(checkedValues){
        this.confirmDisabled=false
        if(checkedValues.length>0){
          this.confirmDisabled=false
          for (let i = 0; i <this.isThroughPlatformList.length ; i++) {
            if(this.isThroughPlatformList[i]=='0'){
              return
            }
          }
          this.isThroughPlatformList.push('0')
        }else{
          this.confirmDisabled=true
          for (let i = 0; i <this.isThroughPlatformList.length ; i++) {
            if(this.isThroughPlatformList[i]=='1'){
              this.isThroughPlatformList=['1']
              return
            }
          }
          this.isThroughPlatformList=[]
        }
      },


      changeSubName(checkedValues){
        if(checkedValues.length>0){
          this.model.subIdList=[]
          for (let i = 0; i <checkedValues.length ; i++) {
            this.model.subIdList.push(checkedValues[i])
          }
        }
      },

      changeSubName1(checkedValues){
        if(checkedValues.length>0){
          this.model.cpSubIdList=[]
          for (let i = 0; i <checkedValues.length ; i++) {
            this.model.cpSubIdList.push(checkedValues[i])
          }
        }
      },

      changePaymentType(checkedValues){
        this.model.paymentType=null
        if(checkedValues.length>0){
          for (let i = 0; i <checkedValues.length ; i++) {
            if(i==0){
              this.model.paymentType=checkedValues[i]+""
            }else{
              this.model.paymentType=this.model.paymentType+","+checkedValues[i]
            }
          }
        }
      },





    }
  }
</script>
<style lang="less" scoped>
  /*.drawer-bootom-button {*/
    /*position: absolute;*/
    /*bottom: -8px;*/
    /*width: 100%;*/
    /*border-top: 1px solid #e8e8e8;*/
    /*padding: 10px 16px;*/
    /*text-align: right;*/
    /*left: 0;*/
    /*background: #fff;*/
    /*border-radius: 0 0 2px 2px;*/
  /*}*/

</style>
