<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="项目名称">
              <a-input placeholder="请输入项目名称" v-model="queryParam.projectName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="广告主名称">
              <!-- <a-input placeholder="请输入广告主名称" v-model="queryParam.advertiserName"></a-input> -->

              <a-select
                optionFilterProp="children"
                showSearch
                :filterOption="filterOption"
                v-model="queryParam.advertiserId"
                placeholder="请选择广告主名称"
              >
                <a-select-option :value="item.id" v-for="(item, index) of list" :key="index">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template>
            <a-col :md="6" :sm="8">
              <a-form-item label="运营负责人">
                <!-- <a-input placeholder="请输入负责人" v-model="queryParam.responsible"></a-input> -->
                <a-select
                  placeholder="请选择运营负责人"
                  showSearch
                  optionFilterProp="children"
                  style="width: 100%"
                  :filterOption="filterOption"
                  v-model="queryParam.responsibleId"
                  @search="search"
                  @focus="getAllUserList"
                >
                  <a-select-option
                    v-for="appModel in options"
                    :key="appModel.userId + new Date().getTime()"
                    :value="appModel.userId"
                    >{{ appModel.realName }}&#12288;&#12288;{{ appModel.roleName }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <template>
            <a-col :md="6" :sm="8">
              <a-form-item label="设计负责人">
                <!-- <a-input placeholder="请输入负责人" v-model="queryParam.responsible"></a-input> -->
                <a-select
                  placeholder="请选择设计负责人"
                  showSearch
                  optionFilterProp="children"
                  style="width: 100%"
                  :filterOption="filterOption"
                  v-model="queryParam.designResponsibleId"
                  @search="search"
                  @focus="getAllUserList"
                >
                  <a-select-option
                    v-for="appModel in options"
                    :key="appModel.userId + new Date().getTime()"
                    :value="appModel.userId"
                    >{{ appModel.realName }}&#12288;&#12288;{{ appModel.roleName }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="6">
              <a-form-item label="行业">
                <j-tree-select
                  v-model="queryParam.industryId"
                  ref="treeSelect"
                  placeholder="请选择行业"
                  dict="sys_category,name,id"
                  pidField="pid"
                  pidValue="f5cb2d2d28417b3b65a6dd744bcb9a76"
                ></j-tree-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>-->

    <!-- table区域-begin -->
    <!-- 供应商 -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <span slot="maxBid" slot-scope="text,record">{{ record.maxBid == 0? '-': (record.maxBid / 1000).toFixed(2) }}</span>
        <!-- <span slot="maxDeepCpaBid" slot-scope="text">{{ text / 1000 }}</span> -->
        <span slot="ocpxActionType" slot-scope="text,record">
          <div v-if="!!record.ocpxActionTypeArray&&record.ocpxActionTypeArray.length>0">
            <span v-for="(item, index)  in record.ocpxActionTypeArray" :key="index"
              >{{item.ocpxName}}({{(item.maxValue / 1000).toFixed(2)}})<br></span
            >
          </div>
          <div v-else>-</div>

          <!-- {{ text | ocpxActionType }} -->
        </span>
        <span slot="deepConversionType" slot-scope="text,record">
          <div v-if="!!record.deepConversionTypeArray&&record.deepConversionTypeArray.length != 0">
            <span v-for="(item, index)  in record.deepConversionTypeArray" :key="index"
              >{{item.deepName}}({{(item.deepValue / 1000).toFixed(2)}})<br></span
            >
          </div>
          <div v-else>-</div>

          <!-- {{ text | ocpxActionType }} -->
        </span>
        <span slot="bidType" slot-scope="text">
          <div v-if="text && JSON.parse(text).length > 0">
            <span v-for="(item, index) of JSON.parse(text)" :key="index"
              >{{ item | bidType }}{{ JSON.parse(text).length ==0||index == JSON.parse(text).length - 1 ? '' : '、' }}</span
            >
          </div>
          <div v-else>-</div>
        </span>
        <span slot="mediaId" slot-scope="text">
          {{ text == '1' || text == '3' ? '头条' :text == '2' || text=='4'?'快手' :text=='5'?'千川':'磁力金牛'}}
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />

          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <!-- /ctop/project/edit -->
        </span>
        <span slot="materialShare" slot-scope="text, record">
            <a-switch :checked='text==0' @change="onChange(text,record)" :loading="spinning"/>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <project-modal ref="modalForm" @ok="modalFormOk"></project-modal>
  </a-card>
</template>

<script>
import ProjectModal from './modules/projectListModal'
import JTreeSelect from '@/components/jeecg/JTreeSelect'
import { httpAction, getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { mapActions, mapGetters } from 'vuex'
export default {
  name: 'ProjectList',
  mixins: [JeecgListMixin],
  components: {
    ProjectModal,
    JTreeSelect,
  },
  data() {
    return {
      description: '项目管理页面',
      // 表头
      columns: [
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName',
        },
        {
          title: '广告主名称',
          align: 'center',
          dataIndex: 'advertiserId_dictText',
        },
        {
          title: '代理商名称',
          align: 'center',
          dataIndex: 'agentCode_dictText',
          customRender:function(t){
            if(t){
              return t
            }else{
              return '-'
            }
          }
        },
        {
          title: '优化目标',
          align: 'center',
          dataIndex: 'bidType',
          scopedSlots: {
            customRender: 'bidType',
          },
        },
        {
          title: '最高出价(元)',
          align: 'center',
          dataIndex: 'maxBid',
          scopedSlots: {
            customRender: 'maxBid',
          },
        },
        // {
        //   title: '最高转化出价',
        //   align: 'center',
        //   dataIndex: 'maxDeepCpaBid',
        //   scopedSlots: {
        //     customRender: 'maxDeepCpaBid',
        //   },
        // },
       
        {
          title: '转化目标(出价/元)',
          align: 'center',
          dataIndex: 'ocpxActionType',
          scopedSlots: {
            customRender: 'ocpxActionType',
          },
        },
        {
          title: '深度转化目标(出价)',
          align: 'center',
          dataIndex: 'deepConversionType',
          scopedSlots: {
            customRender: 'deepConversionType',
          },
        },
        {
          title: '供应商',
          align: 'center',
          dataIndex: 'supplierCode_dictText',
        },
        {
          title: '销售',
          align: 'center',
          dataIndex: 'saleId_dictText',
        },
        {
          title: '运营负责人',
          align: 'center',
          dataIndex: 'responsibleId_dictText',
        },
        {
          title: '设计负责人',
          align: 'center',
          dataIndex: 'designResponsibleId_dictText',
        },
        {
          title: '媒体',
          align: 'center',
          dataIndex: 'mediaId',
          scopedSlots: {
            customRender: 'mediaId',
          },
        },
        {
          title: '是否共享素材',
          align: 'center',
          dataIndex: 'materialShare',
          scopedSlots: {
            customRender: 'materialShare',
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: {
            customRender: 'action',
          },
        },
      ],
      url: {
        list: '/ctop/project/list',
        delete: '/ctop/project/delete',
        deleteBatch: '/ctop/project/deleteBatch',
        exportXlsUrl: 'ctop/project/exportXls',
        importExcelUrl: 'ctop/project/importExcel',
      },
      list: [],
      options: [],
      spinning: false
    }
  },
  filters: {
    bidType(sta) {
      var data = {
        null: '-',
        1: 'CPM',
        2: '点击数',
        6: 'OCPC',
        10: 'OCPM',
        12: "最大转化数"
      }
      return data[sta]
    },
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  created() {
    this.queryParam.userId = this.userInfo().id;
  },
  methods: {
    onChange(text,record) {
      this.spinning=true;
      console.log(text,record)
      let materialShare=text==1?0:1;
      httpAction(`/ctop/project/edit`,{
          id:record.id,
          materialShare
      },'put').then((res)=>{
        
        if(res.success) {
          this.spinning=false;
          this.$message.success('修改成功')
          this.modalFormOk()
        }else {
          this.spinning=false;
          this.$message.success(res.message)
          this.modalFormOk()
        }
      })
    },
   
    ...mapGetters(['nickname', 'avatar', 'userInfo']),
    getList() {
      var params = {}
      // params.userId = this.userInfo().id
      params.pageSize = 1000
      params.pageNo = 1
      params.userId = this.userInfo().id
      getAction('/ctop/advertiser/list', params)
        .then((res) => {
          if (res.success) {
            this.list = res.result.records
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {})
    },
    // searchQuery(){
    //   this.queryParam.userId = this.userInfo().id;
    //   this.getList();
    // },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    getAllUserList(value) {
      this.options = []
      getAction('/sys/user/getAllUserList', { pageSize: 50, pageNo: 1, userName: value }).then((res) => {
        if (res.success) {
          this.options = res.result.list
        }
      })
    },
    search(value) {
      this.options = []
      if (value != '') {
        // this.options = this.list.filter(item => {
        //   var data = item.realName + ' ' + item.roleName
        //   return data.toLowerCase().indexOf(value.toLowerCase()) > -1
        // })
        // this.options = [...this.options]
        this.getAllUserList(value)
      } else {
        this.getAllUserList()
      }
    },
  },
  mounted() {
    this.getAllUserList()
    this.getList()
    // getAction('/sys/user/getAllUserList', { pageSize: 50, pageNo: 1, userName: '' }).then((res) => {
    //   if (res.success) {
    //     this.options = res.result.list
    //   }
    // })
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>