<template>
  <div>
    <el-card class="el-card">
      <el-form v-if="queryList && queryList.length > 0 "  :model="filterQuery" class="query-form" label-width="100px">
<!--        <el-row>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="采购单号">-->
<!--              <el-input v-model="filterQuery.billNo" style="width: 90%" placeholder="请输入单据号" clearable></el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

<!--          <el-col :span="8">-->
<!--            <el-form-item label="采购部门">-->
<!--              <el-select v-model="filterQuery.deptCode" placeholder="请选择申购部门" @change="getInvList" style="width: 90%">-->
<!--                <el-option-->
<!--                  v-for="item in deptList"-->
<!--                  :key="item.code"-->
<!--                  :label="item.name"-->
<!--                  :value="item.code"-->
<!--                >-->
<!--                  <span style="float: left">{{ item.name }}</span>-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

<!--          <el-col :span="8">-->
<!--            <el-form-item label="采购仓库">-->
<!--              <el-select v-model="filterQuery.invCode" style="width: 90%"  placeholder="请选择所属仓库">-->
<!--                <el-option-->
<!--                  v-for="item in invList"-->
<!--                  :key="item.name"-->
<!--                  :label="item.name"-->
<!--                  :value="item.code">-->
<!--                  <span style="float: left">{{ item.name }}</span>-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

<!--        <el-row>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="紧急程度：" prop="emergency">-->
<!--              <el-select v-model="filterQuery.emergency" placeholder="请选择紧急程度" style="width: 90%" clearable>-->
<!--                <el-option label="正常" :value=1></el-option>-->
<!--                <el-option label="较急" :value=2></el-option>-->
<!--                <el-option label="特急" :value=3></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="8">-->
<!--            <el-form-item label="创建时间:">-->
<!--              <el-date-picker-->
<!--                :picker-options="pickerOptions"-->
<!--                v-model="actDateRange"-->
<!--                type="daterange"-->
<!--                format="yyyy 年 MM 月 dd 日"-->
<!--                style="width: 90%"-->
<!--                value-format="yyyy-MM-dd"-->
<!--                range-separator="至"-->
<!--                start-placeholder="开始日期"-->
<!--                end-placeholder="结束日期"-->
<!--              >-->
<!--              </el-date-picker>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
        <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)" :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 == 'select' && executeEval(row,item.expression,true)" :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)" :label="item.columnDesc+`:`">
              <el-select
                v-model="filterQuery[item.columnName]"
                :placeholder="item.columnDesc"
                :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)" :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>
            </div>
          </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="onSubmitFind">查询</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row
                @current-change="(row) => executeFuc(row,'0',tableObj.handleChangeFuc)"
                :default-sort="defaultSort"  @sort-change="handleSortChange">
<!--        <el-table-column label="序号" type="index" width="60"></el-table-column>-->
<!--        <el-table-column label="采购单号" prop="billNo"></el-table-column>-->
<!--        <el-table-column label="采购部门" prop="deptName"></el-table-column>-->
<!--        <el-table-column label="采购仓库" prop="invName"></el-table-column>-->
<!--        <el-table-column label="紧急程度" prop="emergency">-->
<!--          <template slot-scope="scope">-->
<!--            {{ emergencyMap[scope.row.emergency] }}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="到货截止" prop="arrivalTime"></el-table-column>-->
<!--        <el-table-column label="单据状态" prop="status">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag :type="(scope.row.status) | statusFilterType">-->
<!--              {{ statusMap[scope.row.status] }}-->
<!--            </el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="创建时间" prop="createTime"-->
<!--                         sortable="custom" :sort-orders="['ascending', 'descending']"></el-table-column>-->


<!--        <el-table-column label="操作" width="60px">-->
<!--          <template slot-scope="scope">-->
<!--            <el-button type="text" :disabled="scope.row.status!=2"-->
<!--                       @click.native="newDistributionForm(scope.$index, 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="handleCurrentChange"

      ></pagination>


    </el-card>
    <el-card class="el-card">
      <el-table v-loading="loading" :data="detailList" style="width: 100%" border highlight-current-row>
<!--        <el-table-column label="序号" type="index" width="60"></el-table-column>-->
<!--        <el-table-column label="DI/物资编码" prop="nameCode"></el-table-column>-->
<!--        <el-table-column label="物资名称" prop="productName" show-overflow-tooltip="true"></el-table-column>-->
<!--        <el-table-column label="规格型号" prop="spec" show-overflow-tooltip="true"></el-table-column>-->
<!--        <el-table-column label="计量单位" prop="measname" show-overflow-tooltip="true" width="100"></el-table-column>-->
<!--        <el-table-column label="申购数量" prop="count"></el-table-column>-->
<!--        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh"></el-table-column>-->
<!--        <el-table-column label="生产厂家" prop="manufactory"></el-table-column>-->
<!--        <el-table-column label="供应商" prop="supName"></el-table-column>-->
        <template v-for="(item, index) in tableHeader2">
          <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>
    </el-card>

    <el-dialog
      :title="formMap[formName]"
      :visible.sync="newSpDistributionVisible"
      width="80%"
      v-if="newSpDistributionVisible"
      @close='closeDialog'
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <purPlanEdit
        :closeDialog="closeDialog"
        :idQuery="idQuery"
        :editType="editType"
      ></purPlanEdit>
    </el-dialog>
  </div>
</template>
<script>
import purPlanEdit from "./purPlanDetailDialog";
import {delApply, auditListPlan, listApplyDetail} from "@/api/purchase/purPlan";
import { getInvListByUser } from '@/api/system/invWarehouse'
import {getDeptListByUser, getDeptListByUserOptimize} from '@/api/auth/authDept'
import {filterSubAll, filterSubAllOptimize} from "@/api/system/invSubWarehouse";
import {
  getHead,executeFuc
} from "@/utils/customConfig";

export default {
  data() {
    return {
      pageType:null,
      defaultSort: {prop: 'createTime', order: 'desc'},
      showSearch: false,
      filterQuery: {
        billAction: null,
        billNo: "",
        originType: null,
        thirdSysFk: "",
        page: 1,
        limit: 10,
        corpName: null,
        type: 1,
        editStatus: 1,
        emergency:'',
        deptCode:'',
        invCode:null,
        pageType: this.$route.query.pageType,
        productType: 1,
      },
      formName: null,
      formMap: {
        add: "新增采购计划",
        update: "采购计划审核",
      },
      statusMap: {
        1: "草稿",
        2: "未审核",
        3: "已审核",
        4: "已拒绝"
      },
      emergencyMap: {
        1: "正常",
        2: "较急",
        3: "特急",
      },
      invList: {},
      deptList: {},
      idQuery: {},
      editType: 1,
      total: 0,
      tableHeader:[],
      queryList:[],
      fromList:[],
      tableHeader2:[],
      queryList2:[],
      fromList2:[],
      options: {
        getInvList:[],
        getDeptList:[],
      },
      thirdSys: [],
      thirdSysDetail: null,
      busTypes: [],
      originTypes: [],
      list: [],
      detailList: [],
      loading: false,
      invCodebe:null,
      actDateRange: [],
      newSpDistributionVisible: false,
      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]);
            },
          },
        ],
      },
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        billNo: "",
        thirdSysFk: "",
        billFlag: null,
        billAction: null,
        startDate: null,
        endDate: null,
        page: 1,
        limit: 10,
        corpName: null,
        type: 1,
        editStatus: 1,
        deptCode: this.invCodebe,
        invCode:null,
        pageType: this.pageType,
        productType: 1,
      };
      this.getInvList(this)
      this.actDateRange = [];
      this.getList();
    },

    onSubmitFind() {
      this.filterQuery.page = 1;
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },

    handleSortChange(column, prop, order) {
      if (column.order === 'descending') {
        this.filterQuery.sort = 'desc'
      } else {
        this.filterQuery.sort = 'asc'
      }
      this.filterQuery.orderBy = column.prop;
      this.getList();
    },



    getList() {
      if (this.actDateRange != null) {
        this.filterQuery.startDate = this.actDateRange[0];
        this.filterQuery.endDate = this.actDateRange[1];
      } else {
        this.filterQuery.startDate = null;
        this.filterQuery.endDate = null;
      }
      this.loading = true;
      auditListPlan(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
            this.detailList=[]
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    handleDetail(_this,row) {
      let query = {orderIdFk: row.id};
      _this.loading = true;
      listApplyDetail(query) //查找该单号下的所有条码
        .then((response) => {
          _this.detailList = response.data.list || [];
          _this.loading = false;
        })
        .catch(() => {
          _this.loading = false;
          _this.detailList = [];
        });
    },
    deleteDialog(_this,row) {
      _this.$confirm("此操作将永久删除该单据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delApply(row.id)
            .then((response) => {
              _this.loading = false;
              if (response.code == 20000) {
                _this.$message.success("删除成功");
                _this.getList();
              } else {
                _this.$message.error(response.message);
              }
            })
            .catch(() => {
              _this.loading = false;
            });

        })
        .catch(() => {
        });
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },

    closeDialog() {
      this.newSpDistributionVisible = false;
      this.getList();
      this.detailList = [];
    },


    newDistributionForm(_this, row) {
      _this.idQuery.id = '';
      if (_this.$isNotBlank(row) && _this.$isNotBlank(row.id)) {
        _this.idQuery.id = row.id;
        _this.idQuery.formData = row;
        _this.formName = "update";
      } else
        _this.formName = "add";
      _this.newSpDistributionVisible = true;
    },
    getInvList(_this) {
      var query={
        parentId:_this.filterQuery.deptCode
      }
      _this.filterQuery.invCode = null
      filterSubAllOptimize(query).then((res) => {
        _this.options.getInvList = res.data || [];
      })
    },
    getDeptList(_this) {
      getDeptListByUserOptimize().then((res) => {
        _this.options.getDeptList = res.data || [];
        if( _this.options.getDeptList.length>0){
          _this.filterQuery.deptCode= _this.options.getDeptList[0].code
          _this.invCodebe=this.filterQuery.deptCode
        }
        _this.getList();
        _this.getInvList(_this);
      });
    },
    executeFuc(row,type,clickFuc,value){
      return executeFuc(this,row,type,clickFuc,value);
    },
    executeEval(row,expression,defaultRet){
      if(expression){
        return eval(expression);
      }
      return defaultRet;
    }
  }
  ,
  components: {
    purPlanEdit,
  }
  ,
  filters: {
    statusFilterType(status) {
      const statusMap = {
        2: "warning",
        3: "success",
        4: "danger",
      };
      return statusMap[status];
    },
  }
  ,
  mounted() {
  }
  ,
  created() {
    this.getDeptList(this);
    getHead("purPlanAudit-1","1").then((re) => {
      // 处理返回的数据
      this.tableObj = re.data;
      this.tableHeader = re.data.tableList;
      this.queryList = re.data.queryList;
      this.fromList = re.data.fromList;
    });
    getHead("purPlanAudit-2","1").then((re) => {
      // 处理返回的数据
      this.tableObj2 = re.data;
      this.tableHeader2 = re.data.tableList;
      this.queryList2 = re.data.queryList;
      this.fromList2 = re.data.fromList;
    });
    this.pageType = this.$route.query.pageType;
  }
  ,
}
;
</script>
<style scoped>
.itemTag {
  float: left;
  text-align: left;
  margin-top: 10px;
  width: 25%;
}

.text {
  font-size: 13px;
  font-family: "Microsoft YaHei";
}


.el-row {
  display: flex;
  flex-wrap: wrap;
}

.el-col {
  border-radius: 4px;
  flex-wrap: wrap;
}
</style>

