<template>
  <!-- 物资入院申请 -->
  <div>
    <el-card class="el-card">
      <el-form v-if="queryList && queryList.length > 0" :model="filterQuery" label-width="100px" v-show="showSearch">
        <!--        <el-row>-->
        <!--          <el-col :span="8">-->
        <!--            <el-form-item label="DI产品标识:">-->
        <!--              <el-input-->
        <!--                v-model="filterQuery.code"-->
        <!--                style="width: 90%"-->
        <!--                placeholder="请输入DI产品标识"-->
        <!--                clearable-->
        <!--                @keyup.enter.native="keyupErp_submit($event)"-->
        <!--              ></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="8">-->
        <!--            <el-form-item label="物资名称:">-->
        <!--              <el-input-->
        <!--                v-model="filterQuery.name"-->
        <!--                style="width: 90%"-->
        <!--                clearable-->
        <!--                placeholder="请输入物资名称"-->
        <!--              ></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="8">-->
        <!--            <el-form-item label="规格型号:">-->
        <!--              <el-input-->
        <!--                v-model="filterQuery.spec"-->
        <!--                style="width: 90%"-->
        <!--                clearable-->
        <!--                placeholder="请输入规格型号"-->
        <!--              ></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
        <!--        <el-row>-->
        <!--          <el-col :span="8">-->
        <!--            <el-form-item label="注册证编号:">-->
        <!--              <el-input-->
        <!--                v-model="filterQuery.registerNo"-->
        <!--                style="width: 90%"-->
        <!--                clearable-->
        <!--                placeholder="请输入注册证编号"-->
        <!--              ></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="8">-->
        <!--            <el-form-item label="审核状态:">-->
        <!--              <el-select-->
        <!--                v-model="filterQuery.checkStatus"-->
        <!--                style="width: 90%"-->
        <!--                placeholder="请选择审核状态"-->
        <!--              >-->
        <!--                <el-option label="全部" value=""></el-option>-->
        <!--                <el-option label="已审核" value="2"></el-option>-->
        <!--                <el-option label="未审核" value="1"></el-option>-->
        <!--                <el-option label="未通过" value="3"></el-option>-->
        <!--              </el-select>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
        <el-row style=" display:flex;  flex-wrap: wrap; ">
          <template v-for="(item, index) in queryList">
            <el-form-item
              v-if="item.columnType == 'input' && item.columnName !='queryName' && executeEval(row,item.expression,true)"
              :label="item.columnDesc+`:`" :key="item.id">
              <el-input
                v-model="filterQuery[item.columnName]"
                :placeholder="item.columnDesc"
                :disabled="executeEval(null,item.disabledFuc,false)"
                @keyup.enter.native="executeFuc($event,'5',item.clickFuc)"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item
              v-if="item.columnType == 'input' && item.columnName =='queryName' && executeEval(row,item.expression,true)"
              :label="item.columnDesc+`:`" :key="item.id">
              <el-autocomplete
                class="inline-input"
                style="width: 90%"
                v-model="queryName"
                :fetch-suggestions="queryProductName"
                placeholder="请输入内容"
                clearable
                @select="handleSelect"
                @clear="clearQueryProduct"
              ></el-autocomplete>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'select' && executeEval(row,item.expression,true)"
                          :label="item.columnDesc+`:`">
              <el-select v-model="filterQuery[item.columnName]"
                         :placeholder="item.columnDesc"
                         @change="executeFuc($event,'5',item.checkRules)"
                         :disabled="executeEval(null,item.disabledFuc,false)"
                         clearable>
                <el-option
                  v-for="dict in item.lableRuleObj"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
                <span style="float: left">{{ item.name }}</span>
                <span v-if="item.isShowXx == '1'" style="float: right; color: #8492a6; font-size: 13px">{{
                    item.code
                  }}</span>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'selectServer' && executeEval(row,item.expression,true)"
                          :label="item.columnDesc+`:`">
              <el-select
                v-model="filterQuery[item.columnName]"
                :placeholder="item.columnDesc"
                :disabled="executeEval(null,item.disabledFuc,false)"
                filterable
                remote
                @change="executeFuc($event,'5',item.checkRules)"
                :remote-method="(query) => executeFuc(query,'5',item.clickFuc)"
                clearable>
                <el-option
                  v-for="item in options[item.clickFuc]"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                />
                <span style="float: left">{{ item.name }}</span>
                <span v-if="item.isShowXx == '1'" style="float: right; color: #8492a6; font-size: 13px">{{
                    item.code
                  }}</span>
              </el-select>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'datePicker' && executeEval(row,item.expression,true)"
                          :label="item.columnDesc+`:`">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item v-if="item.columnType == 'date' && executeEval(row,item.expression,true)"
                          :label="item.columnDesc+`:`">
              <el-date-picker
                v-model="filterQuery[item.columnName]"
                :style="`width:${item.width+'px'}`"
                value-format="yyyy-MM-dd"
                :disabled="executeEval(null,item.disabledFuc,false)"
                type="date"
                :placeholder="item.columnDesc"
              ></el-date-picker>
            </el-form-item>
          </template>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group style="display: flex">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch"
          >显示/隐藏搜索栏
          </el-button
          >
          <el-button type="primary" icon="el-icon-refresh" @click="onReset"
          >重置
          </el-button
          >
          <el-button type="primary" icon="el-icon-search" @click="search"
          >查询
          </el-button
          >
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="addDiProductVisible = true"
          >选入DI信息
          </el-button
          >
<!--          <el-button-->
<!--            type="primary"-->
<!--            icon="el-icon-plus"-->
<!--            @click="addProductVisible = true"-->
<!--          >添加院内字典-->
<!--          </el-button-->
<!--          >-->

<!--          <el-upload-->
<!--            :action="uploadFileUrl"-->
<!--            multiple-->
<!--            :limit="1"-->
<!--            :headers="headers"-->
<!--            :data="uploadData"-->
<!--            :show-file-list="false"-->
<!--            accept=".xls,.xlsx"-->
<!--            :on-success="handleChange"-->
<!--            :file-list="fileList"-->
<!--          >-->
<!--            <el-button icon="el-icon-upload2" type="primary">Excel导入</el-button>-->
<!--          </el-upload>-->
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table
        :data="list"
        style="width: 100%"
        v-loading="loading"
        border
        highlight-current-row="true"
      >
        <!--        <el-table-column label="序号" type="index"></el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="产品标识"-->
        <!--          prop="code"-->
        <!--          show-overflow-tooltip-->
        <!--        ></el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="物资名称"-->
        <!--          prop="name"-->
        <!--          show-overflow-tooltip-->
        <!--        ></el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="规格型号"-->
        <!--          prop="spec"-->
        <!--          show-overflow-tooltip-->
        <!--        ></el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="医疗器械注册人"-->
        <!--          prop="ylqxzcrbarmc"-->
        <!--          show-overflow-tooltip-->
        <!--        ></el-table-column>-->
        <!--        <el-table-column-->
        <!--          label="注册证编号"-->
        <!--          prop="registerNo"-->
        <!--          show-overflow-tooltip-->
        <!--        ></el-table-column>-->
        <!--        <el-table-column label="审核状态" prop="status" show-overflow-tooltip>-->
        <!--          <template slot-scope="scope">-->
        <!--            <span>{{ checkMap[scope.row.status] }}</span>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="操作" width="120">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-button type="text" size="small" @click="detail(scope.row)"-->
        <!--              >详情</el-button-->
        <!--            >-->
        <!--            <el-button-->
        <!--              type="text"-->
        <!--              size="small"-->
        <!--              :disabled="scope.row.status == 2"-->
        <!--              @click="deleteProduct(scope.row.id)"-->
        <!--            >-->
        <!--              删除-->
        <!--            </el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <template v-for="(item, index) in tableHeader">
          <el-table-column
            v-if="item.columnType == 'id' && executeEval(row,item.expression,true)"
            type="index" :label="item.columnDesc"></el-table-column>
          <el-table-column
            v-if="item.columnType == 'selection'"
            type="selection"
            :width="item.width"
            :selectable="(row,number) => executeFuc(row,'3',item.clickFuc)"
          ></el-table-column>

          <el-table-column
            v-if="item.columnType == 'radio' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="radioCheck"><span></span></el-radio>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'laber' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template slot-scope="scope">
              <span :style="{color: executeFuc(scope.row,'4',item.lableRuleObj[scope.row[item.columnName]])}">{{
                  item.lableRuleObj[scope.row[item.columnName]]
                }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'eltag' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template slot-scope="scope">
              <el-tag
                :type="executeFuc(scope.row,'4',item,item.lableRuleObj?item.lableRuleObj[scope.row[item.columnName]]:scope.row[item.columnName])">
                <span>{{
                    item.lableRuleObj ? item.lableRuleObj[scope.row[item.columnName]] : scope.row[item.columnName]
                  }}</span>
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'button' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :width="item.width"
            :key="item.columnName"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button v-for="(buttonItem, buttonIndex) in item.buttonRulObj"
                         :type="buttonItem.type"
                         :size="buttonItem.size"
                         :style="buttonItem.style"
                         :key="buttonItem"
                         v-if="executeEval(scope.row,buttonItem.hasPermi,true)"
                         :disabled="executeEval(scope.row,buttonItem.disabledFuc,false)"
                         @click.native.stop="executeFuc(scope.row,'1',buttonItem.clickFuc)"
              >{{ buttonItem.name }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            v-if="item.columnType == 'text' && executeEval(row,item.expression,true)"
            :prop="item.columnName"
            :label="item.columnDesc"
            :sortable="item.sort"
            :width="item.width"
            :show-overflow-tooltip="item.tooltip"
            :key="item.columnName"
          >
            <template slot-scope="scope">
              <span :style="{color: executeFuc(scope.row,'4',item,scope.row[item.columnName])}">{{
                  scope.row[item.columnName]
                }}</span>
            </template>
          </el-table-column>
        </template>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="handleCurrentChange"
      ></pagination>
    </el-card>

    <el-dialog
      title="选入配送产品"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="addDiProductVisible"
      width="85%"
      append-to-body
      v-if="addDiProductVisible"
    >
      <thrAddDiProductSelectUdi
        :closeDialog="closeDialog"
        :relId="relId"
        :isImportUdi="isImportUdi"
        :data="thisData"
        @closeUdi="closeUdi"
      >
      </thrAddDiProductSelectUdi>
    </el-dialog>

    <el-dialog
      title="新增配送产品"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="addProductVisible"
      width="85%"
      append-to-body
      v-if="addProductVisible"
    >
      <thrAddProductSelectUdi
        :closeAddDialog="closeDialog"
        :newType="newType"
        @closeUdi="closeUdi"
      >
      </thrAddProductSelectUdi>
    </el-dialog>

    <el-dialog
      title="器械信息详情"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="detailDialog"
      width="60%"
      append-to-body
      v-if="detailDialog"
    >
      <selectDiDetail :editQuery="diDetails" :remark="delect.remark">
      </selectDiDetail>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="产品信息详情"
      :visible.sync="thrProductsDetailVisible"
      width="80%"
      v-if="thrProductsDetailVisible"
    >
      <thrProductsDetail :data="thisDetail"></thrProductsDetail>
    </el-dialog>
  </div>
</template>
<script>
import {
  getThrDiProducts,
  delThrDiProducts,
  getDiProductDetail,
} from "@/api/supplier/supProductsAddDi";
import thrAddDiProductSelectUdi from "@/views/supplier/products/supAddDiProductSelectUdi";
import thrAddProductSelectUdi from "@/views/supplier/products/supProductsAdd";
import selectDiDetail from "./selectDIDetailDialog";
import thrProductsDetail from "@/views/supplier/products/supProductsDetail";
import {
  getHead, executeFuc
} from "@/utils/customConfig";
import store from "@/store";

export default {
  data() {
    return {
      showSearch: true,
      thisDetail: null,
      filterQuery: {
        sourceType: 0,
        code: null,
        name: null,
        spec: null,
        registerNo: null,
        manufactory: null,
        checkStatus: null,
        page: 1,
        limit: 20,
        productsType: 1
      },
      total: 0,
      tableHeader: [],
      queryList: [],
      fromList: [],
      tableHeader2: [],
      queryList2: [],
      fromList2: [],
      options: {},
      thirdSys: [],
      delect: null,
      thirdSysDetail: null,
      list: [],
      addDiProductVisible: false,
      addProductVisible: false,
      checkVisible: false,
      newType: 2,
      thrProductsDetailVisible: false,
      isImportUdi: false,
      relId: null,
      thisData: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
      },
      loading: false,
      checkMap: {
        1: "未审核",
        2: "已审核",
        3: "未通过",
      },
      curRow: null,
      diDetails: null,
      detailDialog: false,
      uploadFileUrl: null,
      uploadData: {
        thirdSys: "thirdId",
      },
      fileList: [],
      headers: {},
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        code: null,
        spec: null,
        registerNo: null,
        manufactory: null,
        checkStatus: null,
        name: null,
        page: 1,
        limit: 20,
        productsType: 1
      };
      this.getList();
    },
    search() {
      this.filterQuery.page = 1;
      this.getList();
    },
    checDialog(row) {
      this.checkVisible = true;
      this.curRow = row;
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    getList() {
      this.loading = true;
      getThrDiProducts(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    detail(_this, row) {
      if (row.type == 1) {
        let query = {devKey: row.deviceRecordKey}
        getDiProductDetail(query).then((res) => {
          if (res.code === 20000) {
            _this.delect = row;
            _this.diDetails = res.data;
            _this.detailDialog = true;
          } else {
            _this.$message.error("参数错误");
          }
        });
      } else {
        _this.thisDetail = row;
        _this.thrProductsDetailVisible = true;
      }
    },
    deleteProduct(_this, row) {
      let query = {
        id: row.id,
      };
      delThrDiProducts(query).then((res) => {
        if (res.code === 20000) {
          _this.$message.success("删除成功");
          _this.getList();
        } else {
          _this.$message.error("删除失败");
        }
      });
    },
    closeDialog() {
      this.selectErpDialogVisible = false;
      this.selectLocalVisible = false;
      this.selectVersionVisible = false;
      this.addProductVisible = false;
      this.getList();
    },
    closeUdi(val) {
      this.addDiProductVisible = false;
      if (val) {
        this.getList();
      }
    },
    executeFuc(row, type, clickFuc, value) {
      return executeFuc(this, row, type, clickFuc, value);
    },
    executeEval(row, expression, defaultRet) {
      if (expression) {
        return eval(expression);
      }
      return defaultRet;
    },
    handleChange(response, files, fileList) {
      console.log(response);
      if (response.code != 20000) {
        this.$message.error(response.message);
      } else {
        this.$message.success(response.data);
        this.getList();
      }
    },
  },
  components: {
    thrAddDiProductSelectUdi,
    selectDiDetail,
    thrAddProductSelectUdi,
    thrProductsDetail,
  },
  mounted() {
  },
  created() {
    getHead("supProductsOptinDi", "1").then((re) => {
      // 处理返回的数据
      this.tableObj = re.data;
      this.tableHeader = re.data.tableList;
      this.queryList = re.data.queryList;
      this.fromList = re.data.fromList;
      this.getList();
    });
    this.uploadFileUrl = process.env.VUE_APP_BASE_API + "/spms/thrAddDi/excel/upload";
    this.headers = {
      ADMIN_ID: this.$store.getters.adminId,
      ADMIN_TOKEN: this.$store.getters.token,
    };
    console.log(this.headers);
    console.log(this.$store.getters);

    console.log(this.uploadFileUrl);
  },
};
</script>
