<template>
  <div>
    <el-card>
      <el-form v-if="queryList && queryList.length > 0" :model="filterQuery" class="query-form"
               size="mini" label-width="100px">
        <el-row style=" display:flex;  flex-wrap: wrap; ">
          <template v-for="(item, index) in queryList">
            <div v-if="showSearch || item.isImport">
              <el-form-item v-if="item.columnType == 'input' && executeEval(row,item.expression,true)"
                            class="query-form-item"
                            :label="item.columnDesc+`:`" :key="item.id">
                <el-input
                  v-model="filterQuery[item.columnName]"
                  :placeholder="item.columnDesc == '模糊查询' ? '产品通用名/规格/批次号' : 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 == 'select' && executeEval(row,item.expression,true)"
                            class="query-form-item"
                            :label="item.columnDesc+`:`">
                <el-select v-model="filterQuery[item.columnName]"
                           :placeholder="item.columnDesc"
                           :disabled="executeEval(null,item.disabledFuc,false)"
                           clearable>
                  <el-option
                    v-for="dict in item.lableRuleObj"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-if="item.columnType == 'selectServer' && executeEval(row,item.expression,true)"
                            class="query-form-item"
                            :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)"
                  filterable
                  remote
                  :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"
                  />
                </el-select>
              </el-form-item>
              <el-form-item v-if="item.columnType == 'datePicker' && executeEval(row,item.expression,true)"
                            class="query-form-item"
                            :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)"
                            class="query-form-item"
                            :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>
            </div>
          </template>
        </el-row>
      </el-form>
      <!--      <div class="left-search" v-if="!showSearch">-->
      <!--        &lt;!&ndash; 关键字搜索 按需配置 &ndash;&gt;-->
      <!--        <el-form v-if="queryList && queryList.length > 0" :model="filterQuery" class="query-form" size="mini"-->
      <!--                 label-width="100px">-->
      <!--          <el-row style=" display:flex;  flex-wrap: wrap; ">-->
      <!--            <template v-for="(item, index) in queryList">-->
      <!--              <el-form-item-->
      <!--                v-if="item.columnType == 'input' && executeEval(row,item.expression,true) && item.columnName == 'keywords'"-->
      <!--                class="query-form-item"-->
      <!--                :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>-->
      <!--            </template>-->
      <!--          </el-row>-->
      <!--        </el-form>-->
      <!--      </div>-->
      <div class="top-right-btn">
        <el-button-group>
          <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="onSubmit(this)"
          >查询
          </el-button
          >
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border :default-sort="defaultSort"
                :row-style="{ height: '32px' }"
                @row-click="(row) => executeFuc(row,'0',tableObj.handleChangeFuc)"
                @sort-change="handleSortChange"
      >

        <!--<el-table-column label="操作" width="100" fixed="right" v-if="splitType!='search'">-->
        <!--  <template slot-scope="scope">-->
        <!--    <el-button-->
        <!--      type="text"-->
        <!--      size="small"-->
        <!--      @click.native.stop="editOrder(scope.row)"-->
        <!--    >编辑-->
        <!--    </el-button-->
        <!--    >-->
        <!--    <el-button-->
        <!--      type="text"-->
        <!--      size="small"-->
        <!--      @click.native.stop="deleteDialog(scope.row)"-->
        <!--    >删除-->
        <!--    </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"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="getList"
      />
    </el-card>

    <el-dialog
      title="损耗出库"
      :visible.sync="destroyOutDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="60%"
      v-if="destroyOutDialogVisible"
    >
      <destroyOutDialog
        :prescribeData="prescribeData"
        :destroyType="destroyType"
        :closeDialog="closeDialog"
      >
      </destroyOutDialog>
    </el-dialog>
  </div>

</template>

<script>
import destroyOutDialog from "./destroyOutDialog.vue";
import {getUserBindWork} from '@/api/basic/collectPoint/userWorkplace'
import {executeFuc, getHead} from '@/utils/customConfig'
import {getSplitCode} from '@/api/collect/collectOrder'
import {getWorkBindBusTypes} from '@/api/basic/workPlace/sysWorkplaceDocuments'
import {filterWorkOptimize} from '@/api/basic/workPlace/sysWorkplaceManage'

export default {
  name: 'IoDestroyOrder',
  data() {
    return {
      busQuery: {
        busKey: '',
        workplaceCode: null,
        page: 1,
        limit: 100
      },
      viewType: null,
      tableHeader: [],
      queryList: [],
      tableObj: [],
      fromList: [],
      defaultSort: {prop: 'createTime', order: 'desc'},
      showSearch: false,
      filterQuery: {
        workPlaceCode: null,
        busType: null,
        page: 1,
        limit: 10,
        startTime: null,
        endTime: null,
        keywords: null,
      },
      options: {
        getWorkPlaceList: [],
        getBusType: [],
        findWorkPlace: [],

      },
      actDateRange: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      list: [],
      showType: 1,
      isLinkDisabled: false,
      destroyOutDialogVisible: false,
      prescribeData: {},
      destroyType: 3,//1 产品损耗 2.根据code损耗3：拆零表损耗
    }
  },
  components: {destroyOutDialog},
  methods: {
    changePlace(_this, query) {
      _this.getWorkBindBusTypes(_this, query)
    },
    getWorkBindBusTypes(_this, query) {
      _this.busQuery.workplaceCode = query
      getWorkBindBusTypes(this.busQuery).then(res => {
        if (res.code == 20000) {
          let busTypes = res.data.list || [];
          // _this.options.getWorkPlace = res.data.list || [];
          let getWorkPlace = busTypes.map(busType => {
            return {
              code: busType.documentTypeCode,
              label: busType.busName
            };
          });
          _this.options.getBusType = getWorkPlace
          if (_this.options.getBusType.length == 1) {
            _this.filterQuery.busType = _this.options.getBusType[0].code
          } else {
            _this.filterQuery.busType = null
          }
        }
        return
      })
    },
    executeFuc(row, type, clickFuc, value) {
      return executeFuc(this, row, type, clickFuc, value);
    },
    executeEval(row, expression, defaultRet) {
      if (expression) {
        return eval(expression);
      }
      return defaultRet;
    },
    handleSortChange(column, prop, order) {
      if (column.order === 'descending') {
        this.filterQuery.sort = 'desc'
      } else {
        this.filterQuery.sort = 'asc'
      }
      this.filterQuery.orderBy = column.prop;
      this.getList();
    },

    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.filterQuery = {
        ...this.filterQuery,
        workPlaceCode: null,
        page: 1,
        limit: 10,
        startTime: null,
        endTime: null,
        keywords: null,

      };
      this.actDateRange = [];
      // this.getList();
    },
    onSubmit(_this) {
      if (_this == null)
        _this = this
      if (_this.actDateRange !== null) {
        _this.filterQuery.startTime = _this.actDateRange[0];
        _this.filterQuery.endTime = _this.actDateRange[1];
      } else {
        _this.filterQuery.startTime = null;
        _this.filterQuery.endTime = null;
      }
      _this.filterQuery.page = 1;
      _this.getList();
    },
    getList() {
      getSplitCode(this.filterQuery).then(res => {
        if (res.code == 20000) {
          if (res.code === 20000) {
            this.list = res.data.list || [];
            this.total = res.data.total || 0;
          } else {
            this.$message.error(res.message);
          }
        }
      })
        .catch((error) => {
          this.$message.error(error.message)
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    //损耗出库
    destroyOut(_this, row) {
      _this.prescribeData = row
      _this.prescribeData.busType = _this.$route.query.busType
      _this.destroyType = 3
      _this.destroyOutDialogVisible = true;
    },
    closeDialog() {
      this.destroyOutDialogVisible = false
      this.getList()
    },
    findWorkPlace(_this, val) {
      let query = {
        // chargeUser: _this.$store.getters.userId,
        userIdFlag: true,
        key: val,
        page: 1,
        limit: 10,
      }
      filterWorkOptimize(query)
        .then((response) => {
          _this.loading = false;
          _this.options.findWorkPlace = response.data || [];
          if (_this.options.findWorkPlace.length == 1) {
            _this.filterQuery.workPlaceCode = _this.options.findWorkPlace[0].code;
            _this.getWorkBindBusTypes(_this, _this.filterQuery.workPlaceCode)
          }
        })
        .catch(() => {
          _this.loading = false;
          _this.options.findWorkPlace = [];
        });
    },
  },
  filters: {},
  created() {

    if (this.$route.query.workplaceId != null) {
      this.isLinkDisabled = true
      this.filterQuery.workPlaceCode = this.$route.query.workplaceId
      this.showType = 2
    }
    this.findWorkPlace(this, "")

    getHead("ioSplitCode", "1").then((re) => {
      // 处理返回的数据
      this.tableObj = re.data;
      this.tableHeader = re.data.tableList;
      this.queryList = re.data.queryList;
      this.fromList = re.data.fromList;
      this.getList()
    });
  }
}
</script>


<style scoped>


.query-form-item {
  margin-right: 5px;
  margin-bottom: 6px;
}


</style>



