<template>
  <div class="mod-config">
    <el-form :inline="true" label-position="top" label-width="100px" style="margin-top: 0px;">
      <el-form-item :label="'BU:'">
        <el-select v-model="searchData.buNo" placeholder="请选择"  >
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for = "i in buList"
            :key = "i.buNo"
            :label = "i.buDesc"
            :value = "i.buNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'HS Code:'">
        <el-input v-model="searchData.hsCode"  style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="' '" >
        <el-button @click="searchTable()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'查询'}}</el-button>
        <el-button @click="addModelOpen()" type="primary" style="margin-left: 2px;margin-top: 0px">{{'新增'}}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      @row-click="changeData"
      highlight-current-row
      :height="height"
      :data="dataList"
      ref="mainTable"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        header-align="center"
        align="center"
        width="100"
        fixed="left"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small"  @click="updateModelOpen(scope.row)">编辑</a>
          <a type="text" size="small"  @click="deleteHsCode(scope.row)">删除</a>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columnList1" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed==''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden"> {{scope.row[item.columnProp]}}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]"
                                             style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 1000]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-tabs style="font-size: 12px;min-height: 200px" class="customer-tab" v-model="activeName"   type="border-card" @tab-click="tabClick" >
      <el-tab-pane label="基本信息" name="attribute">
   <order-attribute ref="dialogAttribute" :site="currentRow.site" :buNo="currentRow.buNo" :order-no="currentRow.hsCode" :code-no="currentRow.codeNo" :function-type="'ECSS'"></order-attribute>
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="维护HSCode" :close-on-click-modal="false" v-drag :visible.sync="addModelFlag" width="500px">
      <el-form  label-position="top"  style="margin-left: 7px;margin-top: -5px;">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item :label="'BU:'">
              <el-select v-model="addModel.buNo" placeholder="请选择" :disabled="addModel.addFlag!==0"  style="width: 100%">
                <el-option
                  v-for = "i in buList"
                  :key = "i.buNo"
                  :label = "i.buDesc"
                  :value = "i.buNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'HS Code'" >
              <el-input v-model="addModel.hsCode" :disabled="addModel.addFlag!==0" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  >
              <span slot="label" @click="getBaseList(33)"><a herf="#">属性模板</a></span>
              <el-input v-model="addModel.codeNo"  ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item :label="'备注'" >
              <el-input v-model="addModel.remark"  ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-footer style="height:40px;margin-top: 10px;text-align:center">
        <el-button type="primary" @click="saveHSCode()">保存</el-button>
        <el-button type="primary" @click="addModelFlag=false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!--列表的组件-->
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
  import Chooselist from '@/views/modules/common/Chooselist_eam'
  import orderAttribute from "./orderProperties"
  import {} from "@/api/sysLanguage.js"
  import {
    searchHsCodeData,
    saveHSCodeData,
    deleteHsCode,
  }from "@/api/ecss/ecss.js"
  import {getAllBuList}from  '@/api/factory/site.js'
  export default {
    name: "null",
    components:{
      orderAttribute,
      Chooselist,
    },
    data() {
      return {
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        height: 200,
        buList:[],
        dataList:[],
        dataList2:[],
        dataListLoading: false,
        searchData: {
          page: 1,
          limit: 100,
          buNo:'',
          hsCode:'',
          codeNo:'',
          username:this.$store.state.user.name,
        },
        buttons:{
          search:'查询',
        },
        addModelFlag:false,
        addModel:{
          addFlag:0,
          site:'',
          buNo:'',
          hsCode:'',
          codeNo:'',
          remark:'',
        },
        addDisabledFlag:true,
        activeName:'attribute',
        columnList1: [
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1BuDesc',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "buDesc",
            headerAlign: "center",
            align: "left",
            columnLabel: "BU",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1HsCode',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "hsCode",
            headerAlign: "center",
            align: "left",
            columnLabel: "HS Code",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1CodeNo',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "codeNo",
            headerAlign: "center",
            align: "center",
            columnLabel: "模板编码",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1CreateBy',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "createBy",
            headerAlign: "center",
            align: "center",
            columnLabel: "创建人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1CreateDate',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "createDate",
            headerAlign: "center",
            align: "center",
            columnLabel: "创建日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1UpdateBy',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "updateBy",
            headerAlign: "center",
            align: "left",
            columnLabel: "修改人",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1UpdateDate',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "updateDate",
            headerAlign: "center",
            align: "center",
            columnLabel: "修改日期",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100
          },
          {
            userId: this.$store.state.user.name,
            functionId: 801003,
            serialNumber: '801003Table1Remark',
            tableId: "801003Table1",
            tableName: "HsCode基础信息",
            columnProp: "remark",
            headerAlign: "center",
            align: "left",
            columnLabel: "备注",
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 300
          },
        ],
        currentRow:{},
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.height = (window.innerHeight - 240)/2;
      })
    },
    methods: {
      // 获取基础数据列表S
      getBaseList(val, type) {
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = ''
          if (val === 33) {
            strVal = this.addModel.codeNo
          }
          this.$refs.baseList.init(val, strVal)
        })
      },

      /* 列表方法的回调 */
      getBaseData(val) {
        if (this.tagNo === 33) {
          this.addModel.codeNo = val.Code_No
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.searchTable()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.searchTable()
      },
      searchTable(){
        this.searchData.limit = this.pageSize
        this.searchData.page = this.pageIndex
        searchHsCodeData(this.searchData).then(({data}) => {
          //区分请求成功和失败的状况
          if (data && data.code == 0) {
            this.dataList = data.page.list
            this.pageIndex = data.page.currPage
            this.pageSize = data.page.pageSize
            this.totalPage = data.page.totalCount
            if(this.dataList.length>0){
              this.$refs.mainTable.setCurrentRow(this.dataList[0]);
              this.changeData(this.dataList[0])
            }else {
              this.changeData(null)
            }
          } else {
            this.dataList = [];
          }
        });
      },
      changeData(row){
        this.currentRow = JSON.parse(JSON.stringify(row));
        this.headerData=row;
        this.refreshCurrentTabTable ();
      },
      refreshCurrentTabTable(){
        if(this.currentRow===''||this.currentRow===null){
          this.currentRow={site:'',hsCode:'',buNo:'',recordType:'',codeNo:''}
        }
        if(this.activeName==='detail'){
          // searchEcssCoDelNotifyDetail(this.currentRow).then(({data}) => {
          //   //区分请求成功和失败的状况
          //   if (data && data.code == 0) {
          //     this.dataList2 = data.rows
          //
          //   } else {
          //     this.dataList2 = [];
          //   }
          // });
        }
      },
      tabClick (tab, event) {
        // 刷新列表数据
        this.refreshCurrentTabTable()
      },

      getBu () {
        let tempData = {
          username: this.$store.state.user.name,
        }
        getAllBuList(tempData).then(({data}) => {
          if (data.code === 0) {
            this.buList = data.rows
          }
        })
      },
      addModelOpen(){
        this.addDisabledFlag=true
        this.addModel={
          addFlag:0,
          site:'',
          buNo:'',
          hsCode:'',
          codeNo:'',
          remark:'',
        }
        this.addModelFlag=true
      },
      updateModelOpen(row){
        this.addDisabledFlag=false
        this.addModel={
          addFlag:1,
          site:row.site,
          buNo:row.buNo,
          hsCode:row.hsCode,
          codeNo:row.codeNo,
          remark:row.remark,
        }
        this.addModelFlag=true
      },
      deleteHsCode(row){
        this.$confirm('确认删除？', '提示').then(() => {
          deleteHsCode(row).then(({data}) => {
            if (data && data.code === 0) {
              this.searchTable()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {}
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        })
      },
      saveHSCode(){
        if(this.addModel.buNo==null||this.addModel.buNo===''){
          this.$alert('请选择BU！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.hsCode==null||this.addModel.hsCode===''){
          this.$alert('请输入HSCode！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        if(this.addModel.codeNo==null||this.addModel.codeNo===''){
          this.$alert('请选择属性模板！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        saveHSCodeData(this.addModel).then(({data}) => {
          if (data && data.code === 0) {
            this.searchTable()
            this.addModelFlag = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },
    },
    created() {
      this.getBu ()
    }
  }
</script>

<style scoped>

</style>
