<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目名称">
              <a-input placeholder="请输入项目名称" v-model="queryParam.xmName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <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>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('成本模型')">导出</a-button> -->
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="maxWxf" slot-scope="text, record">
          <a-button type="link" @click="detailClick(text,record,'wxf','max','外协费最大值')">{{ text }}</a-button>
          <!-- {{Number(text).toFixed(2)}}  style="color: rgba(0,0,0,.65);"-->
        </span>
        <span slot="minWxf" slot-scope="text, record">
          <a-button type="link" @click="detailClick(text,record,'wxf','min','外协费最小值')">{{ text }}</a-button>
        </span>
        <span slot="aveWxf" slot-scope="text, record">
          <a-button type="link" @click="detailClick(text,record,'wxf','ave','外协费平均值')">{{ text }}</a-button>
        </span>
        <span slot="maxClf" slot-scope="text, record">
          <a-button type="link" @click="detailClick(text,record,'clf','max','材料费最大值')">{{ text }}</a-button>
        </span>
        <span slot="minClf" slot-scope="text, record">
          <a-button type="link" @click="detailClick(text,record,'clf','min','材料费最小值')">{{ text }}</a-button>
        </span>
        <span slot="aveClf" slot-scope="text, record">
          <a-button type="link" @click="detailClick(text,record,'clf','ave','材料费平均值')">{{ text }}</a-button>
        </span>
        <span slot="recentClf" slot-scope="text, record">
          <a-button type="link" @click="detailClick(text,record,'clf','recent','材料费近期值')">{{ text }}</a-button>
        </span>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleJG(record)" style="color:#FFA11E;" v-if="record.modelStatus == 1">警告</a>
          <a-divider type="vertical" v-if="record.modelStatus == 1"/>
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <!-- <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item> -->
              <a-menu-item>
                <a @click="handleInform(record)">知会</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleExportItem(record)">导出</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <cost-model-list-modal ref="modalForm" @ok="modalFormOk"></cost-model-list-modal>

    <j-modal
      title="知会"
      :width="800"
      :visible="visibleInform"
      switchFullscreen
      @ok="handleOk"
      :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
      @cancel="handleCancel"
      cancelText="关闭">
      <!-- <j-select-multi-user :returnKeys="returnKeys" placeholder="请选择指定用户" v-model="userIds" :trigger-change="true"></j-select-multi-user> -->
      <j-select-user-by-dep placeholder="请选择消息接收方" v-model="receiver">1111</j-select-user-by-dep>
    </j-modal>

    <cost-model-detail-modal ref="costModelListModal"></cost-model-detail-modal>

    <!-- 警告 -->
    <cost-model-jg-modal ref="costModelJgModal"></cost-model-jg-modal>

  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CostModelListModal from './modules/CostModelListModal'
  import { costModelInform, exportXlsCbmxItem } from '@/api/kzksApi.js'
import CostModelDetailModal from './modulesDetail/CostModelDetailModal.vue'
import CostModelJgModal from './modulesDetail/CostModelJgModal.vue'

  export default {
    name: 'CostModelListList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      CostModelListModal,
      CostModelDetailModal,
        CostModelJgModal
    },
    data () {
      return {
        description: '成本模型管理页面',
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'项目名称',
            align:"center",
            dataIndex: 'xmName'
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy'
          },
          // {
          //   title:'所属部门',
          //   align:"center",
          //   dataIndex: 'sysOrgCode'
          // },
          {
            title:'知会给',
            align:"center",
            dataIndex: 'informTo_dictText'
          },
          {
            title:'外协费',
            align:"center",
            children: [
              {
                title:'最大值',
                align:"center",
                dataIndex: 'maxWxf',
                scopedSlots: {
                  customRender: 'maxWxf',
                },
                // customCell: this.getClickColumn,
              },
              {
                title:'最小值',
                align:"center",
                dataIndex: 'minWxf',
                scopedSlots: {
                  customRender: 'minWxf',
                },
                // customCell: this.getClickColumn,
              },
              {
                title:'平均值',
                align:"center",
                dataIndex: 'aveWxf',
                scopedSlots: {
                  customRender: 'aveWxf',
                },
                // customCell: this.getClickColumn,
              },
            ],
          },
          {
            title:'材料费',
            align:"center",
            children: [
              {
                title:'最大值',
                align:"center",
                dataIndex: 'maxClf',
                scopedSlots: {
                  customRender: 'maxClf',
                },
                // customCell: this.getClickColumn,
              },
              {
                title:'最小值',
                align:"center",
                dataIndex: 'minClf',
                scopedSlots: {
                  customRender: 'minClf',
                },
                // customCell: this.getClickColumn,
              },
              {
                title:'平均值',
                align:"center",
                dataIndex: 'aveClf',
                scopedSlots: {
                  customRender: 'aveClf',
                },
                // customCell: this.getClickColumn,
              },
              {
                title:'近期值',
                align:"center",
                dataIndex: 'recentClf',
                scopedSlots: {
                  customRender: 'recentClf',
                },
                // customCell: this.getClickColumn,
              },
            ],
          },
          // {
          //   title:'外协费',
          //   align:"center",
          //   dataIndex: 'wxf'
          // },
          // {
          //   title:'材料费',
          //   align:"center",
          //   dataIndex: 'clf'
          // },
          // {
          //   title:'材料费类型',
          //   align:"center",
          //   dataIndex: 'clfType'
          // },
          // {
          //   title:'事务费',
          //   align:"center",
          //   dataIndex: 'swf'
          // },
          // {
          //   title:'专用费',
          //   align:"center",
          //   dataIndex: 'zyf'
          // },
          // {
          //   title:'固资费', // 固定资产折旧费
          //   align:"center",
          //   dataIndex: 'gdzczj'
          // },
          // {
          //   title:'燃动费',
          //   align:"center",
          //   dataIndex: 'rldlf'
          // },
          // {
          //   title:'工资费', // 工资及劳务费
          //   align:"center",
          //   dataIndex: 'gzjlwf'
          // },
          // {
          //   title:'管理费',
          //   align:"center",
          //   dataIndex: 'glf'
          // },
          // {
          //   title:'总价',
          //   align:"center",
          //   dataIndex: 'totalPrice'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/costModelList/costModelList/list",
          delete: "/costModelList/costModelList/delete",
          deleteBatch: "/costModelList/costModelList/deleteBatch",
          exportXlsUrl: "/costModelList/costModelList/exportXlsWXFCLFDetail",
          // exportXlsUrl: "/costModelList/costModelList/exportXls",
          importExcelUrl: "costModelList/costModelList/importExcel",

        },
        dictOptions:{},
        superFieldList:[],

        // returnKeys:['id', 'id'], //用户选择返回字段
        // userIds:"",
        visibleInform: false,
        disableSubmit: false,
        receiver: "",

        inform: {},

      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      detailClick(text, record, dialogType, columnType, columnTitle){
        console.log(text, record, columnType, columnTitle)
        // 没有值不弹窗
        if(!text){
          return
        }
        this.$refs.costModelListModal.dialogType = dialogType
        this.$refs.costModelListModal.id = record.id
        this.$refs.costModelListModal.columnType = columnType
        this.$refs.costModelListModal.title =  columnTitle + '明细'
        this.$refs.costModelListModal.show()
      },
      getClickColumn(record, index){
        return {
          style: {
            'color': '#1890ff',
            'cursor': 'pointer',
          },
          on: {
            click: (event)=> {
              // 没有值不弹窗
              if(!event.target.textContent){
                return
              }
              // console.log(record, index, event, event.target.cellIndex)
              // console.log(this.columns[event.target.cellIndex - 1])
              // var clickColumn = this.columns[event.target.cellIndex - 1].dataIndex
              console.log(event.target.cellIndex - 1)
              // var title = this.columns[event.target.cellIndex - 1].title
              // this.$refs.proCostDetail.taskno = record.taskno
              // if(record.status === '1'){
              //   // 批产参数
              //   this.$refs.proCostDetail.pichanInfo = {
              //     pccode: record.pccode,
              //     proportion: record.proportion,
              //     columnValue: clickColumn,
              //   }
              //   this.$refs.proCostDetail.columnValue = 'pichanTask'
              //   this.$refs.proCostDetail.title = title + '批产明细'
              // } else {
              //   this.$refs.proCostDetail.columnValue = clickColumn
              //   this.$refs.proCostDetail.title =  title + '明细'
              // }
              // console.log(this.$refs.proCostDetail.columnValue)
              // this.$refs.costModelListModal.show()
            },
          }
        }
      },
      handleInform(record){
        console.log(record)
        this.inform.id = record.id
        this.visibleInform = true
      },
      handleOk(){
        // this.visibleInform = false
        console.log(this.receiver)
        this.inform.informTo = this.receiver.toString()
        costModelInform(this.inform).then(res => {
          console.log(res)
          this.visibleInform = false
          this.loadData()
        })
      },
      handleCancel(){
        this.visibleInform = false

      },
      handleExportItem(record){
        this.queryParam.xiangmuid = record.id
        this.handleExportXls(record.xmName+'成本模型')
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'xmName',text:'项目名称',dictCode:''})
        fieldList.push({type:'string',value:'wxf',text:'外协费',dictCode:''})
        fieldList.push({type:'string',value:'clf',text:'材料费',dictCode:''})
        fieldList.push({type:'string',value:'clfType',text:'材料费类型',dictCode:''})
        fieldList.push({type:'string',value:'swf',text:'事务费',dictCode:''})
        fieldList.push({type:'string',value:'zyf',text:'专用费',dictCode:''})
        fieldList.push({type:'string',value:'gdzczj',text:'固定资产折旧费',dictCode:''})
        fieldList.push({type:'string',value:'rldlf',text:'燃料动力费',dictCode:''})
        fieldList.push({type:'string',value:'gzjlwf',text:'工资及劳务费',dictCode:''})
        fieldList.push({type:'string',value:'glf',text:'管理费',dictCode:''})
        fieldList.push({type:'string',value:'totalPrice',text:'总价',dictCode:''})
        this.superFieldList = fieldList
      },
      // 警告
      handleJG(record){
        console.log(record)
        this.$refs.costModelJgModal.id = record.id
        this.$refs.costModelJgModal.show()
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>