<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 :md="6" :sm="8">
            <a-form-item label="设备名称">
              <a-tree-select
                             placeholder="请选择设备名称"
                             v-model="this.deviceName"
                             :tree-data="this.treeData"
                             tree-default-expand-all
                             :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
                             @select="handleSelectChange"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="风险项名称">
              <j-dict-select-tag placeholder="请选择风险项" v-model="queryParam.riskId" dict-code="bu_risk_item,risk_name,id" />
            </a-form-item>
          </a-col>
          <a-col>
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchData">查询</a-button>
              <a-button style="margin-left: 8px" @click="searchReset1">重置</a-button>
              </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleSave" type="primary" icon="plus" v-has="'risk:add'">新增</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" v-has="'risk: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" v-has="'risk:muldel'"> 批量操作 <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">
        <template slot="switchRender" slot-scope="text, record">
          <a-switch :checked="text==='0'?true:false" @change="handleSwitchChange(record)" checked-children="开" un-checked-children="关"/>
        </template>

        <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="handleEdit(record)" v-has="'risk:edit'">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)" v-has="'risk:del'">
            <a>删除</a>
          </a-popconfirm>

<!--          <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-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">-->
<!--                  <a>删除</a>-->
<!--                </a-popconfirm>-->
<!--              </a-menu-item>-->
<!--            </a-menu>-->
<!--          </a-dropdown>-->
        </span>
      </a-table>
    </div>
    <bu-risk-item-value-modal ref="modalForm" @ok="modalFormOk"></bu-risk-item-value-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import BuRiskItemValueModal from './modules/BuRiskItemValueModal'
  import {getAction, httpAction, putAction} from "@api/manage";
  import JSelectRiskItem from "@comp/jeecgbiz/JSelectRiskItem";

  export default {
    name: 'BuRiskItemValueList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      JSelectRiskItem,
      BuRiskItemValueModal
    },
    // props: {
    //   deviceId: {
    //     type: String,
    //     required: true
    //   }
    // },
    // watch: {
    //   deviceId(newVal, oldVal) {
    //     console.log("newVal",newVal)
    //     this.queryParam.deviceId = newVal
    //     this.loadData();
    //   }
    // },
    data () {
      return {
        description: 'bu_risk_item_value管理页面',
        treeData:[],
        deviceId:'',
        deviceName:'',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'设备名称',
            align:"center",
            dataIndex: 'deviceName'
          },
          {
            title:'风险项名称',
            align:"center",
            dataIndex: 'riskName'
          },
          {
            title:'单项判断中间值',
            align:"center",
            dataIndex: 'multiple'
          },
          {
            title:'下限值',
            align:"center",
            dataIndex: 'valueOne'
          },
          {
            title:'上限值',
            align:"center",
            dataIndex: 'valueTwo'
          },
          {
            title:'备用一',
            align:"center",
            dataIndex: 'valueThree'
          },
          {
            title:'备用二',
            align:"center",
            dataIndex: 'valueFour'
          },
          {
            title:'是否启用',
            align:"center",
            dataIndex: 'enabled',
            // key: 'switch',
            scopedSlots: { customRender: 'switchRender' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/device/buRiskItemValue/list",
          delete: "/device/buRiskItemValue/delete",
          deleteBatch: "/device/buRiskItemValue/deleteBatch",
          exportXlsUrl: "/device/buRiskItemValue/exportXls",
          importExcelUrl: "device/buRiskItemValue/importExcel",
          edit: "/device/buRiskItemValue/edit",
          treeTest: "/device/buDevice/treeTest",
        },
        dictOptions:{},
        superFieldList:[],
        enable:'',
      }
    },
    created() {
    this.getSuperFieldList();
    this.treeTest()
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      treeTest(){
        getAction(this.url.treeTest).then((res) => {
          if (res) {
            res.forEach(type=>{
              this.childrenList = []
              if(type.children.length>0){
                type.children.forEach(device=>{
                  this.childrenList.push({title:device.deviceName+device.deviceCode,key:device.id,value: device.id,typeId:device.deviceTypeId})
                })
              }else{
                this.childrenList.push({})
              }
              this.treeData.push({title:type.typeName,key:type.id,value: type.id,children:this.childrenList,selectable:false,})
            })
          }else{
          }
        })
      },
      handleSelectChange(selectedKeys, event) {
        this.deviceName = event.$options.propsData.dataRef.title;
        this.deviceId = event.$options.propsData.dataRef.key;
      },
      handleSave: function () {
        this.$refs.modalForm.add(this.queryParam.deviceId);
        this.$refs.modalForm.title = "新增";
        this.$refs.modalForm.disableSubmit = false;
      },
      handleSwitchChange(record){
        const that = this;
        if(record.enabled ==="1"){
          record.enabled = '0'
        }else {
          record.enabled = '1'
        }
        putAction(this.url.edit,record).then((res)=>{
          if(res.success){
            if(record.enabled === '0'){
              that.$message.success("开启成功");
              that.$emit('ok');
            }else {
              that.$message.success("关闭成功");
              that.$emit('ok');
            }

          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      },
      searchData(){
        this.queryParam.deviceId = this.deviceId
        this.loadData();
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'deviceId',text:'设备id'})
        fieldList.push({type:'string',value:'riskId',text:'风险项id'})
        fieldList.push({type:'string',value:'multiple',text:'参数1'})
        fieldList.push({type:'string',value:'valueOne',text:'参数2'})
        fieldList.push({type:'string',value:'valueTwo',text:'参数3'})
        fieldList.push({type:'string',value:'valueThree',text:'参数4'})
        fieldList.push({type:'string',value:'valueFour',text:'参数5'})
        fieldList.push({type:'string',value:'enabled',text:'是否启用'})
        this.superFieldList = fieldList
      },
      searchReset1() {
        this.queryParam = {};
        this.deviceId='';
        this.deviceName = '';
        this.loadData();
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>