var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    isShow: false,
    typeIndex: 1,
    sxArr: [
      {
        name: '积分类型',
        child: [
          { name: "全部", code: 0, checked: true },
          // { name: "绩效分", code: 1, checked: false },
          { name: "A分", code: 2, checked: false },
          { name: "B分", code: 3, checked: false },
        ],
      },
      {
        name: '任务状态',
        child: [
          { name: "全部", code: "all", checked: true },
          { name: "进行中", code: "running", checked: false },
          { name: "已完成", code: "complete", checked: false },
        ],
      },
      {
        name: '排序',
        child: [
          { name: "按发布时间", code: "publish", checked: true },
          { name: "按截止时间", code: "expire", checked: false },
        ],
      }
    ],
    sxArr2: [
      {
        name: '积分类型',
        child: [
          { name: "全部", code: 0, checked: false },
          // { name: "绩效分", code: 1, checked: false },
          { name: "A分", code: 2, checked: false },
          { name: "B分", code: 3, checked: false },
        ],
      },
      {
        name: '任务状态',
        child: [
          { name: "全部", code: "all", checked: false },
          { name: "进行中", code: "running", checked: false },
          { name: "已完成", code: "complete", checked: false },
        ],
      },
      {
        name: '排序',
        child: [
          { name: "按发布时间", code: "publish", checked: false },
          { name: "按截止时间", code: "expire", checked: false },
        ],
      }
    ],
  },
  props: {
    onConfirm: (data) => console.log(data),
    onClose: (data) => console.log(data),
    defaultSx: [0, 'all', 'publish'],
    typeIndex: 1,
  },
  didMount() {
    that = this;
    var sxArr = this.data.sxArr;
    if (this.props.defaultSx.length > 0) {
      sxArr.map(item => {
        item.child.map(item2 => {
          item2.checked = false;
          if (this.props.defaultSx[0] == item2.code) {
            item2.checked = true;
          }
          if (this.props.defaultSx[1] == item2.code) {
            item2.checked = true;
          }
          if (this.props.defaultSx[2] == item2.code) {
            item2.checked = true;
          }
        })
      })
    } else {
      this.props.defaultSx = [0, 'all', 'publish'];
      sxArr.map(item => {
        item.child.map(item2 => {
          item2.checked = false;
          if (this.props.defaultSx[0] == item2.code) {
            item2.checked = true;
          }
          if (this.props.defaultSx[1] == item2.code) {
            item2.checked = true;
          }
          if (this.props.defaultSx[2] == item2.code) {
            item2.checked = true;
          }
        })
      })
    }
    this.setData({ isShow: this.props.isShow, sxArr: sxArr, typeIndex: this.props.typeIndex })
  },
  methods: {
    //单
    ruleActive(e) {
      var index = e.target.dataset.index;
      var sxArrItem = `sxArr[${index}].child`;
      this.setData({
        [sxArrItem]: this.data.sxArr2[index].child
      })
      var indexChild = e.target.dataset.indexChild;
      var isChecked = this.data.sxArr[index].child[indexChild].checked;
      var elem = `sxArr[${index}].child[${indexChild}].checked`;
      this.setData({
        [elem]: !isChecked
      })
    },
    onConfirm() {
      var sxArr = this.data.sxArr;
      var arr = [];
      sxArr.forEach(item => {
        if (item.child.length > 0) {
          item.child.forEach((a) => {
            if (a.checked == true) {
              arr.push(a.code);
            }
          })
        }
      });
      this.props.onConfirm(arr);
      this.props.onClose();
    },
    //关闭
    onClose(e) {
      this.props.onClose();
    },
  },
});
