<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="区域公司" prop="deptOne">
        <el-select v-model="queryParams.deptOne" placeholder="全部" clearable size="small" @change="oneChange">
          <el-option
            v-for="dict in deptOptions"
            :key="dict.deptCode"
            :label="dict.deptName"
            :value="dict.deptCode"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="大区" prop="deptTwo">
        <el-select :disabled="twoIsDisabled" v-model="queryParams.deptTwo" placeholder="全部" clearable size="small"  @change="oneTwoChange">
          <el-option
            v-for="dict in deptTwoOptions"
            :key="dict.deptCode"
            :label="dict.deptName"
            :value="dict.deptCode"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="销售部" prop="deptThree">
        <el-select :disabled="threeIsDisabled" v-model="queryParams.deptThree" placeholder="全部" clearable size="small" @change="onInput">
          <el-option
            v-for="dict in deptThreeOptions"
            :key="dict.deptCode"
            :label="dict.deptName"
            :value="dict.deptCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门店" prop="receiveShopName">
        <el-input
          v-model="queryParams.receiveShopName"
          placeholder="请输入接收门店编码/名称"
          clearable
          size="small"
          @input="onInput"
        />
      </el-form-item>
      <el-form-item label="任务" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称/编码"
          clearable
          size="small"
          @input="onInput"
        />
      </el-form-item>
      <el-form-item label="物品状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择物品状态" clearable size="small" @change="onInput">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['guide:signreport:export']"
          >导出</el-button>
        </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="signreportList" max-height="500">
      <el-table-column label="公司" align="center" prop="deptOne" width="180" :show-overflow-tooltip="true"/>
      <el-table-column label="大区" align="center" prop="deptTwo" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="销售部" align="center" prop="deptThree" width="200" />
      <el-table-column label="接收门店名称" align="center" prop="receiveShopName" width="280" :show-overflow-tooltip="true"/>
      <el-table-column label="接收门店编码" align="center" prop="receiveShopCode" width="120" />
      <el-table-column label="发放人" align="center" prop="issuer" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="发放人编码" align="center" prop="issuerCode" width="120"/>
      <el-table-column label="任务编码" align="center" prop="taskCode" width="90"/>
      <el-table-column label="任务名称" align="center" prop="taskName" width="220" :show-overflow-tooltip="true" />
      <el-table-column label="物料名称" align="center" prop="publicityName" width="220" :show-overflow-tooltip="true" />
      <el-table-column label="物料类型" align="center" prop="publicityType" width="190"/>
      <el-table-column label="发放数量" align="center" prop="extend" width="120"/>
      <el-table-column label="物品发放时间" align="center" prop="issuingTime" width="160"/>
      <el-table-column label="物品目前状态" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <dicts-tag :options="statusOptions" :value="scope.row.status.toString()"/>
        </template>
      </el-table-column>
      <el-table-column label="签收导购名称" align="center" prop="signGuidename" width="120"/>
      <el-table-column label="签收导购编码" align="center" prop="signGuidecode" width="120" />
      <el-table-column label="签收时间" align="center" prop="signTime" width="180"/>
      <el-table-column label="是否有签收照片" align="center" width="180">
        <template slot-scope="scope">
          <dicts-tag :options="yesnoOptions" :value="signImgFn(scope.row.signImgid)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['guide:sign:query']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listSignreport, getSignreport, delSignreport, addSignreport, updateSignreport, exportSignreport ,childListDept} from "@/api/guide/signreport";

export default {
  name: "Signreport",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料签收报表展示表格数据
      signreportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
        deptOptions:[],
        deptTwoOptions:[],
        deptThreeOptions:[],
        twoIsDisabled:true,
        threeIsDisabled:true,
        statusOptions:[],
        yesnoOptions:[],
    };
  },
  created() {
      this.getDicts("guide_sign_status").then(response => {
          this.statusOptions = response.data;
      });
      this.getDicts("sys_yes_no").then(response => {
          this.yesnoOptions = response.data;
      });
      this.getChildList(1,1);
    this.getList();
  },
  methods: {
      onInput(){
          this.$forceUpdate()
      },
      getChildList(parentId,level){
          childListDept(parentId).then(response=>{
              if(level==1){
                  this.deptOptions = response.data;
              }else if(level == 2){
                  this.deptTwoOptions = response.data;
              }else if(level == 3){
                  this.deptThreeOptions = response.data;
              }

          })
      },
      oneChange(val){
          console.log(val)
          this.$forceUpdate();
          if(val==""){
              this.twoIsDisabled=true;
              this.threeIsDisabled=true;
          }else{
            this.deptOptions.forEach((item)=>{
              if(item.deptCode == val){
                this.getChildList(item.deptId,2)
              }
            })

              this.twoIsDisabled=false;
              this.threeIsDisabled=true;
          }
          this.queryParams.deptTwo="";
          this.queryParams.deptThree="";
      },
      oneTwoChange(val){
          this.$forceUpdate();
          console.log(val)
          if(val==""){
              this.threeIsDisabled=true;
          }else{
            this.deptTwoOptions.forEach((item)=>{
              if(item.deptCode == val){
                this.getChildList(item.deptId,3)
              }
            })
              // this.getChildList(val,3)
              this.threeIsDisabled=false;
          }
          this.queryParams.deptThree="";
      },
    /** 查询物料签收报表展示列表 */
    getList() {
      this.loading = true;
      listSignreport(this.queryParams).then(response => {
        this.signreportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有物料签收报表展示数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSignreport(queryParams);
        }).then(response => {
        window.location.href = process.env.VUE_APP_BASE_API + "/common/download?fileName=" + encodeURI(response.msg) + "&delete=" + true;
          // this.downloads(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
      handleDetail(row){
          let query = this.$route.query;
          this.$router.push({path:'/material/signDetail',query:{id:row.id,from:'signreport'}});
      },
      signImgFn(val){
        if(val != null && val!=''){
            return 'Y'
        }else{
            return 'N'
        }
      },
  }
};
</script>
