<template>
  <div class="mod-config">
    <div>
        <span @click="favoriteFunction()">
         <icon-svg :name="favorite?'xiangqufill':'xiangqu'" class="sl-svg"></icon-svg>
        </span>
    </div>
    <el-form :inline="true" :model="querySysLanguagePack" @keyup.enter.native="getDataList()">
      <el-form-item :label="inputSearch1">
        <el-input style="width: 100px;" clearable placeholder="" v-model="mainQueryData.requisitionno"></el-input>
      </el-form-item>
      <el-form-item :label="inputSearch3">
        <el-input style="width: 100px;" clearable placeholder="" v-model="mainQueryData.partno"></el-input>
      </el-form-item>
      <el-form-item :label="inputSearch2">
        <el-select v-model="mainQueryData.orderType" placeholder="请选择" style="width: 100px;">
          <el-option
            key=""
            label="全部"
            value="">
          </el-option>
          <el-option
            v-for="item in orderTypeList"
            :key="item.baseData"
            :label="item.baseDesc"
            :value="item.baseDesc">
          </el-option>
        </el-select>
        <!--        <el-input style="width: 110px;" clearable placeholder="" v-model="mainQueryData.orderType"></el-input>-->
      </el-form-item>
      <el-form-item :label="inputSearch4">
        <el-select v-model="mainQueryData.authorizeFlag" style="width: 90px;">
          <el-option value="" label="全部"></el-option>
          <el-option value="Y" label="已审批"></el-option>
          <el-option value="N" label="未审批"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="inputSearch12">
        <el-select v-model="mainQueryData.approveresult" style="width: 110px;">
          <el-option value="" label="全部"></el-option>
          <el-option value="审批全通过" label="审批全通过"></el-option>
          <el-option value="审批未通过" label="审批未通过"></el-option>
          <el-option value="审批部分通过" label="审批部分通过"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList('Y')" type="primary">{{ buttons.search }}</el-button>
        <el-button @click="initAddModel(null)" type="primary">{{ buttons.addLabelName }}</el-button>
        <el-button @click="saveColumnList()" type="primary" v-show="showDefault">{{ buttons.defaultTable }}
        </el-button>
        <el-button @click="userSetting()" type="primary">{{ buttons.settingTable }}</el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :header="exportHeader"
          :footer="exportFooter"
          :defaultValue="exportDefaultValue"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          {{ buttons.download }}
        </download-excel>

        <el-button v-if="isAuth(':prd:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">{{ buttons.deleteList }}
        </el-button>
      </el-form-item>

    </el-form>
    <el-table
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">

      <el-table-column
        v-for="(item,index) in columnList" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed"
        :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-column
        fixed="right"
        header-align="center"
        align="center"
        width="120"
        :label="buttons.operating">
        <template slot-scope="scope">
          <a v-if="scope.row.authorizeFlag == 'N'" @click="initAddModel(scope.row)">{{ buttons.edit }}</a>
          <a v-if="scope.row.authorizeFlag == 'N'" @click="updateAuthorizeFlag(scope.row)">{{ buttons.close }}</a>
          <a
            v-if="scope.row.authorizeFlag == 'Y' && scope.row.approvedFlag != 'Y' && scope.row.approveresult == '审批未通过'"
            @click="initReapply(scope.row)">{{ buttons.reapply }}</a>
          <a @click="initReviewDetails(scope.row.requisitionno)">{{ buttons.reviewDetails }}</a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 50, 200, 10000]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog :close-on-click-modal="false" :show-close="false" :close-on-press-escape="false" title="设置table列"
               :visible.sync="visible" width="1000px" v-drag>
      <el-form @keyup.enter.native="updateColumnList()"
               v-model="userColumnList"
               inline="inline"
               size="mini"
               label-width="80px">
        <el-form-item v-for="(item,index) in userColumnList"
                      :key="item.columnProp"
                      :label="item.columnLabel"
                      :prop="item.columnProp">
          <el-form-item>
            排序
            <el-input-number v-model="item.sortLv" controls-position="right" size="mini" :min="0"
                             :max="10"></el-input-number>
          </el-form-item>
          <el-form-item>
            <el-switch
              v-model="item.status"
              active-text="显示"
              inactive-text="隐藏">
            </el-switch>
          </el-form-item>
          <el-form-item>
            <el-switch
              v-model="item.columnSortable"
              active-text="排序"
              inactive-text="不排序">
            </el-switch>
          </el-form-item>
          <el-form-item>
            <el-switch
              v-model="item.fixed"
              active-text="固定"
              inactive-text="不固定">
            </el-switch>
          </el-form-item>
          <el-form-item>
            宽度
            <el-input-number v-model="item.columnWidth" controls-position="right" size="mini" :min="0"
                             :max="1000"></el-input-number>
          </el-form-item>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="updateColumnList()">确定</el-button>
          </span>
    </el-dialog>


    <el-dialog title="申请单信息" :visible.sync="addPRDetailFlag" width="672px" :close-on-click-modal="false"
               :close-on-press-escape="false" v-drag>
      <el-form :inline="true" label-position="top">
        <el-form-item :label="inputSearch5">
          <el-select v-model="currentPRDetailData.orderType" placeholder="请选择" filterable style="width: 143px">
            <el-option
              v-for="(item ,index) in orderTypeList"
              :key="index"
              :label="item.baseDesc"
              :value="item.baseData"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="inputSearch10">
          <el-select v-model="currentPRDetailData.supplierid" filterable placeholder="请选择" style="width: 143px">
            <el-option
              v-for="(item ,index) in supplierList"
              :key="index"
              :label="item.suppliername"
              :value="item.supplierid"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="inputSearch6">
          <el-input style="text-align: left;width: 143px" onkeyup="this.value = this.value.toUpperCase()"
                    v-model="currentPRDetailData.partno"></el-input>
        </el-form-item>
        <el-form-item :label="inputSearch11">
          <el-input style="text-align: left;width: 143px" v-model="currentPRDetailData.partdesc"></el-input>
        </el-form-item>
        <el-form-item :label="inputSearch7">
          <el-input style="text-align: left;width: 143px" @change="getNumber()"
                    oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
                    v-model="currentPRDetailData.qty"></el-input>
        </el-form-item>
        <el-form-item :label="inputSearch8">
          <el-input style="text-align: left;width: 143px" v-model="currentPRDetailData.price" @change="getNumber()"
                    oninput="value=value.replace(/^\.+|[^\d.]/g,'')"></el-input>
        </el-form-item>
        <el-form-item :label="inputSearch9">
          <el-input style="text-align: left;width: 143px"
                    v-model="currentPRDetailData.totalPrice"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 30px">
          <upload v-on:childByValue="childByValue"></upload>
        </el-form-item>
        <el-table
          height="170"
          :data="fileList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <el-table-column
            v-for="(item,index) in columnList2" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed"
            :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-column
            header-align="center"
            align="center"
            width="50"
            :label="buttons.operating">
            <template slot-scope="scope">
              <a @click="fileDownload(scope.row)">{{ buttons.fileDownload }}</a>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="addPRDetailFlag = false">取消</el-button>
      <el-button type="primary" :disabled="bannersBut" @click="savePRDetail()">确定</el-button>
    </span>
    </el-dialog>

    <el-dialog title="申请单审核" :visible.sync="reviewDetailsFlag" width="1208px" :close-on-click-modal="false"
               v-drag>
      <el-table
        :height="height"
        :data="reviewDetailsList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <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"
          :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-dialog>

  </div>
</template>

<script>

import {
  searchSysLanguagePackList,
  searchSysLanguageParam,
  searchFunctionButtonList,
  saveButtonList,
  searchSysLanguage,
  searchLanguageListByLanguageCode,
  saveSysLanguageOne
} from "@/api/sysLanguage.js"
import {
  saveTableDefaultList,
  saveTableUser,
  getTableDefaultListLanguage,
  getTableUserListLanguage
} from "@/api/table.js"
import {
  userFavoriteList, saveUserFavorite, removeUserFavorite
} from '@/api/userFavorite.js'
import {
  getPRHeaderList,
  getTblBaseDataList,
  savePRDetail,
  getSupplierList,
  getExportList,
  authorizationHistList,
  updatePRHeaderAuthorizeFlag,
  downFtpFile,
  getFileList,
} from '@/api/purchaseorder/purchaseRequisition.js'
import upload from "../common/upload";
import axios from "axios";
import Vue from "vue";

export default {
  components: {upload},
  data() {
    return {
      insertFileList: [],
      fileList: [],
      // table 高度
      height: 0,
      bannersBut: false,
      addPRDetailFlag: false,
      inputSearch1: '申请单号',
      inputSearch2: '处理类型',
      inputSearch3: '刀模编码',
      inputSearch4: '是否审批',
      inputSearch5: '处理类型',
      inputSearch6: '刀模编码',
      inputSearch7: '申请数量',
      inputSearch8: '预估单价',
      inputSearch9: '预估总价',
      inputSearch10: '供应商名称',
      inputSearch11: '刀模名称',
      inputSearch12: '审批结果',
      currentPRDetailData: {
        username: this.$store.state.user.name,
        requisitionno: '',
        site: this.$store.state.user.site,
        itemno: '',
        supplierid: '',
        suppliername: '',
        orderType: '',
        partno: '',
        partdesc: '',
        qty: 0,
        price: 0,
        totalPrice: 0,
        convertFactor: 0,
        currency: '',
        status: '',
        taxcode: '',
        currencyRate: 1,
        additionalCose: 0,
        orderref1: '',
        orderref2: '',
        sysOssEntityList: [],
      },
      mainQueryData: {
        requisitionno: '',
        orderType: '',
        partno: '',
        authorizeFlag: 'N',
        approveresult: '',
        site: this.$store.state.user.site,
        userId: this.$store.state.user.name,
        limit: '',
        page: '',
      },
      userId: this.$store.state.user.name,
      site: this.$store.state.user.site,
      //审核详情
      reviewDetailsFlag: false,
      //审核显示list
      reviewDetailsList: [],
      // 是否收藏
      favorite: false,
      addLanguage: false,
      functionId: 2001,
      tableId: "common1001",
      value1: true,
      visible: false,
      showDefault: false,
      // 默认table 查询参数
      queryTable: {
        functionId: 2001,
        tableId: 'PRHeaderTable',
        languageCode: this.$i18n.locale
      },
      // 用户table 查询参数
      queryTableUser: {
        userId: this.$store.state.user.name,
        functionId: 2001,
        tableId: "PRHeaderTable",
        status: true,
        languageCode: this.$i18n.locale
      },
      // 语言词典查询参数
      querySysLanguageParam: {
        languageCode: this.$i18n.locale
      },
      // 语言词典集合
      sysLanguageParams: [],
      // 用户table 配置集合
      userColumnList: [],
      // 展示列集
      columnList: [
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001RequisitionNo',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "requisitionno",
          headerAlign: "center",
          align: "center",
          columnLabel: "申请单单号",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001OrderType',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "orderType",
          headerAlign: "center",
          align: "center",
          columnLabel: "处理类型",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001Partno',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "partno",
          headerAlign: "center",
          align: "center",
          columnLabel: "刀模编码",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001Partdesc',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "partdesc",
          headerAlign: "center",
          align: "center",
          columnLabel: "刀模名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001Qty',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "qty",
          headerAlign: "center",
          align: "center",
          columnLabel: "申请数量",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001Price',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "price",
          headerAlign: "center",
          align: "center",
          columnLabel: "预估价格",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001TotalPrice',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "totalPrice",
          headerAlign: "center",
          align: "center",
          columnLabel: "预估总价",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001Supplierid',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "supplierid",
          headerAlign: "center",
          align: "center",
          columnLabel: "供应商编码",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001Suppliername',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "suppliername",
          headerAlign: "center",
          align: "center",
          columnLabel: "供应商名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          serialNumber: 'PRHeader2001AuthorizeFlag',
          tableId: "PRHeaderTable",
          tableName: "PRHeader",
          columnProp: "strAuthorizeFlag",
          headerAlign: "center",
          align: "center",
          columnLabel: "是否审批",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2101,
          serialNumber: 'review2101AuthorizeFlag',
          tableId: "review2101",
          tableName: "PRHeader",
          columnProp: "approveresult",
          headerAlign: "center",
          align: "left",
          columnLabel: "审批结果",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
      ],
      // 展示列集
      columnList1: [
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "stepId",
          headerAlign: "center",
          align: "center",
          columnLabel: "审批步骤号",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "authTypeDb",
          headerAlign: "center",
          align: "center",
          columnLabel: "类型",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "userName",
          headerAlign: "center",
          align: "center",
          columnLabel: "账号",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "userDisplay",
          headerAlign: "center",
          align: "center",
          columnLabel: "用户名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "authRuleId",
          headerAlign: "center",
          align: "center",
          columnLabel: "审批组编码",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "strRuleDesc",
          headerAlign: "center",
          align: "center",
          columnLabel: "审批组名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "strActAuthorizator",
          headerAlign: "center",
          align: "center",
          columnLabel: "最终审批人",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "actAuthorizeDate",
          headerAlign: "center",
          align: "center",
          columnLabel: "最终审批时间",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
          columnWidth: 135,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "strCanAuthFlag",
          headerAlign: "center",
          align: "center",
          columnLabel: "是否可以审批",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "strAuthFlag",
          headerAlign: "center",
          align: "center",
          columnLabel: "是否审批",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "approveResult",
          headerAlign: "center",
          align: "center",
          columnLabel: "审批结果",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "reviewDetails2001",
          tableName: "reviewDetails",
          columnProp: "remark",
          headerAlign: "center",
          align: "center",
          columnLabel: "审批备注",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
        },
      ],
      // 展示列集
      columnList2: [
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "fileDetail2001",
          tableName: "fileDetailTable",
          columnProp: "fileName",
          headerAlign: "center",
          align: "center",
          columnLabel: "文件名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "fileDetail2001",
          tableName: "fileDetailTable",
          columnProp: "createDate",
          headerAlign: "center",
          align: "center",
          columnLabel: "创建时间",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
          columnWidth: 135,
        },
        {
          userId: this.$store.state.user.name,
          functionId: 2001,
          tableId: "fileDetail2001",
          tableName: "fileDetailTable",
          columnProp: "createdBy",
          headerAlign: "center",
          align: "center",
          columnLabel: "创建人",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: false,
          columnWidth: 70,
        },
      ],
      // 数据集
      dataList: [],
      //选项
      orderTypeList: [],
      supplierList: [],
      queryButton: {
        functionId: 2001,
        table_id: 'common1001',
        languageCode: this.$i18n.locale,
        objectType: 'button'
      },
      buttons: {
        add: '添加',
        edit: '编辑',
        delete: '删除',
        deleteList: '批量删除',
        cz: '操作',
        search: '查询',
        download: '导出',
        settingTable: '设置列表',
        defaultTable: '设置默认配置',
        addLabelName: '新增',
        operating: '操作',
        reviewDetails: "审批详情",
        close: '关闭',
        fileDownload: '下载',
        reapply: '重新申请',
      },
      // 导出 start
      exportData: [],
      exportDataStandard: {
        "功能编码": "functionId",
        "序列化编码": "objectId",
        "类型": "objectType",
        "语言值": "languageValue"
      },
      exportName: "刀申请",
      exportHeader: ["刀申请"],
      exportFooter: [],
      exportDefaultValue: "这一行这一列没有数据",
      // 导出 end
      buttonList: [
        // {
        //   functionId: "9001",
        //   languageValue: '添加',
        //   objectId: 'add',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '编辑',
        //   objectId: 'edit',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '删除',
        //   objectId: 'delete',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '批量删除',
        //   objectId: 'deleteList',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '操作',
        //   objectId: 'cz',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '查询',
        //   objectId: 'search',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '导出',
        //   objectId: 'download',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '设置列表',
        //   objectId: 'settingTable',
        //   objectType: "button",
        //   tableId: "common1001"
        // },
        // {
        //   functionId: "9001",
        //   languageValue: '设置默认配置',
        //   objectId: 'defaultTable',
        //   objectType: "button",
        //   tableId: "common1001"
        // }
      ],
      languageList: [],
      languageColumnList: [],
      languageDataList: [],
      queryLanguage: {},
      // 数据集条件
      querySysLanguagePack: {
        page: 1,
        limit: 1,
        languageValue: '',
        objectType: '',
        objectId: ''
      },
      //
      // 分页
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,

    }

  },
  mounted() {
    this.$nextTick(() => {
      this.height = window.innerHeight - 230;
    })
  },
  activated() {
    this.getDataList()
    this.getLanguageList()
    this.initTblBaseDataList()
    this.initSupplier()
  },

  methods: {
    //获取申请单号文件信息
    getFileList(row) {
      let jsonData = {
        site: row.site,
        type: 'PRFile',
        associatedField1: row.requisitionno
      }
      this.fileList = []
      getFileList(jsonData).then(({data}) => {
        if (data.code == 0) {
          this.fileList = data.row
        }
      })
    },
    // 文件下载
    fileDownload(row) {
      axios.get('/api/ftp/file/downFtpFile/' + row.id, {
        responseType: 'blob',
        headers: {
          'Content-Type': 'application/json',
          'token':Vue.cookie.get('token')
        }
      }).then(({data}) => {
        // 不限制文件下载类型
        const blob = new Blob([data], {type: "application/octet-stream"})
        // 下载文件名称
        const fileName = row.fileName
        // a标签下载
        const linkNode = document.createElement('a')
        linkNode.download = fileName // a标签的download属性规定下载文件的名称
        linkNode.style.display = 'none'
        linkNode.href = window.URL.createObjectURL(blob) // 生成一个Blob URL
        document.body.appendChild(linkNode)
        linkNode.click() // 模拟在按钮上的一次鼠标单击
        URL.revokeObjectURL(linkNode.href) // 释放URL 对象
        document.body.removeChild(linkNode)
      })
    },
    childByValue(childValue) {
      // childValue就是子组件传过来的值
      this.fileList.push(childValue)
      this.insertFileList.push(childValue)
    },
    updateAuthorizeFlag(row) {
      let saveData = {
        site: row.site,
        requisitionno: row.requisitionno,
        status: '已关闭',
        authorizeFlag: 'Y',
        approvedFlag: 'N',
      }
      updatePRHeaderAuthorizeFlag(saveData).then(({data}) => {
        if (data.code == 0) {
          this.getDataList()
        }
        this.$alert(data.msg, '操作提示', {
          confirmButtonText: '确定',
          callback: action => {

          }
        });
      })
    },
    initReviewDetails(orderRef1) {
      this.reviewDetailsFlag = true
      this.reviewDetailsList = []
      this.getAuthorizationHistList(orderRef1)
    },
    getAuthorizationHistList(orderRef1) {
      let queryData = {
        site: this.site,
        orderRef1: orderRef1,
        authRuleId: 5,
      }
      authorizationHistList(queryData).then(({data}) => {
        this.reviewDetailsList = data.rows
      })
    },
    getNumber() {
      this.currentPRDetailData.totalPrice = this.decimalUtil.mul(this.currentPRDetailData.qty, this.currentPRDetailData.price)
    },
    savePRDetail() {
      this.bannersBut = true
      if (this.currentPRDetailData.orderType == null || this.currentPRDetailData.orderType.trim() === "") {
        this.$alert('请选择处理类型！', '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.bannersBut = false
          }
        });
        return
      } else if (this.currentPRDetailData.supplierid == null || this.currentPRDetailData.supplierid.trim() === "") {
        this.$alert('请选择供应商名称！', '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.bannersBut = false
          }
        });
        return
      } else if (this.currentPRDetailData.partno == null || this.currentPRDetailData.partno.trim() === "") {
        this.$alert('请输入刀模编码！', '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.bannersBut = false
          }
        });
        return
      } else if (this.currentPRDetailData.qty === 0 || this.currentPRDetailData.qty <= 0) {
        this.$alert('请输入申请数量！', '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.bannersBut = false
          }
        });
        return
      } else if (this.currentPRDetailData.price === 0 || this.currentPRDetailData.price <= 0) {
        this.$alert('请输入预估单价！', '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.bannersBut = false
          }
        });
        return
      } else if (this.currentPRDetailData.partDesc === 0 || this.currentPRDetailData.partDesc <= 0) {
        this.$alert('请输入刀模名称！', '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            this.bannersBut = false
          }
        });
        return
      }
      let partNo = this.currentPRDetailData.partno.toUpperCase()
      this.currentPRDetailData.partno = partNo
      let filter = this.supplierList.filter(item => item.supplierid === this.currentPRDetailData.supplierid);
      this.currentPRDetailData.suppliername = filter[0].suppliername
      this.currentPRDetailData.currency = filter[0].currency
      this.currentPRDetailData.taxcode = filter[0].taxcode
      this.currentPRDetailData.fileType = 'PRFile'
      this.currentPRDetailData.sysOssEntityList = this.insertFileList
      savePRDetail(this.currentPRDetailData).then(({data}) => {
        this.bannersBut = false
        if (data.code == 0) {
          this.bannersFlag = false
          this.getDataList()
        }
        this.$alert(data.msg, '操作提示', {
          confirmButtonText: '确定',
          callback: action => {
            if (data.code == 0) {
              this.addPRDetailFlag = false
            }
            this.addPRDetailFlag = false
            this.bannersBut = false
          }
        });
      })
    },
    initSupplier() {
      let queryData = {
        site: this.site
      }
      getSupplierList(queryData).then(({data}) => {
        this.supplierList = data.rows
      })
    },
    initTblBaseDataList() {
      let queryData = {
        type: 'orderType'
      }
      getTblBaseDataList(queryData).then(({data}) => {
        this.orderTypeList = data.rows
      })
    },
    initReapply(row) {
      this.currentPRDetailData.orderref1 = row.requisitionno
      this.currentPRDetailData.orderref2 = row.itemno
      this.initAddModel(null, 'Y')
    },
    initAddModel(row, val) {
      this.fileList = []
      this.insertFileList = []
      if (row == null) {
        this.currentPRDetailData.requisitionno = ''
        this.currentPRDetailData.itemno = ''
        this.currentPRDetailData.supplierid = ''
        this.currentPRDetailData.suppliername = ''
        this.currentPRDetailData.orderType = ''
        this.currentPRDetailData.partno = ''
        this.currentPRDetailData.qty = 0
        this.currentPRDetailData.price = 0
        this.currentPRDetailData.status = '已下达'
        this.currentPRDetailData.taxCode = ''
        this.currentPRDetailData.partdesc = ''
      } else {
        this.currentPRDetailData.requisitionno = row.requisitionno
        this.currentPRDetailData.itemno = row.itemno
        this.currentPRDetailData.supplierid = row.supplierid
        this.currentPRDetailData.suppliername = row.suppliername
        this.currentPRDetailData.orderType = row.orderType
        this.currentPRDetailData.partno = row.partno
        this.currentPRDetailData.qty = row.qty
        this.currentPRDetailData.price = row.price
        this.currentPRDetailData.status = ''
        this.currentPRDetailData.taxCode = row.taxCode
        this.currentPRDetailData.partDesc = row.partdesc
        this.getFileList(row)
      }
      if (val != 'Y') {
        this.currentPRDetailData.orderref1 = null
        this.currentPRDetailData.orderref2 = null
      }
      this.getNumber()
      this.bannersBut = false
      this.addPRDetailFlag = true
    },
    // 校验用户是否收藏
    favoriteIsOk() {
      let userFavorite = {
        userId: this.$store.state.user.id,
        languageCode: this.$i18n.locale
      }
      userFavoriteList(userFavorite).then(({data}) => {
        let size = data.list.filter(item => item.userId == userFavorite.menuId).length;
        if (size > 0) {
          this.favorite = true
        } else {
          this.favorite = false
        }
      })
    },
    // 收藏 OR 取消收藏
    favoriteFunction() {
      let userFavorite = {
        userId: this.$store.state.user.id,
        functionId: this.$route.meta.menuId,
      }
      if (this.favorite) {
        // 取消收藏
        this.$confirm(`确定取消收藏`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          removeUserFavorite(userFavorite).then(({data}) => {
            this.$message.success(data.msg)
            this.favorite = false
          })
        })
      } else {
        // 收藏
        saveUserFavorite(userFavorite).then(({data}) => {
          this.$message.success(data.msg)
          this.favorite = true
        })
      }

    },
    // 保存语言编辑
    saveLanguageList(val) {
      saveSysLanguageOne(val).then(({data}) => {
        if (data.code == 0) {
          this.$message.success(data.msg)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 编辑语言
    addLanguageFun() {
      if (this.querySysLanguagePack.languageCode) {
        this.addLanguage = true
        this.languageColumnList = []
        let query = {
          functionId: 2001,
          tableId: "common1002",
          languageCode: this.$i18n.locale
        }
        getTableDefaultListLanguage(query).then(({data}) => {
          if (data.rows.length > 0) {
            this.languageColumnList = data.rows
          }
        })
        this.languageColumnList = this.columnList
        searchLanguageListByLanguageCode(this.querySysLanguagePack).then(({data}) => {
          this.languageDataList = data.rows
        })
      } else {
        this.$message("请选中一种语言")
      }
    },
    // 获取多语言列表
    getLanguageList() {
      searchSysLanguage(this.queryLanguage).then(({data}) => {
        this.languageList = data.rows
      })
    },
    // 获取button的词典
    getFunctionButtonList() {
      searchFunctionButtonList(this.queryButton).then(({data}) => {
        if (data.data.length > 0) {
          this.buttons = data.data
        } else {
          saveButtonList(this.buttonList).then(({data}) => {
          })
        }
      })
    },
    // 获取语言词典
    getSysLanguageParamList() {
      searchSysLanguageParam(this.querySysLanguageParam).then(({data}) => {
        this.sysLanguageParams = data.rows
      })
    },
    // 打开页面设置
    userSetting() {
      this.visible = true;
      this.getTableUserAll()
    },
    // 获取 用户的配置
    getTableUserAll() {
      let queryTable = {
        userId: this.$store.state.user.name,
        functionId: 2001,
        tableId: "common1001",
        languageCode: this.$i18n.locale
      }
      getTableUserListLanguage(queryTable).then(({data}) => {
        if (data.code == 0) {
          this.userColumnList = data.rows
          if (data.rows.length <= 0) {
            getTableDefaultListLanguage(this.queryTable).then(({data}) => {
              this.userColumnList = data.rows

            })
          }
        }

      })
    },
    // 修改 table 列
    updateColumnList() {
      let userColumns = []
      for (let column of this.userColumnList) {
        let userColumn = {
          userId: this.$store.state.user.name,
          functionId: column.functionId,
          tableId: column.tableId,
          tableName: column.tableName,
          columnProp: column.columnProp,
          headerAlign: column.headerAlign,
          align: column.align,
          columnWidth: column.columnWidth,
          columnLabel: column.columnLabel,
          columnHidden: column.columnHidden,
          columnImage: column.columnImage,
          columnSortable: column.columnSortable,
          format: column.format,
          sortLv: column.sortLv,
          status: column.status,
          fixed: column.fixed,
          serialNumber: column.serialNumber
        }
        userColumns.push(userColumn)
      }
      saveTableUser(userColumns).then(({data}) => {
        if (data.code == 0) {
          this.$message.success(data.msg)
          this.visible = false
          this.getTableUserColumn()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 获取 用户保存的 格式列
    getTableUserColumn() {
      getTableUserListLanguage(this.queryTableUser).then(({data}) => {
        if (data.rows.length > 0) {
          //this.columnList = []
          this.columnList = data.rows
        } else {
          this.getColumnList()
        }

      })
    },
    // 保存 默认配置 列
    saveColumnList() {

      saveTableDefaultList(this.columnList).then(({data}) => {
        if (data.code == 0) {
          this.$message.success(data.msg)
          this.showDefault = false
        } else {
          this.$message.error(data.msg)
        }
      })
      saveTableDefaultList(this.columnList1).then(({data}) => {
        if (data.code == 0) {
          this.$message.success(data.msg)
          this.showDefault = false
        } else {
          this.$message.error(data.msg)
        }
      })
      this.getFunctionButtonList();
      this.getColumnList()
    },
    // 获取 tableDefault 列
    getColumnList() {
      getTableDefaultListLanguage(this.queryTable).then(({data}) => {
        if (!data.rows.length == 0) {
          this.showDefault = false
          this.columnList = data.rows
        } else {
          this.showDefault = true
        }
      })
    },
    // 获取数据列表
    getDataList(val) {
      this.dataListLoading = true
      if (val == 'Y') {
        this.pageIndex = 1
      }
      this.mainQueryData.limit = this.pageSize
      this.mainQueryData.page = this.pageIndex
      getPRHeaderList(this.mainQueryData).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.rows.list
          this.totalPage = data.rows.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('//prd/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    createExportData() {
      // 点击导出按钮之后,开始导出数据之前的执行函数,返回值为需要下载的数据
      // TODO:构造需要下载的数据返回
      getExportList(this.mainQueryData).then(({data}) => {
        if (data && data.code === 0) {
          return data.rows.list
        }
        return this.dataList;
      })
    },
    startDownload() {
      //  this.exportData = this.dataList
      console.log("数据开始")
    },
    finishDownload() {
      console.log("数据下载完成")
    },
    fields() {
      let json = "{"
      this.columnList.forEach((item, index) => {
        if (index == this.columnList.length - 1) {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
        } else {
          json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
        }
      })
      json += "}"
      let s = eval("(" + json + ")")

      return s
    },
  },
  created() {
    this.getTableUserColumn()
    this.getSysLanguageParamList()
    this.getFunctionButtonList()
    this.favoriteIsOk()
  }
}
</script>
<style>
.sl-input {
  background-color: transparent;
  border: 0 !important;
  font-size: 12px !important;
  height: 12px !important;
  line-height: 14px !important;
  background-color: transparent !important;
  width: 140px;
}

.sl-input:focus, textarea:focus {

  outline: none;

}

.sl-svg {
  overflow: hidden;
  float: right;
}

</style>
