var app = getApp()
var that;
Component({
  mixins: [],
  data: {
    isShow: false,

    sxArr: [
      {
        name: '人员',
        child: [
          { name: "全部", code: "all", checked: true },
          { name: "管理者", code: "manager", checked: false },
          { name: "员工", code: "employee", checked: false },
        ],
      },
      {
        name: '排序',
        child: [
          { name: "从高到低", code: "DESC", checked: true },
          { name: "从低到高", code: "ASC", checked: false },
        ],
      }
    ],
    sxArr2: [
      {
        name: '人员',
        child: [
          { name: "全部", code: "all", checked: false },
          { name: "管理者", code: "manager", checked: false },
          { name: "员工", code: "employee", checked: false },
        ],
      },
      {
        name: '排序',
        child: [
          { name: "从高到低", code: "DESC", checked: false },
          { name: "从低到高", code: "ASC", checked: false },
        ],
      }
    ]
  },
  props: {
    onConfirm: (data) => console.log(data),
    onClose: (data) => console.log(data),
    defaultSx:['all','DESC'],
  },
  didMount() {
    that = this;
    var sxArr=this.data.sxArr;
    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;
          }
        })
    })
    this.setData({ isShow: this.props.isShow,sxArr:sxArr })
  },
  didUpdate() { },
  didUnmount() { },
  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();
    },
  },
});
