<template>
    <a-modal
            :title="title"
            :width="800"
            :visible="visible"
            :confirmLoading="confirmLoading"
            @ok="handleSubmit"
            @cancel="handleCancel"
            cancelText="关闭">

        <a-spin :spinning="confirmLoading">
          <a-form @submit="handleSubmit" :form="form">
            <a-form-item
              label="选择广告组模板"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span: 15}, sm: {span: 17} }">
              <a-select
                v-model="campaignId"
                showSearch
                placeholder="选择广告主模板"
                optionFilterProp="children"
                style="width: 200px"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
                :filterOption="filterOption"
              >
                <a-select-option v-for="campaign in campaignList" :key="campaign.id" :value="campaign.id">{{campaign.campaignName}}</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item
              label="投放范围"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span: 15}, sm: {span: 17} }">
              <a-radio-group v-model="deliveryRange">
                <a-radio-button value="DEFAULT">默认</a-radio-button>
                <a-radio-button value="UNION">穿山甲</a-radio-button>
              </a-radio-group>
            </a-form-item>
            <a-form-item
              label="选择用户定向模板"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span: 15}, sm: {span: 17} }">
              <a-select
                v-model="userorentationId"
                showSearch
                placeholder="选择用户定向模板"
                optionFilterProp="children"
                style="width: 200px"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
                :filterOption="filterOption"
              >
                <a-select-option value="1">默认</a-select-option>
                <!--<a-select-option v-for="userorentation in userorentationList" :key="userorentation.id" :value="userorentation.id">{{userorentation.name}}</a-select-option>-->
              </a-select>
            </a-form-item>

            <a-form-item
              label="选择预算与出价模板"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span: 15}, sm: {span: 17} }">
              <a-select
                v-model="budgetId"
                showSearch
                placeholder="选择预算与出价模板"
                optionFilterProp="children"
                style="width: 200px"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
                :filterOption="filterOption"
              >
                <a-select-option v-for="budget in budgetList" :key="budget.id" :value="budget.id">{{budget.name}}</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item
              label="选择投放目标模板"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span: 15}, sm: {span: 17} }">
              <a-select
                v-model="deliverytargetId"
                showSearch
                placeholder="选择投放目标模板"
                optionFilterProp="children"
                style="width: 200px"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
                :filterOption="filterOption"
              >
                <a-select-option v-for="deliverytarget in deliverytargetList" :key="deliverytarget.id" :value="deliverytarget.id">{{deliverytarget.name}}</a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item
              label="模板名称"
              :labelCol="{lg: {span: 7}, sm: {span: 7}}"
              :wrapperCol="{lg: {span: 10}, sm: {span: 17} }">
              <a-input v-model="templateName"></a-input>
            </a-form-item>
          </a-form>
        </a-spin>
    </a-modal>
</template>

<script>
    import {deleteAction, postAction, getAction} from '@/api/manage';
    import moment from 'moment';
    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'

    export default {
        name: "BytedanceAdvertisePlanTemplateModal",
        data() {
            return {
              headers: {
                authorization: 'authorization-text',
              },
              templateName:'',
              loading: false,
              campaignList: {},
              deliverytargetList:{},
              userList:{},
              userorentationList:{},
              budgetList:{},
              deliveryRange:'DEFAULT',
              campaignId:null,
              deliverytargetId:null,
              budgetId:null,
              userorentationId:null,
                title: "操作",
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                form: this.$form.createForm(this),
                validatorRules: {},
                url: {
                    add: "/ctop/bytedanceAdvertisePlanTemplate/add",
                    edit: "/ctop/bytedanceAdvertisePlanTemplate/edit",
                    dictListUrl: 'toutiao/dictitem/list',
                    campaignListUrl:'/template/campaign/list',
                    budgetTemplateListUrl:'/template/budget/list',
                    deliverytargetTemplateListUrl:'/template/deliverytarget/list',
                    userorentationTemplateListUrl:'/template/userorentation/list',
                    insertUrl:'/template/advertiserplan/insert',
                    userAllocationList:'/template/allocation/list'
                },
            }
        },
        created() {
        },
        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,'campaignTemplateId', 'deliveryRange', 'userOrentationTemplateId', 'budgetTemplateId', 'deliveryTargetTemplateId', 'videoId', 'imageId', 'creativeText', 'iteratorNum'))
                    //时间格式化
                });

            },
            close() {
              this.$emit('close');
              this.visible = false;
            },
          handleCancel () {
            this.close()
          },
          moment,
          accountHandleChange (value) {
            console.log(`selected ${value}`);
            this.refreshCampaignList();
          },
          handleChange (value) {
            console.log(`selected ${value}`);
          },
          handleBlur() {
            console.log('blur');
          },
          handleFocus() {
            console.log('focus');
          },
          filterOption(input, option) {
            return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
          },
          handleSubmit(e) {
            e.preventDefault()
            this.form.validateFields((err, values) => {
              if (!err) {
                console.log('Received values of form: ', values);

                let params = {};
                params.campaignTemplateId = this.campaignId;
                params.deliveryRange = this.deliveryRange;
                params.userOrentationTemplateId = this.userorentationId;
                params.budgetTemplateId = this.budgetId;
                params.deliveryTargetTemplateId = this.deliverytargetId;
                params.name = this.templateName;

                console.log(params)
                postAction(this.url.insertUrl+'?accountId='+this.accountId,params).then((res)=>{
                  console.log(res);
                  alert(res.message);
                });

              }
            })
          },
          onChange(value) {
            console.log(value)
          },
          refreshCampaignList(){
            let campaignParams = {};
            campaignParams.toutiaoId = this.accountId;
            postAction(this.url.campaignListUrl, campaignParams).then((res) => {
              if (res.success) {
                this.campaignList = res.result.records
              }
            });
          }
        },
      created: function () {
        this.refreshCampaignList();
        let params = {};
        params.pageSize = '1000';
        postAction(this.url.budgetTemplateListUrl, params).then((res) => {
          if (res.success) {
            this.budgetList = res.result.records
          }
        });

        params = {};
        params.pageSize = '1000';
        params.mediaId='1';
        postAction(this.url.userAllocationList, params).then((res) => {
          if (res.success) {
            this.userList = res.result.records
          }
        });



        params = {};
        params.pageSize = '1000';
        postAction(this.url.deliverytargetTemplateListUrl, params).then((res) => {
          if (res.success) {
            this.deliverytargetList = res.result.records
          }
        });

        params = {};
        params.pageSize = '1000';
        postAction(this.url.userorentationTemplateListUrl, params).then((res) => {
          if (res.success) {
            this.userorentationList = res.result.records
          }
        });
      }
    }
</script>

<style lang="less" scoped>

</style>