<template>
  <div style="background:#eee;padding: 20px">
    <Card :bordered="false">
      <p slot="title" style="height: 35px;">
        <Select v-model="taskRuleId" style="width:200px">
          <Option
            v-for="item in taskRuleList"
            :value="item.value"
            :key="item.value"
            >{{ item.label }}</Option
          >
        </Select>
        <el-button
          type="primary"
          style="float: right;margin-right: 30px;"
          @click="addTaskRulePage"
          >新增</el-button
        >
      </p>
      <p style="height: 30px;">
        <span style="margin: 5px;"
          >审核通过{{ socreShtg }}/预计完成{{ socreYjwc }}</span
        >
      </p>
      <p>
        <Progress
          :percent="socreShtgRate"
          :stroke-width="30"
          status="active"
          text-inside
        />
      </p>
    </Card>

    <Card style="margin-top: 10px;">
      <div>
        <Table border :columns="totalZjScoreColumns" :data="totalZjScoreData">
          <template slot-scope="{ row }" slot="name">
            <strong>{{ row.name }}</strong>
          </template>
        </Table>
      </div>
      <!--      <div style="margin-top: 30px;">-->
      <!--        <el-button type="primary" @click="showZjDiag">分配</el-button>-->
      <!--      </div>-->
    </Card>

    <Card style="margin-top: 10px;">
      <p slot="title">
        分配积分
      </p>
      <p>
        <avue-crud
          ref="crud"
          :data="tableData"
          v-model="form"
          :permission="permissionList"
          :table-loading="tableLoading"
          :option="tableOption"
          @on-load="getList"
          @search-change="searchChange"
          @refresh-change="refreshChange"
          @size-change="sizeChange"
          @current-change="currentChange"
          @row-update="handleUpdate"
          @row-save="handleSave"
          @row-del="rowDel"
        >
          <template slot="menu" slot-scope="scope">
            <el-button
              :hidden="scope.row.isConduct == '1'"
              type="text"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope.row, scope.index)"
              >编辑
            </el-button>
            <el-button
              :disabled="scope.row.packageAuditStatus == 1"
              type="text"
              size="small"
              icon="el-icon-edit"
              @click="taskTj(scope.row, scope.index)"
              >审核
            </el-button>
          </template>
        </avue-crud>
      </p>
    </Card>
  </div>
</template>
<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  selWmscorepackageList,
  getWmScorePackagePageTask
} from "@/api/wmscorepackage";
import { selWmtaskruleList } from "@/api/wmtaskrule";
import { drugEntList } from "@/api/wmdadrugent";
import { tableOptionFp } from "@/const/crud/wmscorepackage";

import { mapGetters } from "vuex";
export default {
  data() {
    return {
      isFirstInit: true,
      taskRuleId: 0,
      socreYjwc: 0,
      socreShtg: 0,
      socreShtgRate: 0,
      socreZj: 0,
      jifenbao: {
        id: "",
        value: "",
        deptId: ""
      },
      modalAgent: "",
      modalZj: false,
      taskRuleList: [
        // {
        //   value: 'New York',
        //   label: 'New York'
        // }
      ],
      agentList: [
        // {
        //   value: 'New York',
        //   label: 'New York'
        // }
      ],
      totalScoreModel: "",
      totalZjScoreColumns: [
        {
          title: "已分配",
          key: "yfp",
          align: "center"
        },
        {
          title: "已收到",
          key: "ysd",
          align: "center"
        },
        {
          title: "审核通过",
          key: "shtg",
          align: "center"
        }
      ],
      totalZjScoreData: [
        // {
        //   // yfp: 0,
        //   // ysd: 0,
        //   // shtg: 0
        // }
      ],
      totalFpScoreColumns: [
        {
          title: "积分包ID",
          key: "jfbid",
          align: "center"
        },
        {
          title: "分配对象",
          key: "fpdx",
          align: "center"
        },
        {
          title: "应完成",
          key: "ywc",
          align: "center"
        },
        {
          title: "已提交",
          key: "ytj",
          align: "center"
        },
        {
          title: "审核通过",
          key: "shtg",
          align: "center"
        },
        {
          title: "进度",
          key: "jd",
          align: "center"
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "el-button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.show(params.index);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "el-button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.remove(params.index);
                    }
                  }
                },
                "审核"
              )
            ]);
          }
        }
      ],
      searchForm: {},
      tableData: [],
      form: {},
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 1000 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOptionFp
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        // addBtn: this.vaildData(this.permissions.admin_wmscorepackage_add, false),
        // delBtn: this.vaildData(this.permissions.admin_wmscorepackage_del, false),
        // editBtn: this.vaildData(this.permissions.admin_wmscorepackage_edit, false)
      };
    }
  },
  mounted() {
    this.init();
  },
  watch: {
    taskRuleId: function(val, oldVal) {
      this.init();
    }
  },
  methods: {
    handleEdit(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    init() {
      // if(0==this.taskRuleId)
      // {
      //   this.taskRuleId=1;
      //   return;
      // }
      this.refreshChange();
      if (this.isFirstInit) {
        var obj = {
          //   typeid:5,
          //   taskRuleId:1
        };
        selWmtaskruleList(obj)
          .then(data => {
            if (undefined != data.data) {
              var date = new Date();
              var nowYear = date.getFullYear();
              var nowMonth =
                date.getMonth() + 1 < 10
                  ? "0" + (date.getMonth() + 1)
                  : date.getMonth() + 1;
              var date_str = nowYear + "-" + nowMonth;
              var answer = data.data.data;
              for (var i = 0; i < answer.length; i++) {
                if (answer[i].ruleMonth == date_str) {
                  this.taskRuleId = answer[i].id;
                }
                this.taskRuleList.push({
                  value: answer[i].id,
                  label: answer[i].ruleMonth
                });
              }
            }
          })
          .catch(() => {
            loading();
          });

        drugEntList(obj)
          .then(data => {
            if (undefined != data.data) {
              var answer = data.data.data;
              for (var i = 0; i < answer.length; i++) {
                this.agentList.push({
                  value: answer[i].id,
                  label: answer[i].entname
                });
              }
            }
          })
          .catch(() => {
            loading();
          });
        this.isFirstInit = false;
      }
    },
    showZjDiag() {
      this.modalZj = true;
    },
    remove(index) {
      this.data6.splice(index, 1);
    },
    getList(page, params) {
      if (undefined == this.taskRuleId || this.taskRuleId == 0) {
        return false;
      }
      this.totalZjScoreData = [];
      this.searchForm.typeid = 1;
      this.searchForm.agentTypeId = 1;
      this.searchForm.taskRuleId = this.taskRuleId;
      this.tableLoading = true;
      getWmScorePackagePageTask(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          let resData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;

          var yfp = 0;
          var ysd = 0;
          var shtg = 0;
          if (resData.length > 0) {
            for (var i = 0; i < resData.length; i++) {
              if (undefined != resData[i].ytj) {
                ysd += resData[i].ytj;
              }

              if (undefined != resData[i].ysh) {
                shtg += resData[i].ysh;
                if (undefined != resData[i].score) {
                  var socre = parseFloat(
                    (resData[i].ysh * 100) / resData[i].score
                  ).toFixed(0);
                  if (socre > 100) {
                    socre = 100;
                  }
                  resData[i].scoreJd = socre;
                }
              }

              if (undefined != resData[i].score) {
                yfp += resData[i].score;
              }
            }
            this.socreShtg = shtg;
            this.socreYjwc = yfp;

            if (yfp > 0) {
              var socre = parseFloat((shtg * 100) / yfp).toFixed(0);
              if (socre > 100) {
                socre = 100;
              }
              this.socreShtgRate = socre;
            }
            this.totalZjScoreData.push({
              yfp: yfp,
              ysd: ysd,
              shtg: shtg
            });
          }

          this.tableData = resData;

          // this.totalZjScoreData=[];
          // var obj={
          //   typeid:1,
          //   agentTypeId:1,
          //   taskRuleId:this.taskRuleId
          // };
          // selWmscorepackageList(obj).then(data => {
          //   debugger;
          //   if(undefined!=data.data){
          //     var answer=data.data.data;
          //     var yfp=0;
          //     var ysd=0;
          //     var shtg=0;
          //     if(resData.length>0)
          //     {
          //       for(var i=0;i<resData.length;i++)
          //       {
          //         for(var j=0;j<answer.length;j++){
          //           if(answer[j].id==resData[i].id)
          //           {
          //             resData[i].ytj=answer[j].ytj
          //             resData[i].ysh=answer[j].ysh
          //             if(undefined!=answer[j].ytj&&answer[j].ytj>0)
          //             {
          //               resData[i].scoreJd=parseFloat(shtg/yfp).toFixed(0);
          //             }
          //           }
          //         }
          //         if(undefined!=resData[i].score)
          //         {
          //           yfp+=resData[i].score;
          //         }
          //
          //         if(undefined!=resData[i].ytj)
          //         {
          //           ysd += resData[i].ytj;
          //         }
          //
          //         if(undefined!=resData[i].ysh) {
          //           shtg = resData[i].ysh;
          //         }
          //
          //       }
          //       this.socreYjwc=yfp;
          //       this.socreShtg=shtg;
          //       this.socreShtgRate=parseFloat(shtg/yfp).toFixed(0);
          //       this.tableData=resData;
          //
          //       this.totalZjScoreData.push({
          //         yfp:yfp,
          //         ysd:ysd,
          //         shtg:shtg
          //       });
          //     }
          //   }
          // })
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel: function(row, index) {
      this.$confirm("是否确认删除ID为" + row.id, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.id);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    handleUpdate: function(row, index, done, loading) {
      let rowScore = row.ytj;
      if (row.score < row.ytj) {
        this.$message.error("修改积分包分值不能小于已提交分值");
        return false;
      }
      putObj(row)
        .then(data => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleSave: function(row, done, loading) {
      row.typeid = 1;
      row.agentTypeId = 1;
      row.taskRuleId = this.taskRuleId;
      if (undefined == this.taskRuleId || this.taskRuleId == 0) {
        this.$message.error("请选择规则月份");
        return false;
      }
      addObj(row)
        .then(data => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },
    addTaskRulePage() {
      this.$router.push({ path: "/admin/wmtaskrule/index" });
    },
    taskTj(row, index) {
      this.$router.push({ path: "/admin/wmtask/indexYq" });
    }
  }
};
</script>
